diff --git a/api/core/v1alpha2/virtual_machine_operation.go b/api/core/v1alpha2/virtual_machine_operation.go index aef8ba41d..7cf347eee 100644 --- a/api/core/v1alpha2/virtual_machine_operation.go +++ b/api/core/v1alpha2/virtual_machine_operation.go @@ -89,8 +89,9 @@ const ( // * Start - start the virtualmachine. // * Stop - stop the virtualmachine. // * Restart - restart the virtualmachine. -// * Migrate - migrate the virtualmachine. -// +kubebuilder:validation:Enum={Restart,Start,Stop,Migrate} +// * Migrate (deprecated) - migrate the virtualmachine. +// * Evict - evict the virtualmachine. +// +kubebuilder:validation:Enum={Restart,Start,Stop,Migrate,Evict} type VMOPType string const ( @@ -98,4 +99,5 @@ const ( 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 d3fbc9386..5609f091b 100644 --- a/crds/doc-ru-virtualmachineoperations.yaml +++ b/crds/doc-ru-virtualmachineoperations.yaml @@ -15,7 +15,8 @@ spec: * Start - запустить виртуальную машину. * Stop - остановить виртуальную машину. * Restart - перезапустить виртуальную машину. - * Migrate - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ. + * Migrate (deprecated) - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ. + * Evict - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ. virtualMachineName: description: | Имя виртуальной машины, для которой выполняется операция. diff --git a/crds/virtualmachineoperations.yaml b/crds/virtualmachineoperations.yaml index 8623e4294..6b716bd07 100644 --- a/crds/virtualmachineoperations.yaml +++ b/crds/virtualmachineoperations.yaml @@ -78,12 +78,14 @@ 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: 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..2ca1a1b35 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, 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) } @@ -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 || 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.VMOPTypeMigrate: + 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.VMOPTypeMigrate: + 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.VMOPTypeMigrate: + 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..ff0a21d3c 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, consider using Evict operation"}, nil + } + + return admission.Warnings{}, nil } func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {