Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CNV-41586: Missing functions for sysprep customizr IT flow #1997

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/utils/store/customizeInstanceType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { effect, signal } from '@preact/signals-react';

import {
getCustomizeInstanceTypeSessionStorage,
mergeData,
saveCustomizeInstanceTypeSessionStorage,
} from './customizeInstanceType/utils/utils';

Expand All @@ -26,7 +27,9 @@ type UpdateCustomizeInstanceTypeArgs = {
path?: string;
}[];

type UpdateCustomizeInstanceType = (args: UpdateCustomizeInstanceTypeArgs) => V1VirtualMachine;
export type UpdateCustomizeInstanceType = (
args: UpdateCustomizeInstanceTypeArgs,
) => V1VirtualMachine;

export const clearCustomizeInstanceType = () => {
vmSignal.value = null;
Expand Down Expand Up @@ -55,7 +58,7 @@ export const updateCustomizeInstanceType: UpdateCustomizeInstanceType = (
return;
}

obj[part] = merge ? { ...obj[part], ...data } : data;
obj[part] = merge ? mergeData(obj[part], data) : data;
});
});
});
Expand Down
6 changes: 6 additions & 0 deletions src/utils/store/customizeInstanceType/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ export const getCustomizeInstanceTypeSessionStorage = (): V1VirtualMachine => {
}
return null;
};

export const mergeData = (seedData, appendData) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add types here?

