From 5c162ce603b13382dc9413025acf2dd677071e3c Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Wed, 3 Dec 2025 11:38:27 +0100 Subject: [PATCH] Power state fixes (#376) * Do not call vm.Suspend() and vm.Stop() twice * Do not attempt to Stop() or Suspend() the VM twice --- internal/worker/worker.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/internal/worker/worker.go b/internal/worker/worker.go index a02aed8..9c90ac5 100644 --- a/internal/worker/worker.go +++ b/internal/worker/worker.go @@ -364,7 +364,10 @@ func (worker *Worker) syncVMs(ctx context.Context, updateVM func(context.Context case ActionMonitorRunning: if vmResource.Generation != vm.Resource.Generation { // VM specification changed, reboot the VM for the changes to take effect - if v1.ConditionIsTrue(vm.Conditions(), v1.ConditionTypeRunning) { + stoppingOrSuspending := v1.ConditionIsTrue(vm.Conditions(), v1.ConditionTypeStopping) || + v1.ConditionIsTrue(vm.Conditions(), v1.ConditionTypeSuspending) + + if v1.ConditionIsTrue(vm.Conditions(), v1.ConditionTypeRunning) && !stoppingOrSuspending { // VM is running, suspend or stop it first shouldStop := vmResource.PowerState == v1.PowerStateStopped || !vm.Resource.Suspendable @@ -373,21 +376,17 @@ func (worker *Worker) syncVMs(ctx context.Context, updateVM func(context.Context } else { vm.Suspend() } + } - if vm.Resource.Suspendable && vmResource.PowerState != v1.PowerStateStopped { - vm.Suspend() - } else { - vm.Stop() + if v1.ConditionIsFalse(vm.Conditions(), v1.ConditionTypeRunning) && !stoppingOrSuspending { + // VM stopped, update its specification + vm.UpdateSpec(*vmResource) + + if vmResource.PowerState == v1.PowerStateRunning { + // Start the VM + eventStreamer := worker.client.VMs().StreamEvents(vmResource.Name) + vm.Start(eventStreamer) } - } else if vmResource.PowerState == v1.PowerStateRunning { - // VM stopped, start it with the new specification - vm.UpdateSpec(*vmResource) - - eventStreamer := worker.client.VMs().StreamEvents(vmResource.Name) - vm.Start(eventStreamer) - } else if vmResource.PowerState == v1.PowerStateSuspended || vmResource.PowerState == v1.PowerStateStopped { - // VM stopped, just update its specification - vm.UpdateSpec(*vmResource) } }