Skip to content

Commit

Permalink
feat(vmop): rename type migrate to evict (#463)
Browse files Browse the repository at this point in the history
add the "Evict" type
the "Migrate" type is deprecated
---------
Signed-off-by: yaroslavborbat <[email protected]>
Co-authored-by: Ivan Mikheykin <[email protected]>
  • Loading branch information
yaroslavborbat authored Oct 28, 2024
1 parent b429542 commit e599b02
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
6 changes: 4 additions & 2 deletions api/core/v1alpha2/virtual_machine_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,Migrate}
// * 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"
)
3 changes: 2 additions & 1 deletion crds/doc-ru-virtualmachineoperations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ spec:
* Start - запустить виртуальную машину.
* Stop - остановить виртуальную машину.
* Restart - перезапустить виртуальную машину.
* Migrate - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ.
* Migrate (deprecated) - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ.
* Evict - мигрировать виртуальную машину на другой узел, доступный для запуска данной ВМ.
virtualMachineName:
description: |
Имя виртуальной машины, для которой выполняется операция.
Expand Down
4 changes: 3 additions & 1 deletion crds/virtualmachineoperations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions images/virtualization-artifact/pkg/controller/vmop/vmop_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e599b02

Please sign in to comment.