From 1972c8a549d2321d23a20a4eb1280669bdadc6bf Mon Sep 17 00:00:00 2001 From: yaacov Date: Mon, 27 May 2024 11:23:20 +0300 Subject: [PATCH] Save network map target namespace Signed-off-by: yaacov --- .../components/MapsSection/MapsSection.tsx | 8 +++-- .../ProvidersSection/ProvidersSection.tsx | 6 +++- .../details/utils/mapMappingsIdsToLabels.tsx | 2 +- .../details/utils/patchPlanMappingsData.ts | 23 ++++++++++++++- .../migrate/reducer/calculateMappings.ts | 2 +- .../Providers/views/migrate/useSaveEffect.ts | 29 +++++++++++++++++-- .../components/MapsSection/MapsSection.tsx | 2 +- 7 files changed, 62 insertions(+), 10 deletions(-) diff --git a/packages/forklift-console-plugin/src/modules/NetworkMaps/views/details/components/MapsSection/MapsSection.tsx b/packages/forklift-console-plugin/src/modules/NetworkMaps/views/details/components/MapsSection/MapsSection.tsx index f41cf3ec9..69cf80b6d 100644 --- a/packages/forklift-console-plugin/src/modules/NetworkMaps/views/details/components/MapsSection/MapsSection.tsx +++ b/packages/forklift-console-plugin/src/modules/NetworkMaps/views/details/components/MapsSection/MapsSection.tsx @@ -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 { @@ -64,7 +65,10 @@ export const MapsSection: React.FC = ({ 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 }); }; @@ -162,7 +166,7 @@ export const MapsSection: React.FC = ({ obj }) => { isDisabled={!state.hasChanges || state.updating} icon={state.updating ? : undefined} > - {t('Update providers')} + {t('Update mappings')} diff --git a/packages/forklift-console-plugin/src/modules/NetworkMaps/views/details/components/ProvidersSection/ProvidersSection.tsx b/packages/forklift-console-plugin/src/modules/NetworkMaps/views/details/components/ProvidersSection/ProvidersSection.tsx index 668018fb4..0d7530bb7 100644 --- a/packages/forklift-console-plugin/src/modules/NetworkMaps/views/details/components/ProvidersSection/ProvidersSection.tsx +++ b/packages/forklift-console-plugin/src/modules/NetworkMaps/views/details/components/ProvidersSection/ProvidersSection.tsx @@ -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 { @@ -42,7 +43,10 @@ export const ProvidersSection: React.FC = ({ 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 ( diff --git a/packages/forklift-console-plugin/src/modules/Plans/views/details/utils/mapMappingsIdsToLabels.tsx b/packages/forklift-console-plugin/src/modules/Plans/views/details/utils/mapMappingsIdsToLabels.tsx index 86d2de8b7..b08640057 100644 --- a/packages/forklift-console-plugin/src/modules/Plans/views/details/utils/mapMappingsIdsToLabels.tsx +++ b/packages/forklift-console-plugin/src/modules/Plans/views/details/utils/mapMappingsIdsToLabels.tsx @@ -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); diff --git a/packages/forklift-console-plugin/src/modules/Plans/views/details/utils/patchPlanMappingsData.ts b/packages/forklift-console-plugin/src/modules/Plans/views/details/utils/patchPlanMappingsData.ts index a3aeecb6d..eea617c13 100644 --- a/packages/forklift-console-plugin/src/modules/Plans/views/details/utils/patchPlanMappingsData.ts +++ b/packages/forklift-console-plugin/src/modules/Plans/views/details/utils/patchPlanMappingsData.ts @@ -30,7 +30,7 @@ export async function patchPlanMappingsData( { op: 'replace', path: '/spec/map', - value: updatedNetwork, + value: updateNetworkMapSpecMapDestination(updatedNetwork), }, ], }); @@ -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; +} diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/migrate/reducer/calculateMappings.ts b/packages/forklift-console-plugin/src/modules/Providers/views/migrate/reducer/calculateMappings.ts index 5d986d6b6..6295bb2e7 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/migrate/reducer/calculateMappings.ts +++ b/packages/forklift-console-plugin/src/modules/Providers/views/migrate/reducer/calculateMappings.ts @@ -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, diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/migrate/useSaveEffect.ts b/packages/forklift-console-plugin/src/modules/Providers/views/migrate/useSaveEffect.ts index a155bb575..8d17aae3c 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/migrate/useSaveEffect.ts +++ b/packages/forklift-console-plugin/src/modules/Providers/views/migrate/useSaveEffect.ts @@ -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, @@ -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, @@ -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; +} diff --git a/packages/forklift-console-plugin/src/modules/StorageMaps/views/details/components/MapsSection/MapsSection.tsx b/packages/forklift-console-plugin/src/modules/StorageMaps/views/details/components/MapsSection/MapsSection.tsx index 16c676d92..40e446db8 100644 --- a/packages/forklift-console-plugin/src/modules/StorageMaps/views/details/components/MapsSection/MapsSection.tsx +++ b/packages/forklift-console-plugin/src/modules/StorageMaps/views/details/components/MapsSection/MapsSection.tsx @@ -155,7 +155,7 @@ export const MapsSection: React.FC = ({ obj }) => { isDisabled={!state.hasChanges || state.updating} icon={state.updating ? : undefined} > - {t('Update providers')} + {t('Update mappings')}