Skip to content

Commit

Permalink
Allow to edit pod network as source
Browse files Browse the repository at this point in the history
Signed-off-by: yzamir <[email protected]>
  • Loading branch information
yaacov committed Apr 7, 2024
1 parent bd8289e commit 2c8892f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export const MapsSection: React.FC<MapsSectionProps> = ({ 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,
Expand All @@ -112,7 +114,8 @@ export const MapsSection: React.FC<MapsSectionProps> = ({ 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
Expand All @@ -129,15 +132,18 @@ export const MapsSection: React.FC<MapsSectionProps> = ({ 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',
payload: [
...(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'])
),
Expand Down Expand Up @@ -186,7 +192,8 @@ export const MapsSection: React.FC<MapsSectionProps> = ({ 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 ')}
Expand Down Expand Up @@ -227,6 +234,10 @@ function convertInventoryNetworkToV1beta1NetworkMapSpecMapSource(
return undefined;
}

if (inventoryNetwork?.id === 'pod') {
return { type: 'pod' };
}

return {
id: inventoryNetwork?.id,
name: inventoryNetwork['name'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,11 @@ export const PlanMappingsSection: React.FC<PlanMappingsSectionProps> = ({
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);
Expand Down Expand Up @@ -258,7 +259,8 @@ export const PlanMappingsSection: React.FC<PlanMappingsSectionProps> = ({
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,
Expand Down Expand Up @@ -407,11 +409,14 @@ export const PlanMappingsSection: React.FC<PlanMappingsSectionProps> = ({
: 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,
},
};
};

Expand All @@ -434,7 +439,7 @@ export const PlanMappingsSection: React.FC<PlanMappingsSectionProps> = ({
};

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 ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down

0 comments on commit 2c8892f

Please sign in to comment.