From a04792a7fb10600d6f21649a6edd86e487b464e7 Mon Sep 17 00:00:00 2001 From: yaroslavborbat Date: Tue, 22 Oct 2024 14:31:25 +0300 Subject: [PATCH 1/5] rename Signed-off-by: yaroslavborbat --- api/core/v1alpha2/virtual_machine_operation.go | 4 ++-- crds/virtualmachineoperations.yaml | 2 +- .../pkg/controller/service/vm_operation.go | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/api/core/v1alpha2/virtual_machine_operation.go b/api/core/v1alpha2/virtual_machine_operation.go index aef8ba41d..19ab68dd5 100644 --- a/api/core/v1alpha2/virtual_machine_operation.go +++ b/api/core/v1alpha2/virtual_machine_operation.go @@ -90,12 +90,12 @@ const ( // * Stop - stop the virtualmachine. // * Restart - restart the virtualmachine. // * Migrate - migrate the virtualmachine. -// +kubebuilder:validation:Enum={Restart,Start,Stop,Migrate} +// +kubebuilder:validation:Enum={Restart,Start,Stop,Evict} type VMOPType string const ( VMOPTypeRestart VMOPType = "Restart" VMOPTypeStart VMOPType = "Start" VMOPTypeStop VMOPType = "Stop" - VMOPTypeMigrate VMOPType = "Migrate" + VMOPTypeEvict VMOPType = "Evict" ) diff --git a/crds/virtualmachineoperations.yaml b/crds/virtualmachineoperations.yaml index 8623e4294..e4e018eb4 100644 --- a/crds/virtualmachineoperations.yaml +++ b/crds/virtualmachineoperations.yaml @@ -83,7 +83,7 @@ spec: - Restart - Start - Stop - - Migrate + - Evict type: string virtualMachineName: description: diff --git a/images/virtualization-artifact/pkg/controller/service/vm_operation.go b/images/virtualization-artifact/pkg/controller/service/vm_operation.go index 189442210..c2dd03cc5 100644 --- a/images/virtualization-artifact/pkg/controller/service/vm_operation.go +++ b/images/virtualization-artifact/pkg/controller/service/vm_operation.go @@ -63,8 +63,8 @@ func (s VMOperationService) Do(ctx context.Context, vmop *virtv2.VirtualMachineO return s.DoStop(ctx, vmop.GetNamespace(), vmop.Spec.VirtualMachine, vmop.Spec.Force) case virtv2.VMOPTypeRestart: return s.DoRestart(ctx, vmop.GetNamespace(), vmop.Spec.VirtualMachine, vmop.Spec.Force) - case virtv2.VMOPTypeMigrate: - return s.DoMigrate(ctx, vmop.GetNamespace(), vmop.Spec.VirtualMachine) + case virtv2.VMOPTypeEvict: + return s.DoEvict(ctx, vmop.GetNamespace(), vmop.Spec.VirtualMachine) default: return fmt.Errorf("unexpected operation type %q: %w", vmop.Spec.Type, common.ErrUnknownValue) } @@ -98,7 +98,7 @@ func (s VMOperationService) DoRestart(ctx context.Context, vmNamespace, vmName s return powerstate.RestartVM(ctx, s.client, kvvm, kvvmi, force) } -func (s VMOperationService) DoMigrate(ctx context.Context, vmNamespace, vmName string) error { +func (s VMOperationService) DoEvict(ctx context.Context, vmNamespace, vmName string) error { err := s.virtClient.VirtualMachines(vmNamespace).Migrate(ctx, vmName, v1alpha2.VirtualMachineMigrate{}) if err != nil { return fmt.Errorf(`failed to migrate virtual machine "%s/%s": %w`, vmNamespace, vmName, err) @@ -116,7 +116,7 @@ func (s VMOperationService) IsAllowedForVM(vmop *virtv2.VirtualMachineOperation, func (s VMOperationService) IsApplicableForRunPolicy(vmop *virtv2.VirtualMachineOperation, runPolicy virtv2.RunPolicy) bool { switch runPolicy { case virtv2.AlwaysOnPolicy: - return vmop.Spec.Type == virtv2.VMOPTypeRestart || vmop.Spec.Type == virtv2.VMOPTypeMigrate + return vmop.Spec.Type == virtv2.VMOPTypeRestart || vmop.Spec.Type == virtv2.VMOPTypeEvict case virtv2.AlwaysOffPolicy: return false case virtv2.ManualPolicy, virtv2.AlwaysOnUnlessStoppedManually: @@ -140,7 +140,7 @@ func (s VMOperationService) IsApplicableForVMPhase(vmop *virtv2.VirtualMachineOp phase == virtv2.MachineDegraded || phase == virtv2.MachineStarting || phase == virtv2.MachinePause - case virtv2.VMOPTypeMigrate: + case virtv2.VMOPTypeEvict: return phase == virtv2.MachineRunning default: return false @@ -183,7 +183,7 @@ func (s VMOperationService) InProgressReasonForType(vmop *virtv2.VirtualMachineO return vmopcondition.ReasonStopInProgress case virtv2.VMOPTypeRestart: return vmopcondition.ReasonRestartInProgress - case virtv2.VMOPTypeMigrate: + case virtv2.VMOPTypeEvict: return vmopcondition.ReasonMigrationInProgress } return vmopcondition.ReasonCompletedUnknown @@ -210,7 +210,7 @@ func (s VMOperationService) IsComplete(ctx context.Context, vmop *virtv2.Virtual return kvvmi != nil && vmPhase == virtv2.MachineRunning && s.isAfterSignalSentOrCreation(kvvmi.GetCreationTimestamp().Time, vmop), nil - case virtv2.VMOPTypeMigrate: + case virtv2.VMOPTypeEvict: kvvmi, err := s.getKVVMI(ctx, vmop.GetNamespace(), vmop.Spec.VirtualMachine) if err != nil { return false, err From 9f7442df76d20b9650da7bdcb997b521ecb192bc Mon Sep 17 00:00:00 2001 From: yaroslavborbat Date: Tue, 22 Oct 2024 14:50:46 +0300 Subject: [PATCH 2/5] fix ru-doc Signed-off-by: yaroslavborbat --- crds/doc-ru-virtualmachineoperations.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crds/doc-ru-virtualmachineoperations.yaml b/crds/doc-ru-virtualmachineoperations.yaml index d3fbc9386..63aa55e4c 100644 --- a/crds/doc-ru-virtualmachineoperations.yaml +++ b/crds/doc-ru-virtualmachineoperations.yaml @@ -15,7 +15,7 @@ spec: * Start - запустить виртуальную машину. * Stop - остановить виртуальную машину. * Restart - перезапустить виртуальную машину. - * Migrate - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ. + * Evict - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ. virtualMachineName: description: | Имя виртуальной машины, для которой выполняется операция. From ab377b8c2fa17c0f8ef471c1282053c96d939ee3 Mon Sep 17 00:00:00 2001 From: yaroslavborbat Date: Mon, 28 Oct 2024 18:55:21 +0300 Subject: [PATCH 3/5] fix Signed-off-by: yaroslavborbat --- api/core/v1alpha2/virtual_machine_operation.go | 6 ++++-- crds/doc-ru-virtualmachineoperations.yaml | 1 + crds/virtualmachineoperations.yaml | 4 +++- .../pkg/controller/service/vm_operation.go | 10 +++++----- .../pkg/controller/vmop/vmop_webhook.go | 16 ++++++++++++---- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/api/core/v1alpha2/virtual_machine_operation.go b/api/core/v1alpha2/virtual_machine_operation.go index 19ab68dd5..7cf347eee 100644 --- a/api/core/v1alpha2/virtual_machine_operation.go +++ b/api/core/v1alpha2/virtual_machine_operation.go @@ -89,13 +89,15 @@ const ( // * Start - start the virtualmachine. // * Stop - stop the virtualmachine. // * Restart - restart the virtualmachine. -// * Migrate - migrate the virtualmachine. -// +kubebuilder:validation:Enum={Restart,Start,Stop,Evict} +// * Migrate (deprecated) - migrate the virtualmachine. +// * Evict - evict the virtualmachine. +// +kubebuilder:validation:Enum={Restart,Start,Stop,Migrate,Evict} type VMOPType string const ( VMOPTypeRestart VMOPType = "Restart" VMOPTypeStart VMOPType = "Start" VMOPTypeStop VMOPType = "Stop" + VMOPTypeMigrate VMOPType = "Migrate" VMOPTypeEvict VMOPType = "Evict" ) diff --git a/crds/doc-ru-virtualmachineoperations.yaml b/crds/doc-ru-virtualmachineoperations.yaml index 63aa55e4c..5609f091b 100644 --- a/crds/doc-ru-virtualmachineoperations.yaml +++ b/crds/doc-ru-virtualmachineoperations.yaml @@ -15,6 +15,7 @@ spec: * Start - запустить виртуальную машину. * Stop - остановить виртуальную машину. * Restart - перезапустить виртуальную машину. + * Migrate (deprecated) - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ. * Evict - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ. virtualMachineName: description: | diff --git a/crds/virtualmachineoperations.yaml b/crds/virtualmachineoperations.yaml index e4e018eb4..6b716bd07 100644 --- a/crds/virtualmachineoperations.yaml +++ b/crds/virtualmachineoperations.yaml @@ -78,11 +78,13 @@ spec: * Start - start the virtualmachine. * Stop - stop the virtualmachine. * Restart - restart the virtualmachine. - * Migrate - migrate the virtualmachine. + * Migrate (deprecated) - migrate the virtualmachine. + * Evict - evict the virtualmachine. enum: - Restart - Start - Stop + - Migrate - Evict type: string virtualMachineName: diff --git a/images/virtualization-artifact/pkg/controller/service/vm_operation.go b/images/virtualization-artifact/pkg/controller/service/vm_operation.go index c2dd03cc5..2ca1a1b35 100644 --- a/images/virtualization-artifact/pkg/controller/service/vm_operation.go +++ b/images/virtualization-artifact/pkg/controller/service/vm_operation.go @@ -63,7 +63,7 @@ func (s VMOperationService) Do(ctx context.Context, vmop *virtv2.VirtualMachineO return s.DoStop(ctx, vmop.GetNamespace(), vmop.Spec.VirtualMachine, vmop.Spec.Force) case virtv2.VMOPTypeRestart: return s.DoRestart(ctx, vmop.GetNamespace(), vmop.Spec.VirtualMachine, vmop.Spec.Force) - case virtv2.VMOPTypeEvict: + case virtv2.VMOPTypeEvict, virtv2.VMOPTypeMigrate: return s.DoEvict(ctx, vmop.GetNamespace(), vmop.Spec.VirtualMachine) default: return fmt.Errorf("unexpected operation type %q: %w", vmop.Spec.Type, common.ErrUnknownValue) @@ -116,7 +116,7 @@ func (s VMOperationService) IsAllowedForVM(vmop *virtv2.VirtualMachineOperation, func (s VMOperationService) IsApplicableForRunPolicy(vmop *virtv2.VirtualMachineOperation, runPolicy virtv2.RunPolicy) bool { switch runPolicy { case virtv2.AlwaysOnPolicy: - return vmop.Spec.Type == virtv2.VMOPTypeRestart || vmop.Spec.Type == virtv2.VMOPTypeEvict + return vmop.Spec.Type == virtv2.VMOPTypeRestart || vmop.Spec.Type == virtv2.VMOPTypeEvict || vmop.Spec.Type == virtv2.VMOPTypeMigrate case virtv2.AlwaysOffPolicy: return false case virtv2.ManualPolicy, virtv2.AlwaysOnUnlessStoppedManually: @@ -140,7 +140,7 @@ func (s VMOperationService) IsApplicableForVMPhase(vmop *virtv2.VirtualMachineOp phase == virtv2.MachineDegraded || phase == virtv2.MachineStarting || phase == virtv2.MachinePause - case virtv2.VMOPTypeEvict: + case virtv2.VMOPTypeEvict, virtv2.VMOPTypeMigrate: return phase == virtv2.MachineRunning default: return false @@ -183,7 +183,7 @@ func (s VMOperationService) InProgressReasonForType(vmop *virtv2.VirtualMachineO return vmopcondition.ReasonStopInProgress case virtv2.VMOPTypeRestart: return vmopcondition.ReasonRestartInProgress - case virtv2.VMOPTypeEvict: + case virtv2.VMOPTypeEvict, virtv2.VMOPTypeMigrate: return vmopcondition.ReasonMigrationInProgress } return vmopcondition.ReasonCompletedUnknown @@ -210,7 +210,7 @@ func (s VMOperationService) IsComplete(ctx context.Context, vmop *virtv2.Virtual return kvvmi != nil && vmPhase == virtv2.MachineRunning && s.isAfterSignalSentOrCreation(kvvmi.GetCreationTimestamp().Time, vmop), nil - case virtv2.VMOPTypeEvict: + case virtv2.VMOPTypeEvict, virtv2.VMOPTypeMigrate: kvvmi, err := s.getKVVMI(ctx, vmop.GetNamespace(), vmop.Spec.VirtualMachine) if err != nil { return false, err diff --git a/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go b/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go index 38d8ff164..86bf2cb67 100644 --- a/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go +++ b/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go @@ -37,10 +37,18 @@ type Validator struct { log *slog.Logger } -func (v *Validator) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) { - err := fmt.Errorf("misconfigured webhook rules: create operation not implemented") - v.log.Error("Ensure the correctness of ValidatingWebhookConfiguration", "err", err) - return nil, nil +func (v *Validator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) { + vmop, ok := obj.(*v1alpha2.VirtualMachineOperation) + if !ok { + return nil, fmt.Errorf("expected a new VirtualMachineOperation but got a %T", obj) + } + + //TODO: Delete me after v0.15 + if vmop.Spec.Type == v1alpha2.VMOPTypeMigrate { + return admission.Warnings{"The Migrate type is deprecated"}, nil + } + + return admission.Warnings{}, nil } func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { From a75068c0cebfef980ab386dc28a102f914214023 Mon Sep 17 00:00:00 2001 From: yaroslavborbat Date: Mon, 28 Oct 2024 19:04:27 +0300 Subject: [PATCH 4/5] fix Signed-off-by: yaroslavborbat --- .../virtualization-artifact/pkg/controller/vmop/vmop_webhook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go b/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go index 86bf2cb67..be897ca95 100644 --- a/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go +++ b/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go @@ -43,7 +43,7 @@ func (v *Validator) ValidateCreate(_ context.Context, obj runtime.Object) (admis return nil, fmt.Errorf("expected a new VirtualMachineOperation but got a %T", obj) } - //TODO: Delete me after v0.15 + // TODO: Delete me after v0.15 if vmop.Spec.Type == v1alpha2.VMOPTypeMigrate { return admission.Warnings{"The Migrate type is deprecated"}, nil } From b74750f39411ead3bebca68d896aa71c7ddc5268 Mon Sep 17 00:00:00 2001 From: Yaroslav Borbat <86148689+yaroslavborbat@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:19:18 +0300 Subject: [PATCH 5/5] Update images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go Co-authored-by: Ivan Mikheykin Signed-off-by: Yaroslav Borbat <86148689+yaroslavborbat@users.noreply.github.com> --- .../virtualization-artifact/pkg/controller/vmop/vmop_webhook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go b/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go index be897ca95..ff0a21d3c 100644 --- a/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go +++ b/images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go @@ -45,7 +45,7 @@ func (v *Validator) ValidateCreate(_ context.Context, obj runtime.Object) (admis // TODO: Delete me after v0.15 if vmop.Spec.Type == v1alpha2.VMOPTypeMigrate { - return admission.Warnings{"The Migrate type is deprecated"}, nil + return admission.Warnings{"The Migrate type is deprecated, consider using Evict operation"}, nil } return admission.Warnings{}, nil