Skip to content

Commit

Permalink
CNV-29457: Move cloud-init to v2
Browse files Browse the repository at this point in the history
Signed-off-by: Aviv Turgeman <[email protected]>
  • Loading branch information
avivtur committed Oct 5, 2023
1 parent cc1fa30 commit aae6a46
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/utils/components/CloudinitModal/CloudInitNetworkForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export const CloudinitNetworkForm: React.FC<CloudinitNetworkFormProps> = ({
>
<TextInput
id={'address'}
onChange={(v) => updateNetworkField('address', v)}
onChange={(v) => updateNetworkField('addresses', v)}
type="text"
value={networkData?.address || ''}
value={networkData?.addresses || ''}
/>
</FormGroup>
<FormGroup
Expand All @@ -69,9 +69,9 @@ export const CloudinitNetworkForm: React.FC<CloudinitNetworkFormProps> = ({
>
<TextInput
id={'gateway'}
onChange={(v) => updateNetworkField('gateway', v)}
onChange={(v) => updateNetworkField('gateway4', v)}
type="text"
value={networkData?.gateway || ''}
value={networkData?.gateway4 || ''}
/>
</FormGroup>
</>
Expand Down
62 changes: 27 additions & 35 deletions src/utils/components/CloudinitModal/utils/cloudinit-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,22 @@ export const convertUserDataObjectToYAML = (
export const convertYAMLToNetworkDataObject = (networkData: string): CloudInitNetworkData => {
try {
const networkObj = load(networkData) as CloudInitNetwork;
const networkToEdit = networkObj?.network?.config?.[0];

const name = Object.keys(networkObj?.ethernets)?.[0];

const ips =
!isEmpty(networkToEdit?.subnets?.[0]?.address) && networkToEdit?.subnets?.[0]?.address;
const address = Array.isArray(ips) ? ips?.join(',') : ips;
const gateway = networkToEdit?.subnets?.[0]?.gateway;
const name = networkToEdit?.name;
!isEmpty(networkObj?.ethernets?.[name]?.addresses) &&
networkObj?.ethernets?.[name]?.addresses;

const addresses = Array.isArray(ips) ? ips?.join(',') : ips;
const gateway4 = networkObj?.ethernets?.[name]?.gateway4;

const nonEmptyNetworkObj = !!address || !!gateway || !!name;
const nonEmptyNetworkObj = !!addresses || !!name || !!gateway4;

return (
nonEmptyNetworkObj && {
address,
gateway,
addresses,
gateway4,
name,
}
);
Expand All @@ -75,27 +77,19 @@ export const convertYAMLToNetworkDataObject = (networkData: string): CloudInitNe
};

export const convertNetworkDataObjectToYAML = (networkData: CloudInitNetworkData): string => {
const hasValue = !isEmpty(networkData?.name || networkData?.address || networkData?.gateway);
const { addresses, gateway4, name } = networkData || {};
const hasValue = !isEmpty(name) || !isEmpty(addresses) || !isEmpty(gateway4);
try {
return hasValue
? dump({
network: {
config: [
{
name: networkData?.name,
subnets: [
{
address: (networkData?.address || '')?.replace(/\s/g, '').split(','),
gateway: networkData?.gateway,
type: 'static',
},
],
type: 'physical',
},
],
version: '1',
ethernets: {
[name || '']: {
addresses: (addresses || '')?.replace(/\s/g, '').split(','),
...(gateway4 && { gateway4 }),
},
},
})
version: 2,
} as CloudInitNetwork)
: null;
} catch (e) {
kubevirtConsole.error(e);
Expand Down Expand Up @@ -164,19 +158,17 @@ export type CloudInitUserData = {
};

export type CloudInitNetworkData = {
address: string;
gateway: string;
addresses: string;
gateway4: string;
name: string;
};

type CloudInitNetwork = {
network: {
config: {
mac_address?: string;
name: string;
subnets: { address: string[]; gateway: string; type: string }[];
type: string;
}[];
version: string;
ethernets: {
[name: string]: {
addresses: string[];
gateway4: string;
};
};
version: number;
};

0 comments on commit aae6a46

Please sign in to comment.