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

[WIP] CNV-23832: Apply StorageClass from Template's boot source #1532

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export const useVmTemplateSource = (template: V1Template): useVmTemplateSourceVa
setTemplateBootSource({
source: {
pvc: dataSource?.spec?.source?.pvc,
sourceRef: {
kind: dataSource?.kind,
name,
namespace,
},
},
storageClassName: pvc?.spec?.storageClassName,
type: BOOT_SOURCE.DATA_SOURCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { useEffect, useState } from 'react';
import { useHistory, useParams } from 'react-router-dom';
import produce from 'immer';

import {
applyCPUMemory,
applyTemplateMetadataToVM,
} from '@catalog/templatescatalog/components/TemplatesCatalogDrawer/utils/helpers';
import { isRHELTemplate } from '@catalog/utils/utils';
import { SecretModel, V1Template } from '@kubevirt-ui/kubevirt-api/console';
import VirtualMachineModel from '@kubevirt-ui/kubevirt-api/console/models/VirtualMachineModel';
Expand All @@ -14,16 +18,12 @@ import {
import { DEFAULT_NAMESPACE } from '@kubevirt-utils/constants/constants';
import { DataUpload, useCDIUpload } from '@kubevirt-utils/hooks/useCDIUpload/useCDIUpload';
import { getAnnotation } from '@kubevirt-utils/resources/shared';
import {
ANNOTATIONS,
LABEL_USED_TEMPLATE_NAME,
LABEL_USED_TEMPLATE_NAMESPACE,
} from '@kubevirt-utils/resources/template';
import { ANNOTATIONS } from '@kubevirt-utils/resources/template';
import {
getTemplateOS,
getTemplateVirtualMachineObject,
} from '@kubevirt-utils/resources/template/utils/selectors';
import { getMemoryCPU, getVolumes } from '@kubevirt-utils/resources/vm';
import { getVolumes } from '@kubevirt-utils/resources/vm';
import { ensurePath, isEmpty, kubevirtConsole } from '@kubevirt-utils/utils/utils';

import { useWizardVMContext } from '../../../utils/WizardVMContext';
Expand Down Expand Up @@ -88,17 +88,10 @@ export const useCustomizeFormSubmit = ({
const dataVolumeTemplate = vmObj?.spec?.dataVolumeTemplates?.[0];

const updatedVM = produce(vmObj, (vmDraft) => {
ensurePath(vmDraft, [
'spec.template.spec.domain.cpu',
'spec.template.spec.domain.memory.guest',
]);
applyCPUMemory(vmDraft, vm);
applyTemplateMetadataToVM(vmDraft, template);

vmDraft.metadata.namespace = ns || DEFAULT_NAMESPACE;
vmDraft.metadata.labels[LABEL_USED_TEMPLATE_NAME] = processedTemplate.metadata.name;
vmDraft.metadata.labels[LABEL_USED_TEMPLATE_NAMESPACE] = template.metadata.namespace;
const { cpu, memory } = getMemoryCPU(vm);
vmDraft.spec.template.spec.domain.cpu.cores = cpu?.cores;
vmDraft.spec.template.spec.domain.memory.guest = memory;

const updatedVolumes = applyCloudDriveCloudInitVolume(vmObj);
vmDraft.spec.template.spec.volumes = isRHELTemplate(processedTemplate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
V1Template,
} from '@kubevirt-ui/kubevirt-api/console';
import VirtualMachineModel from '@kubevirt-ui/kubevirt-api/console/models/VirtualMachineModel';
import { V1DataVolumeTemplateSpec } from '@kubevirt-ui/kubevirt-api/kubevirt';
import { updateCloudInitRHELSubscription } from '@kubevirt-utils/components/CloudinitModal/utils/cloudinit-utils';
import { isEqualObject } from '@kubevirt-utils/components/NodeSelectorModal/utils/helpers';
import {
addSecretToVM,
applyCloudDriveCloudInitVolume,
Expand All @@ -25,12 +27,10 @@ import {
ANNOTATIONS,
getTemplateOS,
getTemplateVirtualMachineObject,
LABEL_USED_TEMPLATE_NAME,
LABEL_USED_TEMPLATE_NAMESPACE,
} from '@kubevirt-utils/resources/template';
import { getMemoryCPU } from '@kubevirt-utils/resources/vm';
import { TemplateBootSource } from '@kubevirt-utils/resources/template/hooks/useVmTemplateSource/utils';
import { getDataVolumeTemplates } from '@kubevirt-utils/resources/vm';
import { ensurePath, isEmpty } from '@kubevirt-utils/utils/utils';
import { k8sCreate } from '@openshift-console/dynamic-plugin-sdk';
import { useAccessReview, useK8sModels } from '@openshift-console/dynamic-plugin-sdk';
import {
Alert,
Expand All @@ -50,6 +50,12 @@ import {
TextInput,
} from '@patternfly/react-core';

import {
applyCPUMemory,
applyTemplateMetadataToVM,
processTemplate,
shouldApplyInitialStorageClass,
} from './utils/helpers';
import AuthorizedSSHKey from './AuthorizedSSHKey';

type TemplatesCatalogDrawerCreateFormProps = {
Expand All @@ -61,6 +67,7 @@ type TemplatesCatalogDrawerCreateFormProps = {
onCancel: () => void;
subscriptionData: RHELAutomaticSubscriptionData;
template: V1Template;
templateBootSource: TemplateBootSource;
};

export const TemplatesCatalogDrawerCreateForm: FC<TemplatesCatalogDrawerCreateFormProps> = memo(
Expand All @@ -73,6 +80,7 @@ export const TemplatesCatalogDrawerCreateForm: FC<TemplatesCatalogDrawerCreateFo
onCancel,
subscriptionData,
template,
templateBootSource,
}) => {
const history = useHistory();
const { t } = useKubevirtTranslation();
Expand All @@ -90,84 +98,86 @@ export const TemplatesCatalogDrawerCreateForm: FC<TemplatesCatalogDrawerCreateFo
verb: 'create',
});

const onQuickCreate = () => {
const onQuickCreate = async () => {
avivtur marked this conversation as resolved.
Show resolved Hide resolved
setIsQuickCreating(true);
setQuickCreateError(undefined);

const nameParameterExists = isNameParameterExists(template);

const templateToProcess = produce(template, (draftTemplate) => {
if (nameParameterExists)
if (isNameParameterExists(template)) {
replaceTemplateParameterValue(draftTemplate, NAME_INPUT_FIELD, vmName);

const vmObject = getTemplateVirtualMachineObject(draftTemplate);
}

if (!isEmpty(authorizedSSHKey)) {
draftTemplate.objects = template.objects.filter((obj) => obj?.kind !== SecretModel.kind);
}
});

if (vm?.spec?.template) {
ensurePath(vmObject, [
'spec.template.spec.domain.cpu',
'spec.template.spec.domain.memory.guest',
]);
try {
const processedTemplate = await processTemplate(templateToProcess, namespace);

const { cpu, memory } = getMemoryCPU(vm);
vmObject.spec.template.spec.domain.cpu.cores = cpu?.cores;
vmObject.spec.template.spec.domain.memory.guest = memory;
const vmObject = getTemplateVirtualMachineObject(processedTemplate);

const modifiedTemplateObjects = template?.objects?.map((obj) =>
obj.kind === VirtualMachineModel.kind ? vmObject : obj,
);
const applyBootSourceStorageClass = await shouldApplyInitialStorageClass(vmObject);

draftTemplate.objects = modifiedTemplateObjects;
}
});
const overrideVM = produce(vmObject, (draftVM) => {
draftVM.metadata.namespace = namespace;

applyTemplateMetadataToVM(draftVM, template);
applyCPUMemory(draftVM, vm);

const updatedVolumes = applyCloudDriveCloudInitVolume(draftVM);
draftVM.spec.template.spec.volumes = isRHELTemplate(processedTemplate)
? updateCloudInitRHELSubscription(updatedVolumes, subscriptionData)
: updatedVolumes;

quickCreateVM({
models,
overrides: { authorizedSSHKey, name: vmName, namespace, startVM, subscriptionData },
template: templateToProcess,
})
.then((quickCreatedVM) => {
setIsQuickCreating(false);
history.push(getResourceUrl({ model: VirtualMachineModel, resource: quickCreatedVM }));
})
.catch((err) => {
setIsQuickCreating(false);
setQuickCreateError(err);
if (startVM) draftVM.spec.running = true;

if (applyBootSourceStorageClass) {
const dataVolumeTemplates: V1DataVolumeTemplateSpec[] = getDataVolumeTemplates(vm).map(
(dvt) => {
if (isEqualObject(dvt?.spec?.sourceRef, templateBootSource?.source?.sourceRef)) {
return produce(dvt, (draftDVT) => {
draftDVT.spec.storage.storageClassName = applyBootSourceStorageClass;
});
}

return dvt;
},
);

draftVM.spec.dataVolumeTemplates = dataVolumeTemplates;
}
});

const quickCreatedVM = await quickCreateVM({
models,
processedTemplate,
vm: !isEmpty(authorizedSSHKey) ? addSecretToVM(overrideVM, authorizedSSHKey) : overrideVM,
});

setIsQuickCreating(false);
history.push(getResourceUrl({ model: VirtualMachineModel, resource: quickCreatedVM }));
} catch (processError) {
setIsQuickCreating(false);
setQuickCreateError(processError);
}
};

const onCustomize = (e: MouseEvent) => {
e.preventDefault();

if (isEmpty(template?.parameters)) {
return k8sCreate<V1Template>({
data: { ...template, metadata: { ...template?.metadata, namespace } },
model: ProcessedTemplatesModel,
ns: namespace,
queryParams: {
dryRun: 'All',
},
}).then(async (processedTemplate) => {
return processTemplate(template, namespace).then(async (processedTemplate) => {
const vmObject = getTemplateVirtualMachineObject(processedTemplate);

const updatedVM = produce(vmObject, (vmDraft) => {
ensurePath(vmDraft, [
'spec.template.spec.domain.cpu',
'spec.template.spec.domain.memory.guest',
]);

vmDraft.metadata.namespace = namespace;
vmDraft.metadata.labels[LABEL_USED_TEMPLATE_NAME] = template.metadata.name;
vmDraft.metadata.labels[LABEL_USED_TEMPLATE_NAMESPACE] = template.metadata.namespace;
const { cpu, memory } = getMemoryCPU(vm);
vmDraft.spec.template.spec.domain.cpu.cores = cpu?.cores;
vmDraft.spec.template.spec.domain.memory.guest = memory;

const updatedVolumes = applyCloudDriveCloudInitVolume(vmObject);
vmDraft.spec.template.spec.volumes = isRHELTemplate(processedTemplate)
const updatedVM = produce(vmObject, (draftVM) => {
draftVM.metadata.namespace = namespace;

applyCPUMemory(draftVM, vm);
applyTemplateMetadataToVM(draftVM, template);

const updatedVolumes = applyCloudDriveCloudInitVolume(draftVM);
draftVM.spec.template.spec.volumes = isRHELTemplate(processedTemplate)
? updateCloudInitRHELSubscription(updatedVolumes, subscriptionData)
: updatedVolumes;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export const TemplatesCatalogDrawerFooter: FC<TemplateCatalogDrawerFooterProps>
template,
}) => {
const { t } = useKubevirtTranslation();
const { isBootSourceAvailable, loaded: bootSourceLoaded } = useVmTemplateSource(template);
const {
isBootSourceAvailable,
loaded: bootSourceLoaded,
templateBootSource,
} = useVmTemplateSource(template);

const [authorizedSSHKeys, updateAuthorizedSSHKeys, userSettingsLoaded] =
useKubevirtUserSettings('ssh');
const { loaded: loadedRHELSubscription, subscriptionData } = useRHELAutomaticSubscription();
Expand Down Expand Up @@ -109,6 +114,7 @@ export const TemplatesCatalogDrawerFooter: FC<TemplateCatalogDrawerFooterProps>
onCancel={onCancel}
subscriptionData={subscriptionData}
template={template}
templateBootSource={templateBootSource}
/>
</Stack>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
ProcessedTemplatesModel,
StorageClassModel,
V1Template,
} from '@kubevirt-ui/kubevirt-api/console';
import { IoK8sApiStorageV1StorageClassList } from '@kubevirt-ui/kubevirt-api/kubernetes';
import { V1VirtualMachine } from '@kubevirt-ui/kubevirt-api/kubevirt';
import { isDefaultStorageClass } from '@kubevirt-utils/components/DiskModal/DiskFormFields/utils/helpers';
import { getName, getNamespace } from '@kubevirt-utils/resources/shared';
import {
LABEL_USED_TEMPLATE_NAME,
LABEL_USED_TEMPLATE_NAMESPACE,
} from '@kubevirt-utils/resources/template';
import { getDataVolumeTemplates, getMemoryCPU } from '@kubevirt-utils/resources/vm';
import { ensurePath, isEmpty } from '@kubevirt-utils/utils/utils';
import { k8sCreate, k8sGet } from '@openshift-console/dynamic-plugin-sdk';

export const shouldApplyInitialStorageClass = async (vm: V1VirtualMachine): Promise<string> => {
const vmSCExists = getDataVolumeTemplates(vm)?.some(
(dvt) => !!dvt?.spec?.storage?.storageClassName,
);
if (vmSCExists) return null;

const storageClasses = await k8sGet<IoK8sApiStorageV1StorageClassList>({
model: StorageClassModel,
});

const defaultSC = storageClasses?.items?.find((item) => isDefaultStorageClass(item));

return isEmpty(defaultSC) ? getName(storageClasses?.items[0]) : null;
};

export const applyCPUMemory = (draftVM: V1VirtualMachine, contextVM: V1VirtualMachine) => {
ensurePath(draftVM, ['spec.template.spec.domain.cpu', 'spec.template.spec.domain.memory.guest']);

const { cpu, memory } = getMemoryCPU(contextVM);
draftVM.spec.template.spec.domain.cpu.cores = cpu?.cores;
draftVM.spec.template.spec.domain.memory.guest = memory;
};

export const applyTemplateMetadataToVM = (draftVM: V1VirtualMachine, template: V1Template) => {
draftVM.metadata.labels[LABEL_USED_TEMPLATE_NAME] = getName(template);
draftVM.metadata.labels[LABEL_USED_TEMPLATE_NAMESPACE] = getNamespace(template);
};

export const processTemplate = (template: V1Template, namespace: string) =>
k8sCreate<V1Template>({
data: { ...template, metadata: { ...template?.metadata, namespace } },
model: ProcessedTemplatesModel,
ns: namespace,
queryParams: {
dryRun: 'All',
},
});
Loading
Loading