Skip to content

Commit 685eff3

Browse files
authored
fix(vm): fix stopping a VM with unapplied changes (#991)
Signed-off-by: Dmitry Lopatin <dmitry.lopatin@flant.com>
1 parent 424803c commit 685eff3

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

images/virtualization-artifact/pkg/controller/vm/internal/sync_kvvm.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
vmutil "github.com/deckhouse/virtualization-controller/pkg/common/vm"
3434
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
3535
"github.com/deckhouse/virtualization-controller/pkg/controller/kvbuilder"
36-
"github.com/deckhouse/virtualization-controller/pkg/controller/powerstate"
3736
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
3837
"github.com/deckhouse/virtualization-controller/pkg/controller/vm/internal/state"
3938
"github.com/deckhouse/virtualization-controller/pkg/controller/vmchange"
@@ -495,7 +494,7 @@ func (h *SyncKvvmHandler) canApplyChanges(
495494
if vm == nil || changes.IsEmpty() {
496495
return false
497496
}
498-
if vmutil.ApprovalMode(vm) == virtv2.Automatic || !changes.IsDisruptive() || kvvmi == nil {
497+
if !changes.IsDisruptive() || kvvmi == nil {
499498
return true
500499
}
501500

@@ -537,23 +536,10 @@ func (h *SyncKvvmHandler) applyVMChangesToKVVM(ctx context.Context, s state.Virt
537536

538537
switch action {
539538
case vmchange.ActionRestart:
540-
h.recorder.WithLogging(log).Event(current, corev1.EventTypeNormal, virtv2.ReasonVMChangesApplied, "Apply disruptive changes with restart")
541-
h.recorder.WithLogging(log).Event(
542-
current,
543-
corev1.EventTypeNormal,
544-
virtv2.ReasonVMRestarted,
545-
"Restart initiated by controller to apply changes",
546-
)
547-
548539
// Update KVVM spec according the current VM spec.
549540
if err = h.updateKVVM(ctx, s); err != nil {
550541
return fmt.Errorf("update virtual machine instance with new spec: %w", err)
551542
}
552-
// Ask kubevirt to re-create KVVMI to apply new spec from KVVM.
553-
err = powerstate.RestartVM(ctx, h.client, kvvm, kvvmi, false)
554-
if err != nil {
555-
return fmt.Errorf("restart virtual machine instance to apply changes: %w", err)
556-
}
557543

558544
case vmchange.ActionApplyImmediate:
559545
message := "Apply changes without restart"

images/virtualization-artifact/pkg/controller/vm/internal/sync_power_state.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,27 @@ func (h *SyncPowerStateHandler) syncPowerState(
151151

152152
switch vmAction {
153153
case Nothing:
154+
vm := s.VirtualMachine().Changed()
155+
cbAwaitingRestart, exist := conditions.GetCondition(vmcondition.TypeAwaitingRestartToApplyConfiguration, vm.Status.Conditions)
156+
if exist && cbAwaitingRestart.Status == metav1.ConditionTrue &&
157+
cbAwaitingRestart.ObservedGeneration == vm.GetGeneration() &&
158+
vm.Spec.Disruptions.RestartApprovalMode == virtv2.Automatic {
159+
160+
log := logger.FromContext(ctx)
161+
h.recorder.WithLogging(log).Event(vm, corev1.EventTypeNormal, virtv2.ReasonVMChangesApplied, "Apply disruptive changes with restart")
162+
h.recorder.WithLogging(log).Event(
163+
vm,
164+
corev1.EventTypeNormal,
165+
virtv2.ReasonVMRestarted,
166+
"Restart initiated by controller to apply changes",
167+
)
168+
err = powerstate.RestartVM(ctx, h.client, kvvm, kvvmi, false)
169+
if err != nil {
170+
return fmt.Errorf("restart virtual machine instance to apply changes: %w", err)
171+
}
172+
return nil
173+
}
174+
154175
return nil
155176
case Start:
156177
return h.start(ctx, s, kvvm, isConfigurationApplied)

tests/e2e/vm_configuration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func CheckCPUCoresNumber(approvalMode, stage string, requiredValue int, virtualM
106106
case approvalMode == ManualMode && stage == StageAfter:
107107
Expect(vmResource.Status.RestartAwaitingChanges).ShouldNot(BeNil())
108108
case approvalMode == AutomaticMode && stage == StageAfter:
109-
Expect(vmResource.Status.RestartAwaitingChanges).Should(BeNil())
109+
Expect(vmResource.Status.RestartAwaitingChanges).ShouldNot(BeNil())
110110
}
111111
}
112112
}

0 commit comments

Comments
 (0)