From 2c8892fff3ac0c0ec54f0bf65260a7b774d6f02e Mon Sep 17 00:00:00 2001 From: yzamir Date: Sun, 7 Apr 2024 16:40:34 +0300 Subject: [PATCH] Allow to edit pod network as source Signed-off-by: yzamir --- .../components/MapsSection/MapsSection.tsx | 21 ++++++++++---- .../tabs/Mappings/PlanMappingsSection.tsx | 23 +++++++++------ .../modules/Providers/hooks/useNetworks.ts | 29 ++++++++++++++----- .../views/migrate/reducer/reducer.ts | 9 ++++-- 4 files changed, 58 insertions(+), 24 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 1c05f6b1e..f41cf3ec9 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 @@ -91,7 +91,9 @@ export const MapsSection: React.FC = ({ obj }) => { const currentDestinationNet = destinationNetworks.find( (n) => OpenShiftNetworkAttachmentDefinitionToName(n) == current.destination, ); - const currentSourceNet = sourceNetworks.find((n) => n?.name === current.source); + const currentSourceNet = sourceNetworks.find((n) => n?.name === current.source) || { + id: 'pod', + }; const nextDestinationNet = destinationNetworks.find( (n) => OpenShiftNetworkAttachmentDefinitionToName(n) == next.destination, @@ -112,7 +114,8 @@ export const MapsSection: React.FC = ({ obj }) => { }; const payload = state?.networkMap?.spec?.map?.map((map) => { - return map?.source?.id === currentSourceNet?.id && + return (map?.source?.id === currentSourceNet?.id || + map.source?.type === currentSourceNet?.id) && (map.destination?.name === currentDestinationNet?.['name'] || map.destination?.type === currentDestinationNet?.['type']) ? nextMap @@ -129,7 +132,9 @@ export const MapsSection: React.FC = ({ obj }) => { const currentDestinationNet = destinationNetworks.find( (n) => OpenShiftNetworkAttachmentDefinitionToName(n) == current.destination, ) || { type: 'pod' }; - const currentSourceNet = sourceNetworks.find((n) => n?.name === current.source); + const currentSourceNet = sourceNetworks.find((n) => n?.name === current.source) || { + id: 'pod', + }; dispatch({ type: 'SET_MAP', @@ -137,7 +142,8 @@ export const MapsSection: React.FC = ({ obj }) => { ...(state?.networkMap?.spec?.map.filter( (map) => !( - map?.source?.id === currentSourceNet?.id && + (map?.source?.id === currentSourceNet?.id || + map.source?.type === currentSourceNet?.id) && (map.destination?.name === currentDestinationNet['name'] || map.destination?.type === currentDestinationNet['type']) ), @@ -186,7 +192,8 @@ export const MapsSection: React.FC = ({ obj }) => { isMapped: isNetMapped(n?.id), }))} mappings={state?.networkMap?.spec?.map.map((m) => ({ - source: getSourceNetName(sourceNetworks, m.source), + source: + m.source?.type === 'pod' ? 'Pod network' : getSourceNetName(sourceNetworks, m.source), destination: getDestinationNetName(destinationNetworks, m.destination), }))} generalSourcesLabel={t('Other networks present on the source provider ')} @@ -227,6 +234,10 @@ function convertInventoryNetworkToV1beta1NetworkMapSpecMapSource( return undefined; } + if (inventoryNetwork?.id === 'pod') { + return { type: 'pod' }; + } + return { id: inventoryNetwork?.id, name: inventoryNetwork['name'], diff --git a/packages/forklift-console-plugin/src/modules/Plans/views/details/tabs/Mappings/PlanMappingsSection.tsx b/packages/forklift-console-plugin/src/modules/Plans/views/details/tabs/Mappings/PlanMappingsSection.tsx index 98c8c4739..66e001cea 100644 --- a/packages/forklift-console-plugin/src/modules/Plans/views/details/tabs/Mappings/PlanMappingsSection.tsx +++ b/packages/forklift-console-plugin/src/modules/Plans/views/details/tabs/Mappings/PlanMappingsSection.tsx @@ -224,10 +224,11 @@ export const PlanMappingsSection: React.FC = ({ const onDeleteNetworkMapping = ({ source, destination }: Mapping) => { const newState = state.updatedNetwork.filter( (obj) => - mapSourceNetworksIdsToLabels(sourceNetworks)[obj.source.id] != source || + (mapSourceNetworksIdsToLabels(sourceNetworks)[obj.source.id] !== source && + !(obj.source.type === 'pod' && source.includes('Pod'))) || (mapTargetNetworksIdsToLabels(targetNetworks, plan)[obj.destination.type] ?? obj.destination?.name ?? - 'Not available') != destination, + 'Not available') !== destination, ); setIsAddNetworkMapAvailable(true); @@ -258,7 +259,8 @@ export const PlanMappingsSection: React.FC = ({ const onReplaceNetworkMapping = ({ current, next }) => { const replacedIndex = state.updatedNetwork.findIndex( (obj) => - mapSourceNetworksIdsToLabels(sourceNetworks)[obj.source.id] == current.source && + (mapSourceNetworksIdsToLabels(sourceNetworks)[obj.source.id] == current.source || + (obj.source.type === 'pod' && current.source.includes('Pod'))) && (mapTargetNetworksIdsToLabels(targetNetworks, plan)[obj.destination.type] ?? obj.destination?.name ?? 'Not available') == current.destination, @@ -407,11 +409,14 @@ export const PlanMappingsSection: React.FC = ({ : targetNetworks[nextTargetIndex].namespace, type: nextTargetIndex < 0 && targetName === POD_NETWORK ? 'pod' : 'multus', }, - source: { - id: sourceNetworks[nextSourceIndex].id, - name: sourceNetworks[nextSourceIndex].name, - type: sourceNetworks[nextSourceIndex].providerType, - }, + source: + sourceNetworks[nextSourceIndex].id === 'pod' + ? { type: 'pod' } + : { + id: sourceNetworks[nextSourceIndex].id, + name: sourceNetworks[nextSourceIndex].name, + type: sourceNetworks[nextSourceIndex].providerType, + }, }; }; @@ -434,7 +439,7 @@ export const PlanMappingsSection: React.FC = ({ }; const labeledSelectedNetworkMaps: Mapping[] = state.updatedNetwork?.map((obj) => ({ - source: mapSourceNetworksIdsToLabels(sourceNetworks)[obj.source.id], + source: mapSourceNetworksIdsToLabels(sourceNetworks)[obj.source.id || obj.source?.type], destination: mapTargetNetworksIdsToLabels(targetNetworks, plan)[obj.destination.type] ?? obj.destination?.name ?? diff --git a/packages/forklift-console-plugin/src/modules/Providers/hooks/useNetworks.ts b/packages/forklift-console-plugin/src/modules/Providers/hooks/useNetworks.ts index 815449c46..f82e8c4de 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/hooks/useNetworks.ts +++ b/packages/forklift-console-plugin/src/modules/Providers/hooks/useNetworks.ts @@ -12,6 +12,17 @@ import { import useProviderInventory from './useProviderInventory'; +const podNetwork: InventoryNetwork = { + providerType: 'openshift', + object: undefined, + uid: 'pod', + version: '', + namespace: '', + name: 'Pod network', + selfLink: '', + id: 'pod', +}; + export type InventoryNetwork = | OpenShiftNetworkAttachmentDefinition | OpenstackNetwork @@ -33,13 +44,17 @@ export const useSourceNetworks = ( disabled: !provider, }); - const typedNetworks = useMemo( - () => - Array.isArray(networks) - ? networks.map((net) => ({ ...net, providerType } as InventoryNetwork)) - : [], - [networks], - ); + const typedNetworks = useMemo(() => { + const networksList = Array.isArray(networks) + ? networks.map((net) => ({ ...net, providerType } as InventoryNetwork)) + : []; + + if (Array.isArray(networks) && provider?.spec?.type === 'openshift') { + networksList.push(podNetwork); + } + + return networksList; + }, [networks]); return [typedNetworks, loading, error]; }; diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/migrate/reducer/reducer.ts b/packages/forklift-console-plugin/src/modules/Providers/views/migrate/reducer/reducer.ts index 720904c7b..4559669c3 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/migrate/reducer/reducer.ts +++ b/packages/forklift-console-plugin/src/modules/Providers/views/migrate/reducer/reducer.ts @@ -337,9 +337,12 @@ const handlers: { // triggered by the user flow.editingDone = true; netMap.spec.map = networkMappings.map(({ source, destination }) => ({ - source: { - id: sourceNetworkLabelToId[source], - }, + source: + sourceNetworkLabelToId[source] === 'pod' + ? { type: 'pod' } + : { + id: sourceNetworkLabelToId[source], + }, destination: destination === POD_NETWORK ? { type: 'pod' }