From 8eb1f802e163dc717585381f7abe10059a6514d3 Mon Sep 17 00:00:00 2001 From: "dmitry.lopatin" Date: Mon, 9 Dec 2024 23:00:37 +0300 Subject: [PATCH] ++ Signed-off-by: dmitry.lopatin --- api/core/v1alpha2/vmcondition/condition.go | 1 + .../vm/internal/sync_power_state.go | 26 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/api/core/v1alpha2/vmcondition/condition.go b/api/core/v1alpha2/vmcondition/condition.go index e99b6416a..29e372ea6 100644 --- a/api/core/v1alpha2/vmcondition/condition.go +++ b/api/core/v1alpha2/vmcondition/condition.go @@ -34,6 +34,7 @@ const ( TypeAgentReady Type = "AgentReady" TypeAgentVersionNotSupported Type = "AgentVersionNotSupported" TypeConfigurationApplied Type = "ConfigurationApplied" + TypePowerConfigurationApplied Type = "PowerConfigurationApplied" TypeAwaitingRestartToApplyConfiguration Type = "AwaitingRestartToApplyConfiguration" TypeFilesystemReady Type = "FilesystemReady" TypeSizingPolicyMatched Type = "SizingPolicyMatched" diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/sync_power_state.go b/images/virtualization-artifact/pkg/controller/vm/internal/sync_power_state.go index 99d9fbacd..3049845ed 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/sync_power_state.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/sync_power_state.go @@ -60,39 +60,44 @@ func (h *SyncPowerStateHandler) Handle(ctx context.Context, s state.VirtualMachi current := s.VirtualMachine().Current() changed := s.VirtualMachine().Changed() - cbConfApplied := conditions.NewConditionBuilder(vmcondition.TypeConfigurationApplied). + cbPowerConfApplied := conditions.NewConditionBuilder(vmcondition.TypePowerConfigurationApplied). Generation(current.GetGeneration()). Status(metav1.ConditionUnknown). Reason(conditions.ReasonUnknown) defer func() { - conditions.SetCondition(cbConfApplied, &changed.Status.Conditions) + conditions.SetCondition(cbPowerConfApplied, &changed.Status.Conditions) }() kvvm, err := s.KVVM(ctx) if err != nil { - cbConfApplied. + cbPowerConfApplied. Status(metav1.ConditionFalse). Reason(vmcondition.ReasonConfigurationNotApplied). Message(service.CapitalizeFirstLetter(err.Error()) + ".") return reconcile.Result{}, err } - err = h.syncPowerState(ctx, s, kvvm, &changed.Spec) + err = h.syncPowerState(ctx, s, kvvm, changed.Spec.RunPolicy) if err != nil { err = fmt.Errorf("failed to sync powerstate: %w", err) h.recorder.Event(current, corev1.EventTypeWarning, virtv2.ReasonErrVmNotSynced, err.Error()) - cbConfApplied. + cbPowerConfApplied. Status(metav1.ConditionFalse). Reason(vmcondition.ReasonConfigurationNotApplied). Message(service.CapitalizeFirstLetter(err.Error()) + ".") + } else { + cbPowerConfApplied. + Status(metav1.ConditionTrue). + Reason(vmcondition.ReasonConfigurationApplied). + Message("") } return reconcile.Result{}, err } // syncPowerState enforces runPolicy on the underlying KVVM. -func (h *SyncPowerStateHandler) syncPowerState(ctx context.Context, s state.VirtualMachineState, kvvm *virtv1.VirtualMachine, effectiveSpec *virtv2.VirtualMachineSpec) error { +func (h *SyncPowerStateHandler) syncPowerState(ctx context.Context, s state.VirtualMachineState, kvvm *virtv1.VirtualMachine, vmRunPolicy virtv2.RunPolicy) error { log := logger.FromContext(ctx) if kvvm == nil { @@ -104,7 +109,6 @@ func (h *SyncPowerStateHandler) syncPowerState(ctx context.Context, s state.Virt return fmt.Errorf("find the internal virtual machine instance: %w", err) } - vmRunPolicy := effectiveSpec.RunPolicy var shutdownInfo powerstate.ShutdownInfo s.Shared(func(s *state.Shared) { shutdownInfo = s.ShutdownInfo @@ -119,7 +123,6 @@ func (h *SyncPowerStateHandler) syncPowerState(ctx context.Context, s state.Virt return fmt.Errorf("force AlwaysOff: delete KVVMI: %w", err) } } - err = h.ensureRunStrategy(ctx, kvvm, virtv1.RunStrategyHalted) case virtv2.AlwaysOnPolicy: strategy, _ := kvvm.RunStrategy() if strategy == virtv1.RunStrategyAlways && kvvmi == nil { @@ -145,8 +148,6 @@ func (h *SyncPowerStateHandler) syncPowerState(ctx context.Context, s state.Virt } } } - - err = h.ensureRunStrategy(ctx, kvvm, virtv1.RunStrategyManual) case virtv2.AlwaysOnUnlessStoppedManually: strategy, _ := kvvm.RunStrategy() if strategy == virtv1.RunStrategyAlways && kvvmi == nil { @@ -183,8 +184,6 @@ func (h *SyncPowerStateHandler) syncPowerState(ctx context.Context, s state.Virt } } } - - err = h.ensureRunStrategy(ctx, kvvm, virtv1.RunStrategyManual) case virtv2.ManualPolicy: // Manual policy requires to handle only guest-reset event. // All types of shutdown are a final state. @@ -207,10 +206,9 @@ func (h *SyncPowerStateHandler) syncPowerState(ctx context.Context, s state.Virt } } } - - err = h.ensureRunStrategy(ctx, kvvm, virtv1.RunStrategyManual) } + err = h.ensureRunStrategy(ctx, kvvm, virtv1.RunStrategyManual) if err != nil { return fmt.Errorf("enforce runPolicy %s: %w", vmRunPolicy, err) }