Skip to content

Commit

Permalink
Bug 1788810: deprecate usage of flavor.template.kubevirt.io/Custom label
Browse files Browse the repository at this point in the history
- use Custom only internally by the UI and acknowledge missing label as Custom flavor
- remove kubevirt.io/size: Custom label as well
- capitalize flavors in VM Wizard
- fix display of VM template flavor
  • Loading branch information
suomiy committed May 4, 2020
1 parent 6ae5639 commit 3ee4d44
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CommonData, VMSettingsField, VMWizardProps } from '../../types';
import { asDisabled, asHidden, asRequired } from '../../utils/utils';
import { ProvisionSource } from '../../../../constants/vm/provision-source';
import { InitialStepStateGetter, VMSettings } from './types';
import { CUSTOM_FLAVOR } from '../../../../constants/vm';

export const getInitialVmSettings = (data: CommonData): VMSettings => {
const {
Expand Down Expand Up @@ -53,6 +54,7 @@ export const getInitialVmSettings = (data: CommonData): VMSettings => {
},
[VMSettingsField.FLAVOR]: {
isRequired: asRequired(true),
value: CUSTOM_FLAVOR,
},
[VMSettingsField.MEMORY]: {
binaryUnitValidation: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { iGetNetworks } from '../../selectors/immutable/networks';
import { podNetwork } from '../initial-state/networks-tab-initial-state';
import { vmWizardInternalActions } from '../internal-actions';
import {
CUSTOM_FLAVOR,
DataVolumeSourceType,
DiskBus,
DiskType,
Expand Down Expand Up @@ -63,6 +62,7 @@ import { joinIDs } from '../../../../utils';
import { VM_TEMPLATE_NAME_PARAMETER } from '../../../../constants/vm-templates';
import { selectVM } from '../../../../selectors/vm-template/basic';
import { convertToHighestUnitFromUnknown } from '../../../form/size-unit-utils';
import { toUIFlavor, isCustomFlavor } from '../../../../selectors/vm-like/flavor';

export const prefillVmTemplateUpdater = ({ id, dispatch, getState }: UpdateOptions) => {
const state = getState();
Expand Down Expand Up @@ -122,8 +122,10 @@ export const prefillVmTemplateUpdater = ({ id, dispatch, getState }: UpdateOptio

// update flavor
const [flavor] = getTemplateFlavors([userTemplate]);
vmSettingsUpdate[VMSettingsField.FLAVOR] = { value: flavor };
if (flavor === CUSTOM_FLAVOR) {
vmSettingsUpdate[VMSettingsField.FLAVOR] = {
value: toUIFlavor(flavor),
};
if (isCustomFlavor(flavor)) {
vmSettingsUpdate[VMSettingsField.CPU] = { value: parseCPU(getCPU(vm), DEFAULT_CPU).cores }; // TODO also add sockets + threads
const memory = convertToHighestUnitFromUnknown(getMemory(vm));
vmSettingsUpdate[VMSettingsField.MEMORY] = {
Expand Down Expand Up @@ -259,7 +261,9 @@ export const prefillVmTemplateUpdater = ({ id, dispatch, getState }: UpdateOptio
},
);
if (flavors.length === 1) {
vmSettingsUpdate[VMSettingsField.FLAVOR] = { value: flavors[0] };
vmSettingsUpdate[VMSettingsField.FLAVOR] = {
value: toUIFlavor(flavors[0]),
};
}

const newSourceStorage = getProvisionSourceStorage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
VMWizardTabsMetadata,
} from '../../types';
import { getStringEnumValues } from '../../../../utils/types';
import { CUSTOM_FLAVOR } from '../../../../constants/vm/constants';
import { iGetCreateVMWizardTabs } from './common';
import { iGetCommonData } from './selectors';
import { isCustomFlavor } from '../../../../selectors/vm-like/flavor';

const getTabBoolean = (state, wizardID: string, stepId: VMWizardTab, key) =>
!!iGetIn(iGetCreateVMWizardTabs(state, wizardID), [stepId, key]);
Expand Down Expand Up @@ -88,6 +88,6 @@ export const isWizardEmpty = (state, wizardID: string) => {

return ![...fields].some((fieldKey) => {
const value = iGetIn(stepData, [VMWizardTab.VM_SETTINGS, 'value', fieldKey, 'value']);
return fieldKey === VMSettingsField.FLAVOR && value === CUSTOM_FLAVOR ? null : value;
return fieldKey === VMSettingsField.FLAVOR && isCustomFlavor(value) ? null : value;
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { getBooleanReadableValue } from '../../../../utils/strings';
import { iGetFieldValue } from '../../selectors/immutable/field';
import { VMSettingsField } from '../../types';
import { iGet, iGetIn, toShallowJS } from '../../../../utils/immutable';
import { CUSTOM_FLAVOR } from '../../../../constants/vm';
import { iGetRelevantTemplate } from '../../../../selectors/immutable/template/combined';
import { VMTemplateWrapper } from '../../../../k8s/wrapper/vm/vm-template-wrapper';
import { Map as ImmutableMap } from 'immutable';
import { ITemplate } from '../../../../types/template';
import { getFlavorText } from '../../../../selectors/vm/flavor-text';
import { isCustomFlavor } from '../../../../selectors/vm-like/flavor';

export const getReviewValue = (field: any, fieldType: FormFieldType) => {
const value = iGetFieldValue(field);
Expand All @@ -33,7 +33,7 @@ export const getFlavorValue = ({
let cpu;
let memory;

if (flavor === CUSTOM_FLAVOR) {
if (isCustomFlavor(flavor)) {
cpu = {
sockets: 1,
cores: parseInt(getFieldValue(iVMSettings, VMSettingsField.CPU), 10),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as _ from 'lodash';
import * as React from 'react';
import { FormSelect, FormSelectOption } from '@patternfly/react-core';
import { ValidationErrorType, asValidationObject } from '@console/shared/src/utils/validation';
Expand Down Expand Up @@ -125,7 +126,7 @@ export const OSFlavor: React.FC<OSFlavorProps> = React.memo(
isDisabled={!!flavor}
/>
{flavors.map((f) => {
return <FormSelectOption key={f} value={f} label={f} />;
return <FormSelectOption key={f} value={f} label={_.capitalize(f)} />;
})}
</FormSelect>
</FormField>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as React from 'react';
import { DASH, getName, getNamespace } from '@console/shared';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { CUSTOM_FLAVOR } from '../../../constants/vm';
import { VMKind } from '../../../types/vm';
import {
getCPU,
getDataVolumeTemplates,
getDisks,
getFlavor,
getFlavorDescription,
getInterfaces,
getMemory,
getOperatingSystemName,
getVolumes,
getWorkloadProfile,
Expand All @@ -19,6 +19,7 @@ import {
getDataVolumeStorageClassName,
} from '../../../selectors/dv/selectors';
import { getPvcResources, getPvcStorageClassName } from '../../../selectors/pvc/selectors';
import { getFlavorText } from '../../../selectors/vm/flavor-text';

import './_clone-vm-modal.scss';

Expand Down Expand Up @@ -64,15 +65,6 @@ const getDisksDescription = (
});
};

const getFullFlavorDescription = (vm: VMKind) => {
const flavorDesc = getFlavorDescription(vm);
const flavor = getFlavor(vm) || CUSTOM_FLAVOR;
if (!flavorDesc) {
return flavor;
}
return `${flavor} - ${flavorDesc}`;
};

export const ConfigurationSummary: React.FC<ConfigurationSummaryProps> = ({
id,
vm,
Expand All @@ -86,7 +78,13 @@ export const ConfigurationSummary: React.FC<ConfigurationSummaryProps> = ({
<dt>Operating System</dt>
<dd>{getOperatingSystemName(vm) || DASH}</dd>
<dt>Flavor</dt>
<dd>{getFullFlavorDescription(vm)}</dd>
<dd>
{getFlavorText({
flavor: getFlavor(vm),
cpu: getCPU(vm),
memory: getMemory(vm),
})}
</dd>
<dt>Workload Profile</dt>
<dd>{getWorkloadProfile(vm) || DASH}</dd>
<dt>NICs</dt>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ import { getDialogUIError } from '../../../utils/strings';
import { flavorSort } from '../../../utils/sort';
import { getTemplateFlavors } from '../../../selectors/vm-template/advanced';
import { getVMTemplateNamespacedName } from '../../../selectors/vm-template/selectors';
import { toUIFlavor, isCustomFlavor } from '../../../selectors/vm-like/flavor';

const getId = (field: string) => `vm-flavor-modal-${field}`;

const getAvailableFlavors = (template: TemplateKind) => {
const flavors = [...getTemplateFlavors([template]), CUSTOM_FLAVOR];
const flavors = getTemplateFlavors([template]).filter((f) => f && !isCustomFlavor(f));
flavors.push(CUSTOM_FLAVOR);

return _.uniq(flavorSort(flavors).filter((f) => f));
return _.uniq(flavorSort(flavors));
};

const VMFlavorModal = withHandlePromise((props: VMFlavornModalProps) => {
Expand All @@ -55,14 +57,14 @@ const VMFlavorModal = withHandlePromise((props: VMFlavornModalProps) => {
const underlyingTemplate = getLoadedData(template);

const flavors = getAvailableFlavors(underlyingTemplate);
const vmFlavor = getFlavor(vmLike) || flavors[flavors.length - 1];
const vmFlavor = toUIFlavor(getFlavor(vmLike) || flavors[flavors.length - 1]);

const [sourceMemSize, sourceMemUnit] = stringValueUnitSplit(getMemory(vm) || '');
const sourceCPURaw = getCPU(vm);
const sourceCPU = vCPUCount(sourceCPURaw);

const [flavor, setFlavor] = React.useState(vmFlavor);
const isCustom = flavor === CUSTOM_FLAVOR;
const isCustom = isCustomFlavor(flavor);

const [memSize, setMemSize] = React.useState<string>(isCustom ? sourceMemSize || '' : '');
const [memUnit, setMemUnit] = React.useState<string>(
Expand Down Expand Up @@ -111,8 +113,8 @@ const VMFlavorModal = withHandlePromise((props: VMFlavornModalProps) => {
<FormRow title="Flavor" fieldId={getId('flavor')} isRequired>
<FormSelect
onChange={(f) => {
if (f === CUSTOM_FLAVOR) {
const isSourceCustom = vmFlavor === CUSTOM_FLAVOR;
if (isCustomFlavor(f)) {
const isSourceCustom = isCustomFlavor(vmFlavor);
setMemSize(isSourceCustom ? sourceMemSize || '' : '');
setMemUnit(isSourceCustom ? sourceMemUnit || BinaryUnit.Gi : BinaryUnit.Gi);
setCpus(isSourceCustom ? `${sourceCPU}` : '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const VMTemplateSchedulingList: React.FC<VMTemplateResourceSummaryProps>
const vm = asVM(template);
const vmWrapper = new VMWrapper(vm);
const flavorText = getFlavorText({
flavor: getFlavor(vm),
flavor: getFlavor(template),
cpu: vmWrapper.getCPU(),
memory: vmWrapper.getMemory(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const TEMPLATE_WORKLOAD_LABEL = 'workload.template.kubevirt.io';
export const TEMPLATE_VM_NAME_LABEL = 'vm.kubevirt.io/name';
export const TEMPLATE_OS_NAME_ANNOTATION = 'name.os.template.kubevirt.io';
export const TEMPLATE_VM_DOMAIN_LABEL = 'kubevirt.io/domain';
export const TEMPLATE_VM_SIZE_LABEL = 'kubevirt.io/size';

export const LABEL_USED_TEMPLATE_NAME = 'vm.kubevirt.io/template';
export const LABEL_USED_TEMPLATE_NAMESPACE = 'vm.kubevirt.io/template.namespace';
Expand Down
44 changes: 33 additions & 11 deletions frontend/packages/kubevirt-plugin/src/k8s/patches/vm/vm-patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { VMGenericLikeEntityKind, VMLikeEntityKind } from '../../../types/vmLike
import { getAnnotations, getDescription } from '../../../selectors/selectors';
import { getFlavor, getCPU, getMemory, parseCPU, DEFAULT_CPU } from '../../../selectors/vm';
import { isTemplate, isVM } from '../../../selectors/check-type';
import { CUSTOM_FLAVOR, TEMPLATE_FLAVOR_LABEL } from '../../../constants';
import { TEMPLATE_FLAVOR_LABEL, TEMPLATE_VM_SIZE_LABEL } from '../../../constants';
import { getVMLikePatches } from '../vm-template';
import { selectVM } from '../../../selectors/vm-template/basic';
import { CPU, VMITemplate, VMKind } from '../../../types/vm';
import { PatchBuilder } from '@console/shared/src/k8s';
import { getLabels } from '@console/shared/src';
import { isCustomFlavor } from '../../../selectors/vm-like/flavor';

const getDomainPatches = (vm: VMKind): Patch[] => {
let patch: Patch = null;
Expand Down Expand Up @@ -62,19 +63,22 @@ const getUpdateFlavorPatchesImpl = (
isVM(vmLike) || isTemplate(vmLike) ? '/metadata/labels' : '/spec/template/metadata/labels'; // or VMITemplate

const patches = [];
if (oldFlavor !== newFlavor) {
// also remove old unused Custom labels
if (isCustomFlavor(newFlavor) || oldFlavor !== newFlavor) {
const labels = getLabels(vmLike);
const flavorLabel = Object.keys(labels || {}).find((key) =>
key.startsWith(TEMPLATE_FLAVOR_LABEL),
);
if (flavorLabel) {
patches.push(new PatchBuilder(path).setObjectRemove(flavorLabel, labels).build());
}
patches.push(
new PatchBuilder(path)
.setObjectUpdate(`${TEMPLATE_FLAVOR_LABEL}/${newFlavor}`, 'true', labels)
.build(),
);
if (!isCustomFlavor(newFlavor)) {
patches.push(
new PatchBuilder(path)
.setObjectUpdate(`${TEMPLATE_FLAVOR_LABEL}/${newFlavor}`, 'true', labels)
.build(),
);
}
}
return patches;
};
Expand Down Expand Up @@ -168,6 +172,26 @@ export const getUpdateDescriptionPatches = (
return patches;
};

const getSizeLabelPatch = (flavor: string, vmi: VMITemplate): Patch[] => {
const patches = [];

if (isCustomFlavor(flavor)) {
patches.push(
new PatchBuilder('/spec/template/metadata/labels')
.setObjectRemove(TEMPLATE_VM_SIZE_LABEL, getLabels(vmi))
.build(),
);
} else {
patches.push(
new PatchBuilder('/spec/template/metadata/labels')
.setObjectUpdate(TEMPLATE_VM_SIZE_LABEL, flavor, getLabels(vmi))
.build(),
);
}

return patches;
};

export const getUpdateFlavorPatches = (
vmLike: VMLikeEntityKind,
template: TemplateKind,
Expand All @@ -182,7 +206,7 @@ export const getUpdateFlavorPatches = (
threads: 1,
};
let customMem = mem;
if (flavor !== CUSTOM_FLAVOR) {
if (!isCustomFlavor(flavor)) {
const templateVm = selectVM(template);
customCpu = parseCPU(getCPU(templateVm), DEFAULT_CPU);
customMem = getMemory(templateVm);
Expand All @@ -193,9 +217,7 @@ export const getUpdateFlavorPatches = (
...getVMLikePatches(vmLike, (vm: VMKind) => {
const vmi = vm.spec?.template;
const additionalPatches = [
new PatchBuilder('/spec/template/metadata/labels')
.setObjectUpdate(`kubevirt.io/size`, flavor, getLabels(vmi))
.build(),
...getSizeLabelPatch(flavor, vmi),
...getUpdateCpuMemoryPatch(vm, customCpu, customMem),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '../../../../constants/vm';
import { VMWrapper } from '../../../wrapper/vm/vm-wrapper';
import { VMTemplateWrapper } from '../../../wrapper/vm/vm-template-wrapper';
import { isCustomFlavor } from '../../../../selectors/vm-like/flavor';

export const initializeCommonMetadata = (
settings: {
Expand All @@ -34,7 +35,10 @@ export const initializeCommonMetadata = (
}

entity.addLabel(`${TEMPLATE_OS_LABEL}/${settings.osID}`, 'true');
entity.addLabel(`${TEMPLATE_FLAVOR_LABEL}/${settings[VMSettingsField.FLAVOR]}`, 'true');

if (!isCustomFlavor(settings[VMSettingsField.FLAVOR])) {
entity.addLabel(`${TEMPLATE_FLAVOR_LABEL}/${settings[VMSettingsField.FLAVOR]}`, 'true');
}

if (settings[VMSettingsField.WORKLOAD_PROFILE]) {
entity.addLabel(
Expand Down Expand Up @@ -75,7 +79,10 @@ export const initializeCommonVMMetadata = (
// show metadata inside a VMI

entity.addTemplateLabel(`${TEMPLATE_OS_LABEL}/${settings.osID}`, 'true');
entity.addTemplateLabel(`${TEMPLATE_FLAVOR_LABEL}/${settings[VMSettingsField.FLAVOR]}`, 'true');

if (!isCustomFlavor(settings[VMSettingsField.FLAVOR])) {
entity.addTemplateLabel(`${TEMPLATE_FLAVOR_LABEL}/${settings[VMSettingsField.FLAVOR]}`, 'true');
}

if (settings[VMSettingsField.WORKLOAD_PROFILE]) {
entity.addTemplateLabel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import {
ANNOTATION_FIRST_BOOT,
ANNOTATION_PXE_INTERFACE,
CUSTOM_FLAVOR,
DataVolumeSourceType,
VolumeType,
} from '../../../../constants/vm';
Expand All @@ -24,6 +23,7 @@ import { StorageUISource } from '../../../../components/modals/disk-modal/storag
import { insertName, joinIDs } from '../../../../utils';
import { VM_TEMPLATE_NAME_PARAMETER } from '../../../../constants/vm-templates';
import { CreateVMParams } from './types';
import { isCustomFlavor } from '../../../../selectors/vm-like/flavor';

const resolveDataVolumeName = (
dataVolumeWrapper: DataVolumeWrapper,
Expand Down Expand Up @@ -111,7 +111,7 @@ export const initializeVM = (params: CreateVMParams, vm: VMWrapper) => {
const settings = asSimpleSettings(vmSettings);
const isRunning = settings[VMSettingsField.START_VM];

if (settings[VMSettingsField.FLAVOR] === CUSTOM_FLAVOR) {
if (isCustomFlavor(settings[VMSettingsField.FLAVOR])) {
vm.setCPU({ sockets: 1, cores: parseInt(settings[VMSettingsField.CPU], 10), threads: 1 });
vm.setMemory(settings[VMSettingsField.MEMORY]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ metadata:
labels:
template.kubevirt.io/type: vm
os.template.kubevirt.io/fedora31: 'true'
flavor.template.kubevirt.io/Custom: 'true'
workload.template.kubevirt.io/server: 'true'
annotations:
name.os.template.kubevirt.io/fedora31: Fedora 31
Expand All @@ -29,7 +28,6 @@ objects:
metadata:
labels:
kubevirt.io/domain: '\${NAME}'
kubevirt.io/size: Custom
spec:
domain:
cpu:
Expand Down

0 comments on commit 3ee4d44

Please sign in to comment.