Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
Signed-off-by: dmitry.lopatin <[email protected]>
  • Loading branch information
LopatinDmitr committed Dec 11, 2024
1 parent 01d4cb6 commit 58f108f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 75 deletions.
28 changes: 17 additions & 11 deletions api/core/v1alpha2/vmcondition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ func (t Type) String() string {
}

const (
TypeIPAddressReady Type = "VirtualMachineIPAddressReady"
TypeClassReady Type = "VirtualMachineClassReady"
TypeBlockDevicesReady Type = "BlockDevicesReady"
TypeRunning Type = "Running"
TypeMigrating Type = "Migrating"
TypeMigratable Type = "Migratable"
TypePodStarted Type = "PodStarted"
TypeProvisioningReady Type = "ProvisioningReady"
TypeAgentReady Type = "AgentReady"
TypeAgentVersionNotSupported Type = "AgentVersionNotSupported"
TypeConfigurationApplied Type = "ConfigurationApplied"
TypeIPAddressReady Type = "VirtualMachineIPAddressReady"
TypeClassReady Type = "VirtualMachineClassReady"
TypeBlockDevicesReady Type = "BlockDevicesReady"
TypeRunning Type = "Running"
TypeMigrating Type = "Migrating"
TypeMigratable Type = "Migratable"
TypePodStarted Type = "PodStarted"
TypeProvisioningReady Type = "ProvisioningReady"
TypeAgentReady Type = "AgentReady"
TypeAgentVersionNotSupported Type = "AgentVersionNotSupported"

// TypeConfigurationApplied indicates status to the synchronization of the KVVM configuration.
TypeConfigurationApplied Type = "ConfigurationApplied"

// TypePowerConfigurationApplied indicates status to the synchronization of the power state.
TypePowerConfigurationApplied Type = "PowerConfigurationApplied"

TypeAwaitingRestartToApplyConfiguration Type = "AwaitingRestartToApplyConfiguration"
TypeFilesystemReady Type = "FilesystemReady"
TypeSizingPolicyMatched Type = "SizingPolicyMatched"
Expand Down
21 changes: 2 additions & 19 deletions images/virtualization-artifact/pkg/controller/kvbuilder/kvvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,8 @@ func (b *KVVM) SetCPUModel(class *virtv2.VirtualMachineClass) error {
return nil
}

func (b *KVVM) SetRunPolicy(runPolicy virtv2.RunPolicy) error {
switch runPolicy {
case virtv2.AlwaysOnPolicy:
b.Resource.Spec.RunStrategy = pointer.GetPointer(virtv1.RunStrategyAlways)
case virtv2.AlwaysOffPolicy:
b.Resource.Spec.RunStrategy = pointer.GetPointer(virtv1.RunStrategyHalted)
case virtv2.ManualPolicy:
if !b.ResourceExists {
// initialize only
b.Resource.Spec.RunStrategy = pointer.GetPointer(virtv1.RunStrategyManual)
}
case virtv2.AlwaysOnUnlessStoppedManually:
if !b.ResourceExists {
// initialize only
b.Resource.Spec.RunStrategy = pointer.GetPointer(virtv1.RunStrategyAlways)
}
default:
return fmt.Errorf("unexpected runPolicy %s. %w", runPolicy, common.ErrUnknownValue)
}
func (b *KVVM) SetRunPolicy() error {
b.Resource.Spec.RunStrategy = pointer.GetPointer(virtv1.RunStrategyManual)
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func ApplyVirtualMachineSpec(
class *virtv2.VirtualMachineClass,
ipAddress string,
) error {
if err := kvvm.SetRunPolicy(vm.Spec.RunPolicy); err != nil {
if err := kvvm.SetRunPolicy(); err != nil {
return err
}
if err := kvvm.SetOsType(vm.Spec.OsType); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

kvvmutil "github.com/deckhouse/virtualization-controller/pkg/common/kvvm"
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/powerstate"
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
Expand Down Expand Up @@ -60,51 +59,55 @@ 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 {
return nil
return fmt.Errorf("kvvm is nil")
}

kvvmi, err := s.KVVMI(ctx)
if err != nil {
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
Expand All @@ -119,10 +122,8 @@ 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 {
if kvvmi == nil {
if err = powerstate.StartVM(ctx, h.client, kvvm); err != nil {
return fmt.Errorf("failed to start VM: %w", err)
}
Expand All @@ -145,13 +146,13 @@ 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 {
if err = powerstate.StartVM(ctx, h.client, kvvm); err != nil {
return fmt.Errorf("failed to start VM: %w", err)
if kvvmi == nil {
cond, ok := conditions.GetCondition(vmcondition.TypePowerConfigurationApplied, s.VirtualMachine().Changed().Status.Conditions)
if !ok || cond.Status != metav1.ConditionTrue {
if err = powerstate.StartVM(ctx, h.client, kvvm); err != nil {
return fmt.Errorf("failed to start VM: %w", err)
}
}
}
if kvvmi != nil && kvvmi.DeletionTimestamp == nil {
Expand Down Expand Up @@ -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.
Expand All @@ -207,30 +206,6 @@ func (h *SyncPowerStateHandler) syncPowerState(ctx context.Context, s state.Virt
}
}
}

err = h.ensureRunStrategy(ctx, kvvm, virtv1.RunStrategyManual)
}

if err != nil {
return fmt.Errorf("enforce runPolicy %s: %w", vmRunPolicy, err)
}

return nil
}

func (h *SyncPowerStateHandler) ensureRunStrategy(ctx context.Context, kvvm *virtv1.VirtualMachine, desiredRunStrategy virtv1.VirtualMachineRunStrategy) error {
if kvvm == nil {
return nil
}
kvvmRunStrategy := kvvmutil.GetRunStrategy(kvvm)

if kvvmRunStrategy == desiredRunStrategy {
return nil
}
patch := kvvmutil.PatchRunStrategy(desiredRunStrategy)
err := h.client.Patch(ctx, kvvm, patch)
if err != nil {
return fmt.Errorf("patch KVVM with runStrategy %s: %w", desiredRunStrategy, err)
}

return nil
Expand Down

0 comments on commit 58f108f

Please sign in to comment.