From 2ec5ebb373be75fe0ef5249b5a11350a217ed1ec Mon Sep 17 00:00:00 2001 From: Francesco Torchia Date: Tue, 17 Sep 2024 21:59:12 +0200 Subject: [PATCH] Refactoring devices models Signed-off-by: Francesco Torchia --- .../VirtualMachinePciDevices/index.vue | 24 +++++++------------ .../VirtualMachineUSBDevices/index.vue | 24 +++++++------------ .../VirtualMachineVGpuDevices/index.vue | 24 +++++++------------ .../models/kubevirt.io.virtualmachine.js | 4 ++++ 4 files changed, 31 insertions(+), 45 deletions(-) diff --git a/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachinePciDevices/index.vue b/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachinePciDevices/index.vue index 05ee97d4..161aeeda 100644 --- a/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachinePciDevices/index.vue +++ b/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachinePciDevices/index.vue @@ -7,7 +7,7 @@ import CompatibilityMatrix from '../CompatibilityMatrix'; import DeviceList from './DeviceList'; import remove from 'lodash/remove'; -import { get, set } from '@shell/utils/object'; +import { set } from '@shell/utils/object'; import { HCI } from '../../../types'; import DeviceList from './DeviceList'; import CompatibilityMatrix from './CompatibilityMatrix'; @@ -123,9 +123,8 @@ export default { if (vm.metadata.name === this.vm?.metadata?.name) { return inUse; } - const devices = get(vm, 'spec.template.spec.domain.devices.hostDevices') || []; - devices.forEach((device) => { + vm.hostDevices.forEach((device) => { inUse[device.name] = { usedBy: [vm.metadata.name] }; }); @@ -136,19 +135,14 @@ export default { }, devicesByNode() { - const out = {}; + return this.enabledDevices?.reduce((acc, device) => { + const name = device.status?.nodeName; - this.enabledDevices.forEach((deviceCRD) => { - const nodeName = deviceCRD.status?.nodeName; - - if (!out[nodeName]) { - out[nodeName] = [deviceCRD]; - } else { - out[nodeName].push(deviceCRD); - } - }); - - return out; + return name ? { + ...acc, + [name]: [...(acc[name] || []), device], + } : acc; + }, {}); }, // determine which nodes contain all devices selected diff --git a/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachineUSBDevices/index.vue b/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachineUSBDevices/index.vue index 93ee7b94..a58f236d 100644 --- a/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachineUSBDevices/index.vue +++ b/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachineUSBDevices/index.vue @@ -9,7 +9,7 @@ import CompatibilityMatrix from '../CompatibilityMatrix'; import DeviceList from './DeviceList'; import remove from 'lodash/remove'; -import { get, set } from '@shell/utils/object'; +import { set } from '@shell/utils/object'; export default { name: 'VirtualMachineUSBDevices', @@ -120,9 +120,8 @@ export default { if (vm.metadata.name === this.vm?.metadata?.name) { return inUse; } - const devices = get(vm, 'spec.template.spec.domain.devices.hostDevices') || []; - devices.forEach((device) => { + vm.hostDevices.forEach((device) => { inUse[device.name] = { usedBy: [vm.metadata.name] }; }); @@ -133,19 +132,14 @@ export default { }, devicesByNode() { - const out = {}; + return this.enabledDevices?.reduce((acc, device) => { + const name = device.status?.nodeName; - this.enabledDevices.forEach((deviceCRD) => { - const nodeName = deviceCRD.status?.nodeName; - - if (!out[nodeName]) { - out[nodeName] = [deviceCRD]; - } else { - out[nodeName].push(deviceCRD); - } - }); - - return out; + return name ? { + ...acc, + [name]: [...(acc[name] || []), device], + } : acc; + }, {}); }, compatibleNodes() { diff --git a/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachineVGpuDevices/index.vue b/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachineVGpuDevices/index.vue index bd773e50..bd69c863 100644 --- a/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachineVGpuDevices/index.vue +++ b/pkg/harvester/edit/kubevirt.io.virtualmachine/VirtualMachineVGpuDevices/index.vue @@ -5,7 +5,7 @@ import LabeledSelect from '@shell/components/form/LabeledSelect'; import Banner from '@components/Banner/Banner.vue'; import remove from 'lodash/remove'; -import { get, set } from '@shell/utils/object'; +import { set } from '@shell/utils/object'; import { HCI } from '../../../types'; import VGpuDeviceList from './VGpuDeviceList'; @@ -89,9 +89,8 @@ export default { if (vm.metadata.name === this.vm?.metadata?.name) { return inUse; } - const devices = get(vm, 'spec.template.spec.domain.devices.hostDevices') || []; - devices.forEach((device) => { + vm.hostDevices.forEach((device) => { inUse[device.name] = { usedBy: [vm.metadata.name] }; }); @@ -102,19 +101,14 @@ export default { }, devicesByNode() { - const out = {}; + return this.enabledDevices?.reduce((acc, device) => { + const name = device.spec?.nodeName; - this.enabledDevices.forEach((deviceCRD) => { - const nodeName = deviceCRD.spec?.nodeName; - - if (!out[nodeName]) { - out[nodeName] = [deviceCRD]; - } else { - out[nodeName].push(deviceCRD); - } - }); - - return out; + return name ? { + ...acc, + [name]: [...(acc[name] || []), device], + } : acc; + }, {}); }, compatibleNodes() { diff --git a/pkg/harvester/models/kubevirt.io.virtualmachine.js b/pkg/harvester/models/kubevirt.io.virtualmachine.js index 312a83a5..f7a02958 100644 --- a/pkg/harvester/models/kubevirt.io.virtualmachine.js +++ b/pkg/harvester/models/kubevirt.io.virtualmachine.js @@ -1078,6 +1078,10 @@ export default class VirtVm extends HarvesterResource { }); } + get hostDevices() { + return this.spec?.template?.spec?.domain?.devices?.hostDevices || []; + } + setInstanceLabels(val) { if ( !this.spec?.template?.metadata?.labels ) { set(this, 'spec.template.metadata.labels', {});