Power state fixes (#376)

* Do not call vm.Suspend() and vm.Stop() twice

* Do not attempt to Stop() or Suspend() the VM twice
This commit is contained in:
Nikolay Edigaryev 2025-12-03 11:38:27 +01:00 committed by GitHub
parent 0542a0d52a
commit 5c162ce603
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 14 deletions

View File

@ -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)
}
}