return Array.isArray(seedData) || Array.isArray(appendData)
? [...(seedData || []), ...(appendData || [])]
: { ...(seedData || {}), ...(appendData || {}) };
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import React from 'react';
import Loading from '@kubevirt-utils/components/Loading/Loading';
import SearchItem from '@kubevirt-utils/components/SearchItem/SearchItem';
import { useKubevirtTranslation } from '@kubevirt-utils/hooks/useKubevirtTranslation';
import { updateVMCustomizeIT, vmSignal } from '@kubevirt-utils/store/customizeInstanceType';
import {
updateCustomizeInstanceType,
updateVMCustomizeIT,
vmSignal,
} from '@kubevirt-utils/store/customizeInstanceType';
import {
DescriptionList,
Divider,
Expand All @@ -30,7 +34,7 @@ const CustomizeInstanceTypeInitialRunTab = () => {
<DescriptionList className="pf-c-description-list">
<InitialRunTabCloudinit canUpdateVM onSubmit={updateVMCustomizeIT} vm={vm} />
<Divider />
<InitialRunTabSysprep canUpdateVM vm={vm} />
<InitialRunTabSysprep canUpdateVM onSubmit={updateCustomizeInstanceType} vm={vm} />
</DescriptionList>
</PageSection>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ import { SysprepModal } from '@kubevirt-utils/components/SysprepModal/SysprepMod
import VirtualMachineDescriptionItem from '@kubevirt-utils/components/VirtualMachineDescriptionItem/VirtualMachineDescriptionItem';
import { t } from '@kubevirt-utils/hooks/useKubevirtTranslation';
import { getVolumes } from '@kubevirt-utils/resources/vm';
import { UpdateCustomizeInstanceType } from '@kubevirt-utils/store/customizeInstanceType';
import { isEmpty } from '@kubevirt-utils/utils/utils';
import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk';

import { createSysprepConfigMap, patchVMWithExistingSysprepConfigMap } from '../utils/utils';

type InitialRunTabSysprepProps = {
canUpdateVM: boolean;
onSubmit?: UpdateCustomizeInstanceType;
vm: V1VirtualMachine;
};
const InitialRunTabSysprep: FC<InitialRunTabSysprepProps> = ({ canUpdateVM, vm }) => {
const InitialRunTabSysprep: FC<InitialRunTabSysprepProps> = ({ canUpdateVM, onSubmit, vm }) => {
const { createModal } = useModal();
const vmVolumes = getVolumes(vm);

Expand All @@ -44,10 +46,11 @@ const InitialRunTabSysprep: FC<InitialRunTabSysprepProps> = ({ canUpdateVM, vm }

const { [AUTOUNATTEND]: autoUnattend, [UNATTEND]: unattend } = externalSysprepConfig?.data || {};

const onSysprepSelected = (name: string) => patchVMWithExistingSysprepConfigMap(name, vm);
const onSysprepSelected = (name: string) =>
patchVMWithExistingSysprepConfigMap(name, vm, onSubmit);

const onSysprepCreation = async (unattended: string, autounattend: string): Promise<void> =>
createSysprepConfigMap(unattended, autounattend, externalSysprepConfig, vm);
createSysprepConfigMap(unattended, autounattend, externalSysprepConfig, vm, onSubmit);

return (
<VirtualMachineDescriptionItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,65 @@ import {
UNATTEND,
} from '@kubevirt-utils/components/SysprepModal/sysprep-utils';
import { getDisks, getVolumes } from '@kubevirt-utils/resources/vm';
import { UpdateCustomizeInstanceType } from '@kubevirt-utils/store/customizeInstanceType';
import { isEmpty } from '@kubevirt-utils/utils/utils';
import { k8sCreate, k8sPatch } from '@openshift-console/dynamic-plugin-sdk';

export const patchVMWithExistingSysprepConfigMap = async (
name: string,
vm: V1VirtualMachine,
onSubmit?: UpdateCustomizeInstanceType,
): Promise<void> => {
const vmVolumes = getVolumes(vm);
const vmDisks = getDisks(vm);

await k8sPatch<V1VirtualMachine>({
data: [
{
op: 'replace',
path: `/spec/template/spec/domain/devices/disks`,
value: [
...vmDisks.filter((disk) => disk?.name !== SYSPREP),
...(!isEmpty(name) ? [sysprepDisk()] : []),
],
},
{
op: 'replace',
path: `/spec/template/spec/volumes`,
value: [
...vmVolumes.filter((vol) => vol?.name !== SYSPREP),
...(!isEmpty(name) ? [sysprepVolume(name)] : []),
onSubmit
? onSubmit([
{
data: [
...(vmDisks || []).filter((disk) => disk?.name !== SYSPREP),
...(!isEmpty(name) ? [sysprepDisk()] : []),
],
path: `spec.template.spec.domain.devices.disks`,
},
{
data: [
...(vmVolumes || []).filter((vol) => vol?.name !== SYSPREP),
...(!isEmpty(name) ? [sysprepVolume(name)] : []),
],
path: `spec.template.spec.volumes`,
},
])
: await k8sPatch<V1VirtualMachine>({
data: [
{
op: 'replace',
path: `/spec/template/spec/domain/devices/disks`,
value: [
...vmDisks.filter((disk) => disk?.name !== SYSPREP),
...(!isEmpty(name) ? [sysprepDisk()] : []),
],
},
{
op: 'replace',
path: `/spec/template/spec/volumes`,
value: [
...vmVolumes.filter((vol) => vol?.name !== SYSPREP),
...(!isEmpty(name) ? [sysprepVolume(name)] : []),
],
},
],
},
],
model: VirtualMachineModel,
resource: vm,
});
model: VirtualMachineModel,
resource: vm,
});
};

export const createSysprepConfigMap = async (
unattended: string,
autounattend: string,
externalSysprepConfig: IoK8sApiCoreV1ConfigMap,
vm: V1VirtualMachine,
onSubmit?: UpdateCustomizeInstanceType,
): Promise<void> => {
const vmVolumes = getVolumes(vm);
const vmDisks = getDisks(vm);
Expand Down Expand Up @@ -78,20 +98,33 @@ export const createSysprepConfigMap = async (
}

await k8sCreate({ data: configMap, model: ConfigMapModel });
await k8sPatch<V1VirtualMachine>({
data: [
{
op: 'replace',
path: `/spec/template/spec/domain/devices/disks`,
value: [...vmDisks, sysprepDisk()],
},
{
op: 'replace',
path: `/spec/template/spec/volumes`,
value: [...vmVolumes, sysprepVolume(configMap.metadata.name)],
},
],
model: VirtualMachineModel,
resource: vm,
});
onSubmit
? onSubmit([
{
data: [sysprepDisk()],
merge: true,
path: 'spec.template.spec.domain.devices.disks',
},
{
data: [sysprepVolume(configMap.metadata.name)],
merge: true,
path: `spec.template.spec.volumes`,
},
])
: await k8sPatch<V1VirtualMachine>({
data: [
{
op: 'replace',
path: `/spec/template/spec/domain/devices/disks`,
value: [...vmDisks, sysprepDisk()],
},
{
op: 'replace',
path: `/spec/template/spec/volumes`,
value: [...vmVolumes, sysprepVolume(configMap.metadata.name)],
},
],
model: VirtualMachineModel,
resource: vm,
});
};
Loading