Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit 557905c

Browse files
rawagnermareklibra
authored andcommitted
Add metadata to created VM - flavor, os, workload and used template (#79)
1 parent 1266e66 commit 557905c

File tree

8 files changed

+84
-8
lines changed

8 files changed

+84
-8
lines changed

src/constants/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const ANNOTATION_DEFAULT_DISK = 'defaults.template.cnv.io/disk';
1212
export const ANNOTATION_DEFAULT_NETWORK = 'defaults.template.cnv.io/network';
1313
export const ANNOTATION_FIRST_BOOT = 'cnv.ui.firstBoot';
1414
export const ANNOTATION_PXE_INTERFACE = 'cnv.ui.pxeInterface';
15+
export const ANNOTATION_USED_TEMPLATE = 'template.cnv.ui';
1516

1617
export const CLOUDINIT_DISK = 'cloudinitdisk';
1718
export const CLOUDINIT_NOCLOUD = 'cloudInitNoCloud';

src/k8s/mock_templates/fedora28.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const fedora28 = {
33
kind: 'Template',
44
metadata: {
55
name: 'fedora-generic',
6+
namespace: 'default',
67
annotations: {
78
'openshift.io/display-name': 'Fedora 23+ VM',
89
description:

src/k8s/mock_templates/rhel-high-p.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const rhelHighPerformance = {
33
kind: 'Template',
44
metadata: {
55
name: 'rhel-high-performance',
6+
namespace: 'default',
67
annotations: {
78
'openshift.io/display-name': 'Red Hat Enterprise Linux 7.0+ VM High Performance',
89
description:

src/k8s/mock_templates/rhel75.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const rhel75 = {
33
kind: 'Template',
44
metadata: {
55
name: 'rhel-generic',
6+
namespace: 'default',
67
annotations: {
78
'openshift.io/display-name': 'Red Hat Enterprise Linux 7.0+ VM',
89
description:

src/k8s/mock_templates/ubuntu1804.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const ubuntu1804 = {
33
kind: 'Template',
44
metadata: {
55
name: 'ubuntu1804',
6+
namespace: 'default',
67
annotations: {
78
'openshift.io/display-name': 'Ubuntu 18.04 (Xenial Xerus) VM',
89
description:

src/k8s/mock_templates/windows.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const windows = {
33
kind: 'Template',
44
metadata: {
55
name: 'win2k12r2',
6+
namespace: 'default',
67
annotations: {
78
'openshift.io/display-name': 'Microsoft Windows Server 2012 R2 VM',
89
description:

src/k8s/request.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
ANNOTATION_PXE_INTERFACE,
1919
TEMPLATE_API_VERSION,
2020
PVC_ACCESSMODE_RWO,
21-
CLOUDINIT_NOCLOUD
21+
CLOUDINIT_NOCLOUD,
22+
TEMPLATE_FLAVOR_LABEL,
23+
TEMPLATE_OS_LABEL,
24+
TEMPLATE_WORKLOAD_LABEL,
25+
ANNOTATION_USED_TEMPLATE
2226
} from '../constants';
2327
import {
2428
NAMESPACE_KEY,
@@ -34,7 +38,9 @@ import {
3438
CLOUD_INIT_KEY,
3539
HOST_NAME_KEY,
3640
AUTHKEYS_KEY,
37-
NAME_KEY
41+
NAME_KEY,
42+
OPERATING_SYSTEM_KEY,
43+
WORKLOAD_PROFILE_KEY
3844
} from '../components/Wizard/CreateVmWizard/constants';
3945
import { VirtualMachineModel, ProcessedTemplatesModel, PersistentVolumeClaimModel } from '../models';
4046
import { getTemplatesWithLabels, getTemplate } from '../utils/templates';
@@ -57,6 +63,7 @@ export const createVM = (k8sCreate, templates, basicSettings, { networks }, stor
5763

5864
return k8sCreate(ProcessedTemplatesModel, template).then(({ objects }) => {
5965
const vm = selectVm(objects);
66+
addMetadata(vm, template, getSetting);
6067
modifyVmObject(vm, template, getSetting, networks, additionalStorage);
6168

6269
if (getSetting(IMAGE_SOURCE_TYPE_KEY) === PROVISION_SOURCE_TEMPLATE) {
@@ -112,15 +119,27 @@ const resolveTemplate = (templates, basicSettings, getSetting, storage) => {
112119
}
113120
});
114121

115-
// processedtemplate endpoint is namespaced
116-
chosenTemplate.metadata.namespace = getSetting(NAMESPACE_KEY);
117-
118122
// make sure api version is correct
119123
chosenTemplate.apiVersion = TEMPLATE_API_VERSION;
120124

121125
return chosenTemplate;
122126
};
123127

128+
const addMetadata = (vm, template, getSetting) => {
129+
const userTemplate = getSetting(IMAGE_SOURCE_TYPE_KEY) === PROVISION_SOURCE_TEMPLATE;
130+
131+
const flavor = userTemplate ? CUSTOM_FLAVOR : getSetting(FLAVOR_KEY);
132+
addAnnotation(vm, TEMPLATE_FLAVOR_LABEL, flavor);
133+
134+
const os = userTemplate ? template.metadata.labels[TEMPLATE_OS_LABEL] : getSetting(OPERATING_SYSTEM_KEY);
135+
addAnnotation(vm, TEMPLATE_OS_LABEL, os);
136+
137+
const workload = userTemplate ? template.metadata.labels[TEMPLATE_WORKLOAD_LABEL] : getSetting(WORKLOAD_PROFILE_KEY);
138+
addAnnotation(vm, TEMPLATE_WORKLOAD_LABEL, workload);
139+
140+
addAnnotation(vm, ANNOTATION_USED_TEMPLATE, `${template.metadata.namespace}/${template.metadata.name}`);
141+
};
142+
124143
const modifyVmObject = (vm, template, getSetting, networks, storages) => {
125144
setFlavor(vm, getSetting);
126145
setNetworks(vm, template, getSetting, networks);

src/k8s/tests/request.test.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { get } from 'lodash';
1+
import { get, cloneDeep } from 'lodash';
22
import { createVM } from '../request';
33
import { settingsValue } from '../selectors';
44

@@ -10,7 +10,11 @@ import {
1010
PROVISION_SOURCE_URL,
1111
TEMPLATE_PARAM_VM_NAME,
1212
templates,
13-
POD_NETWORK
13+
POD_NETWORK,
14+
TEMPLATE_FLAVOR_LABEL,
15+
TEMPLATE_WORKLOAD_LABEL,
16+
TEMPLATE_OS_LABEL,
17+
ANNOTATION_USED_TEMPLATE
1418
} from '../../constants';
1519

1620
import {
@@ -28,7 +32,8 @@ import {
2832
CLOUD_INIT_KEY,
2933
HOST_NAME_KEY,
3034
AUTHKEYS_KEY,
31-
OPERATING_SYSTEM_KEY
35+
OPERATING_SYSTEM_KEY,
36+
WORKLOAD_PROFILE_KEY
3237
} from '../../components/Wizard/CreateVmWizard/constants';
3338

3439
import { linuxUserTemplate } from '../mock_user_templates/linux.template';
@@ -53,6 +58,9 @@ const basicSettings = {
5358
},
5459
[OPERATING_SYSTEM_KEY]: {
5560
value: 'rhel7.0'
61+
},
62+
[WORKLOAD_PROFILE_KEY]: {
63+
value: 'generic'
5664
}
5765
};
5866

@@ -349,6 +357,13 @@ const testPXE = vm => {
349357
return vm;
350358
};
351359

360+
const testMetadata = (vm, os, workload, flavor, template) => {
361+
expect(vm.metadata.annotations[TEMPLATE_FLAVOR_LABEL]).toEqual(flavor);
362+
expect(vm.metadata.annotations[TEMPLATE_OS_LABEL]).toEqual(os);
363+
expect(vm.metadata.annotations[TEMPLATE_WORKLOAD_LABEL]).toEqual(workload);
364+
expect(vm.metadata.annotations[ANNOTATION_USED_TEMPLATE]).toEqual(template);
365+
};
366+
352367
describe('request.js', () => {
353368
it('registryImage', () => createVM(k8sCreate, templates, basicSettings, networks).then(testRegistryImage));
354369
it('from URL', () =>
@@ -483,4 +498,40 @@ describe('request.js', () => {
483498
testFirstAttachedStorage(vm, 0, 0, 2);
484499
return vm;
485500
}));
501+
502+
it('VM has os/flavor/workload metadata', () =>
503+
createVM(k8sCreate, templates, basicSettings, pxeNetworks, attachStorageDisks).then(vm => {
504+
testMetadata(
505+
vm,
506+
basicSettings[OPERATING_SYSTEM_KEY].value,
507+
basicSettings[WORKLOAD_PROFILE_KEY].value,
508+
basicSettings[FLAVOR_KEY].value,
509+
'default/rhel-generic'
510+
);
511+
return vm;
512+
}));
513+
it('VM has os/flavor/workload metadata - user template without os and workload', () =>
514+
createVM(k8sCreate, templates, vmUserTemplate, pxeNetworks, attachStorageDisks).then(vm => {
515+
testMetadata(vm, undefined, undefined, CUSTOM_FLAVOR, 'openshift/linux-template');
516+
return vm;
517+
}));
518+
it('VM has os/flavor/workload metadata - user template with os and workload', () => {
519+
const settings = cloneDeep(vmUserTemplate);
520+
settings[USER_TEMPLATE_KEY].value = 'linux-template1';
521+
const userTemplate = cloneDeep(templates.find(template => template.metadata.name === 'linux-template'));
522+
userTemplate.metadata.name = 'linux-template1';
523+
userTemplate.metadata.labels[TEMPLATE_OS_LABEL] = 'fooOs';
524+
userTemplate.metadata.labels[TEMPLATE_WORKLOAD_LABEL] = 'fooWorkload';
525+
526+
return createVM(k8sCreate, [...templates, userTemplate], settings, pxeNetworks, attachStorageDisks).then(vm => {
527+
testMetadata(
528+
vm,
529+
userTemplate.metadata.labels[TEMPLATE_OS_LABEL],
530+
userTemplate.metadata.labels[TEMPLATE_WORKLOAD_LABEL],
531+
CUSTOM_FLAVOR,
532+
`${userTemplate.metadata.namespace}/${userTemplate.metadata.name}`
533+
);
534+
return vm;
535+
});
536+
});
486537
});

0 commit comments

Comments
 (0)