Skip to content

Commit

Permalink
Added support for dataVolumeTemplate in yaml
Browse files Browse the repository at this point in the history
Signed-off-by: Matan Schatzman <mschatzm@redhat.com>

Signed-off-by: Matan Schatzman <mschatzm@redhat.com>
  • Loading branch information
metalice committed Dec 24, 2020
1 parent edafa4d commit 3ae8432
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { iGetCommonData, iGetLoadedCommonData } from '../../selectors/immutable/
import {
iGetCommonTemplateCloudInit,
iGetRelevantTemplate,
iGetCommonTemplateDiskBus,
} from '../../../../selectors/immutable/template/combined';
import { CLOUDINIT_DISK, DiskType, DiskBus, VolumeType } from '../../../../constants/vm';
import { vmWizardInternalActions } from '../internal-actions';
Expand Down Expand Up @@ -76,7 +77,11 @@ export const commonTemplatesUpdater = ({ id, prevState, dispatch, getState }: Up
.init({
name: CLOUDINIT_DISK,
})
.setType(DiskType.DISK, { bus: DiskBus.VIRTIO })
.setType(DiskType.DISK, {
bus:
DiskBus.fromString(iGetCommonTemplateDiskBus(iTemplate, 'cloudinitdisk')) ||
DiskBus.VIRTIO,
})
.asResource(),
volume: new VolumeWrapper()
.init({ name: CLOUDINIT_DISK })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
hasStepUpdateDisabled,
isStepLocked,
} from '../../selectors/immutable/wizard-selectors';
import { VMWizardProps, VMWizardStorage, VMWizardTab } from '../../types';
import { VMWizardProps, VMWizardStorage, VMWizardStorageType, VMWizardTab } from '../../types';
import { VMDisksTable } from '../../../vm-disks/vm-disks';
import { vmWizardActions } from '../../redux/actions';
import { ActionType } from '../../redux/types';
Expand Down Expand Up @@ -89,6 +89,16 @@ const StorageTabFirehose: React.FC<StorageTabFirehoseProps> = ({
isDeleteDisabled,
}) => {
const { t } = useTranslation();

React.useEffect(() => {
storages.forEach(({ type, id, disk }) => {
const isTemplateType = type === VMWizardStorageType.TEMPLATE;
// eslint-disable-next-line no-template-curly-in-string
const isMatchedDiskName = disk.name === '${NAME}';
isTemplateType && isMatchedDiskName && removeStorage(id);
});
}, [removeStorage, storages]);

const showStorages = storages.length > 0 || isBootDiskRequired;

const withProgress = wrapWithProgress(setTabLocked);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import { windowsToolsStorage } from '../../../../components/create-vm-wizard/red
import { getEmptyInstallStorage } from '../../../../utils/storage';
import { ignoreCaseSort } from '../../../../utils/sort';
import { ProvisionSource } from '../../../../constants/vm/provision-source';
import { getParameterValue } from '../../../../selectors/selectors';
import {
TEMPLATE_BASE_IMAGE_NAMESPACE_PARAMETER,
TEMPLATE_BASE_IMAGE_NAME_PARAMETER,
} from '../../../../constants/vm';

type GetRootDataVolume = (args: {
name: string;
Expand Down Expand Up @@ -113,6 +118,8 @@ export const createVM = async (
),
);
const processedTemplate = await k8sCreate(ProcessedTemplatesModel, templateWrapper.asResource());
const nameParam = getParameterValue(template, TEMPLATE_BASE_IMAGE_NAME_PARAMETER);
const namespaceParam = getParameterValue(template, TEMPLATE_BASE_IMAGE_NAMESPACE_PARAMETER);
const vmWrapper = new VMWrapper(selectVM(processedTemplate))
.setNamespace(namespace)
.setHostname(name);
Expand All @@ -127,10 +134,17 @@ export const createVM = async (
});

const rootDisk = new DiskWrapper(
vmWrapper.getDisks().find((d) => d.name === ROOT_DISK_NAME),
vmWrapper.getDisks().find((d) => d.name === ROOT_DISK_NAME) || {
name: ROOT_DISK_NAME,
disk: {
bus: vmWrapper.getDiskByPVC(nameParam) || DiskBus.VIRTIO.getValue(),
},
},
true,
).setBootOrder(1);

vmWrapper.removeDiskByPVC(nameParam, namespaceParam);

let rootDataVolume;
let isCDRom: boolean;

Expand Down
38 changes: 38 additions & 0 deletions frontend/packages/kubevirt-plugin/src/k8s/wrapper/vm/vm-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ export class VMWrapper extends K8sResourceWrapper<VMKind, VMWrapper> implements
return this;
};

getDiskByPVC = (PVCName: string): string => {
const dataVolumeByPVCName = this.getDataVolumeTemplates().find(
(dataVolumeTemplate) => dataVolumeTemplate?.spec?.source?.pvc?.name === PVCName,
);

const diskByPVCName = this.getDisks().find(
(d) => d.name === dataVolumeByPVCName?.metadata?.name,
);

return diskByPVCName?.disk?.bus;
};

addTemplateAnnotation = (key: string, value: string) => {
if (key) {
this.ensurePath('spec.template.metadata.annotations');
Expand Down Expand Up @@ -213,6 +225,32 @@ export class VMWrapper extends K8sResourceWrapper<VMKind, VMWrapper> implements
return this;
};

removeDiskByPVC = (nameParam: string, namespaceParam: string) => {
this.ensurePath('spec.template.spec.domain.devices', {});
const metaDataNames: string[] = [];

this.data.spec.dataVolumeTemplates = this.data.spec.dataVolumeTemplates.filter((dataVolume) => {
const { name, namespace } = dataVolume?.spec?.source?.pvc;
if (name === nameParam && namespace === namespaceParam) {
metaDataNames.push(dataVolume?.metadata?.name);
return false;
}
return true;
});

this.data.spec.template.spec.volumes = this.data.spec.template.spec.volumes.filter((volume) => {
return !metaDataNames.find((metaDataName) => volume.name === metaDataName);
});

this.data.spec.template.spec.domain.devices.disks = this.data.spec.template.spec.domain.devices.disks.filter(
(disk) => !disk.name || !metaDataNames.find((metaDataName) => disk.name === metaDataName),
);

this.ensureStorageConsistency();

return this;
};

updateVolume = (volume: V1Volume) => {
this.data.spec.template.spec.volumes = this.getVolumes().map((vol) => {
if (volume.name === vol.name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ export const iGetCommonTemplateCloudInit = (tmp: ITemplate) => {
return iGet(iCloudInitStorage, 'cloudInitNoCloud');
};

export const iGetCommonTemplateDiskBus = (tmp: ITemplate, diskName: string) => {
const cloudDisk = iGetIn(iSelectVM(tmp), [
'spec',
'template',
'spec',
'domain',
'devices',
'disks',
])?.find((disk) => iGetIn(disk, ['name']) === diskName);

return iGetIn(cloudDisk, ['disk', 'bus']);
};

export const iGetDefaultTemplate = (
iCommonTemplates: ImmutableMap<string, ITemplate>,
os: string,
Expand Down

0 comments on commit 3ae8432

Please sign in to comment.