From 5c2a23924d03f631bd2ff3662b030f0d0a0e28a3 Mon Sep 17 00:00:00 2001 From: Andy Lee Date: Wed, 8 Oct 2025 16:11:27 +0800 Subject: [PATCH] fix: allow to edit as yaml if has empty CPU or memory (#547) * fix: allow edit as yaml in create VM page if empty CPU or memory Signed-off-by: Andy Lee * refactor: remove getCPUMemoryValidation Signed-off-by: Andy Lee --------- Signed-off-by: Andy Lee --- .../edit/kubevirt.io.virtualmachine/index.vue | 25 ++++++++++++++++--- pkg/harvester/mixins/harvester-vm/index.js | 15 ----------- pkg/harvester/utils/cpuMemory.js | 6 ++--- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/pkg/harvester/edit/kubevirt.io.virtualmachine/index.vue b/pkg/harvester/edit/kubevirt.io.virtualmachine/index.vue index 0321cb3d..b2eb1edd 100644 --- a/pkg/harvester/edit/kubevirt.io.virtualmachine/index.vue +++ b/pkg/harvester/edit/kubevirt.io.virtualmachine/index.vue @@ -338,6 +338,15 @@ export default { saveVM(buttonCb) { clear(this.errors); + this.validateCPUMemory(); + + // block create VM flow if has validation errors + if (this.errors.length) { + buttonCb(false); + + return; + } + if (this.isSingle) { this.saveSingle(buttonCb); } else { @@ -345,6 +354,18 @@ export default { } }, + validateCPUMemory() { + const { cpu, memory } = this; + + if ((!cpu)) { + this.errors.push(this.t('validation.required', { key: this.t('harvester.virtualMachine.input.cpu') }, true)); + } + + if ((!memory)) { + this.errors.push(this.t('validation.required', { key: this.t('harvester.virtualMachine.input.memory') }, true)); + } + }, + async saveSingle(buttonCb) { this.parseVM(); this.value.spec.template.spec.hostname = this.hostname ? this.hostname : this.value.metadata.name; @@ -466,9 +487,7 @@ export default { } } - const cpuMemoryErrors = this.getCPUMemoryValidation(); - const accessCredentialsErrors = this.getAccessCredentialsValidation(); - const errors = [...cpuMemoryErrors, ...accessCredentialsErrors]; + const errors = this.getAccessCredentialsValidation(); if (errors.length > 0) { return Promise.reject(errors); diff --git a/pkg/harvester/mixins/harvester-vm/index.js b/pkg/harvester/mixins/harvester-vm/index.js index 2a7c2071..9e0218e6 100644 --- a/pkg/harvester/mixins/harvester-vm/index.js +++ b/pkg/harvester/mixins/harvester-vm/index.js @@ -1369,21 +1369,6 @@ export default { } }, - getCPUMemoryValidation() { - const errors = []; - const { cpu, memory } = this; - - if ((!cpu)) { - errors.push(this.t('validation.required', { key: this.t('harvester.virtualMachine.input.cpu') }, true)); - } - - if ((!memory)) { - errors.push(this.t('validation.required', { key: this.t('harvester.virtualMachine.input.memory') }, true)); - } - - return errors; - }, - getAccessCredentialsValidation() { const errors = []; diff --git a/pkg/harvester/utils/cpuMemory.js b/pkg/harvester/utils/cpuMemory.js index 7883eaee..d06858d3 100644 --- a/pkg/harvester/utils/cpuMemory.js +++ b/pkg/harvester/utils/cpuMemory.js @@ -3,15 +3,15 @@ import { HCI as HCI_ANNOTATIONS } from '@pkg/harvester/config/labels-annotations export function getVmCPUMemoryValues(vm) { if (!vm) { return { - cpu: 0, + cpu: null, memory: null, isHotplugEnabled: false }; } const isHotplugEnabled = isCPUMemoryHotPlugEnabled(vm); - const { sockets = 1, threads = 1, cores = 1 } = vm.spec.template.spec.domain.cpu || {}; - const cpu = sockets * threads * cores; + const { sockets = 1, threads = 1, cores = null } = vm.spec.template.spec.domain.cpu || {}; + const cpu = cores === null ? null : sockets * threads * cores; if (isHotplugEnabled) { return {