Skip to content

Commit

Permalink
Merge pull request #1173 from yaacov/save-network-map-namespace-2-6-2
Browse files Browse the repository at this point in the history
🐞 Save network map target namespace
  • Loading branch information
yaacov authored May 27, 2024
2 parents ddc8d56 + 1972c8a commit ee0a85e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'src/modules/Providers/hooks/useNetworks';
import { MappingList } from 'src/modules/Providers/views/migrate/components/MappingList';
import { Mapping } from 'src/modules/Providers/views/migrate/types';
import { updateNetworkMapDestination } from 'src/modules/Providers/views/migrate/useSaveEffect';
import { useForkliftTranslation } from 'src/utils/i18n';

import {
Expand Down Expand Up @@ -64,7 +65,10 @@ export const MapsSection: React.FC<MapsSectionProps> = ({ obj }) => {

const onUpdate = async () => {
dispatch({ type: 'SET_UPDATING', payload: true });
await k8sUpdate({ model: NetworkMapModel, data: state.networkMap });
await k8sUpdate({
model: NetworkMapModel,
data: updateNetworkMapDestination(state.networkMap),
});
dispatch({ type: 'SET_UPDATING', payload: false });
};

Expand Down Expand Up @@ -162,7 +166,7 @@ export const MapsSection: React.FC<MapsSectionProps> = ({ obj }) => {
isDisabled={!state.hasChanges || state.updating}
icon={state.updating ? <Spinner size="sm" /> : undefined}
>
{t('Update providers')}
{t('Update mappings')}
</Button>
</FlexItem>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useReducer } from 'react';
import { Suspend } from 'src/modules/Plans/views/details/components';
import { updateNetworkMapDestination } from 'src/modules/Providers/views/migrate/useSaveEffect';
import { useForkliftTranslation } from 'src/utils/i18n';

import {
Expand Down Expand Up @@ -42,7 +43,10 @@ export const ProvidersSection: React.FC<ProvidersSectionProps> = ({ obj }) => {

const onUpdate = async () => {
dispatch({ type: 'SET_UPDATING', payload: true });
await k8sUpdate({ model: NetworkMapModel, data: state.networkMap });
await k8sUpdate({
model: NetworkMapModel,
data: updateNetworkMapDestination(state.networkMap),
});
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const mapTargetNetworksIdsToLabels = (
): { [label: string]: string } => {
const tuples: [string, string][] = targets
.filter(({ namespace }) => namespace === plan.spec.targetNamespace || namespace === 'default')
.map((net) => [net.uid, net.name]);
.map((net) => [net.uid, `${net.namespace}/${net.name}`]);

tuples.push(['pod', POD_NETWORK]);
const labelToId = resolveCollisions(tuples);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function patchPlanMappingsData(
{
op: 'replace',
path: '/spec/map',
value: updatedNetwork,
value: updateNetworkMapSpecMapDestination(updatedNetwork),
},
],
});
Expand All @@ -47,3 +47,24 @@ export async function patchPlanMappingsData(
],
});
}

/**
* Updates the destination name and namespace in the network map entries.
* If the destination name contains a '/', it splits the name into two parts.
* The first part becomes the new namespace, and the second part becomes the new name.
*
* @param {NetworkMap} networkMap - The network map object to update.
* @returns {NetworkMap} The updated network map object.
*/
export function updateNetworkMapSpecMapDestination(
networkMaps: V1beta1NetworkMapSpecMap[],
): V1beta1NetworkMapSpecMap[] {
networkMaps?.forEach((entry) => {
const parts = entry.destination.name.split('/');
if (parts.length === 2) {
entry.destination.namespace = parts[0];
entry.destination.name = parts[1];
}
});
return networkMaps;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const calculateNetworks = (
const targetNetworkNameToUid = Object.fromEntries(
existingResources.targetNetworks
.filter(({ namespace }) => namespace === plan.spec.targetNamespace || namespace === 'default')
.map((net) => [net.name, net.uid]),
.map((net) => [`${net.namespace}/${net.name}`, net.uid]),
);
const targetNetworkLabels = [
POD_NETWORK,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useRef } from 'react';
import { useHistory } from 'react-router';
import { produce } from 'immer';
import { deepCopy } from 'src/utils';

import {
NetworkMapModel,
Expand All @@ -25,11 +26,12 @@ const createStorage = (storageMap: V1beta1StorageMap) =>
data: storageMap,
});

const createNetwork = (netMap: V1beta1NetworkMap) =>
k8sCreate({
const createNetwork = (netMap: V1beta1NetworkMap) => {
return k8sCreate({
model: NetworkMapModel,
data: netMap,
data: updateNetworkMapDestination(netMap),
});
};

const createPlan = async (
plan: V1beta1Plan,
Expand Down Expand Up @@ -108,3 +110,24 @@ export const useSaveEffect = (state: CreateVmMigrationPageState, dispatch) => {
.catch((error) => mounted.current && dispatch(setAPiError(error)));
}, [state.flow.editingDone, state.underConstruction.storageMap]);
};

/**
* Updates the destination name and namespace in the network map entries.
* If the destination name contains a '/', it splits the name into two parts.
* The first part becomes the new namespace, and the second part becomes the new name.
*
* @param {NetworkMap} networkMap - The network map object to update.
* @returns {NetworkMap} The updated network map object.
*/
export function updateNetworkMapDestination(networkMap: V1beta1NetworkMap): V1beta1NetworkMap {
const networkMapCopy = deepCopy(networkMap);

networkMapCopy.spec.map?.forEach((entry) => {
const parts = entry.destination.name.split('/');
if (parts.length === 2) {
entry.destination.namespace = parts[0];
entry.destination.name = parts[1];
}
});
return networkMapCopy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const MapsSection: React.FC<MapsSectionProps> = ({ obj }) => {
isDisabled={!state.hasChanges || state.updating}
icon={state.updating ? <Spinner size="sm" /> : undefined}
>
{t('Update providers')}
{t('Update mappings')}
</Button>
</FlexItem>

Expand Down

0 comments on commit ee0a85e

Please sign in to comment.