Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion api/core/v1alpha2/virtual_machine_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type VirtualMachineOperation struct {
// +kubebuilder:validation:XValidation:rule="self.type == 'Migrate' ? !has(self.force) || !self.force : true",message="The `Migrate` operation cannot be performed forcibly."
// +kubebuilder:validation:XValidation:rule="self.type == 'Restore' ? has(self.restore) : true",message="Restore requires restore field."
// +kubebuilder:validation:XValidation:rule="self.type == 'Clone' ? has(self.clone) : true",message="Clone requires clone field."
// +kubebuilder:validation:XValidation:rule="!(has(self.migrate)) || self.type == 'Migrate'",message="spec.migrate can only be set when spec.type is 'Migrate'"
type VirtualMachineOperationSpec struct {
Type VMOPType `json:"type"`
// Name of the virtual machine the operation is performed for.
Expand All @@ -63,6 +64,8 @@ type VirtualMachineOperationSpec struct {
Restore *VirtualMachineOperationRestoreSpec `json:"restore,omitempty"`
// Clone defines the clone operation.
Clone *VirtualMachineOperationCloneSpec `json:"clone,omitempty"`
// Migrate defines the Migrate operation.
Migrate *VirtualMachineOperationMigrateSpec `json:"migrate,omitempty"`
}

// VirtualMachineOperationRestoreSpec defines the restore operation.
Expand All @@ -84,6 +87,14 @@ type VirtualMachineOperationCloneSpec struct {
Customization *VirtualMachineOperationCloneCustomization `json:"customization,omitempty"`
}

// VirtualMachineOperationMigrateSpec defines the restore operation.
type VirtualMachineOperationMigrateSpec struct {
// NodeSelector is a selector which must be true for the virtual machine to fit on a node.
// Selector which must match a node's labels for the virtual machine to be scheduled on
// that node.
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
}

// +kubebuilder:validation:XValidation:rule="!has(self.namePrefix) || (size(self.namePrefix) >= 1 && size(self.namePrefix) <= 59)",message="namePrefix length must be between 1 and 59 characters if set"
// +kubebuilder:validation:XValidation:rule="!has(self.nameSuffix) || (size(self.nameSuffix) >= 1 && size(self.nameSuffix) <= 59)",message="nameSuffix length must be between 1 and 59 characters if set"
// VirtualMachineOperationCloneCustomization defines customization options for cloning.
Expand Down Expand Up @@ -162,7 +173,7 @@ const (
// * `Start`: Start the virtual machine.
// * `Stop`: Stop the virtual machine.
// * `Restart`: Restart the virtual machine.
// * `Migrate` (deprecated): Migrate the virtual machine to another node where it can be started.
// * `Migrate`: Migrate the virtual machine to another node where it can be started.
// * `Evict`: Migrate the virtual machine to another node where it can be started.
// * `Restore`: Restore the virtual machine from a snapshot.
// * `Clone`: Clone the virtual machine to a new virtual machine.
Expand Down
28 changes: 28 additions & 0 deletions api/core/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion crds/virtualmachineoperations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ spec:
* Effect on `Restart` and `Stop`: operation performs immediately.
* Effect on `Evict` and `Migrate`: enable the AutoConverge feature to force migration via CPU throttling if the `PreferSafe` or `PreferForced` policies are used for live migration.
type: boolean
migrate:
description: Migrate defines the Migrate operation.
properties:
nodeSelector:
additionalProperties:
type: string
description: |-
NodeSelector is a selector which must be true for the virtual machine to fit on a node.
Selector which must match a node's labels for the virtual machine to be scheduled on
that node.
type: object
type: object
restore:
description: Restore defines the restore operation.
properties:
Expand Down Expand Up @@ -191,7 +203,7 @@ spec:
* `Start`: Start the virtual machine.
* `Stop`: Stop the virtual machine.
* `Restart`: Restart the virtual machine.
* `Migrate` (deprecated): Migrate the virtual machine to another node where it can be started.
* `Migrate`: Migrate the virtual machine to another node where it can be started.
* `Evict`: Migrate the virtual machine to another node where it can be started.
* `Restore`: Restore the virtual machine from a snapshot.
* `Clone`: Clone the virtual machine to a new virtual machine.
Expand Down Expand Up @@ -227,6 +239,8 @@ spec:
rule: "self.type == 'Restore' ? has(self.restore) : true"
- message: Clone requires clone field.
rule: "self.type == 'Clone' ? has(self.clone) : true"
- message: spec.migrate can only be set when spec.type is 'Migrate'
rule: "!(has(self.migrate)) || self.type == 'Migrate'"
status:
properties:
conditions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package service
import (
"context"
"fmt"
"maps"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -51,7 +52,7 @@ func (s MigrationService) IsApplicableForRunPolicy(runPolicy v1alpha2.RunPolicy)
}

func (s MigrationService) CreateMigration(ctx context.Context, vmop *v1alpha2.VirtualMachineOperation) error {
return client.IgnoreAlreadyExists(s.client.Create(ctx, &virtv1.VirtualMachineInstanceMigration{
vmim := &virtv1.VirtualMachineInstanceMigration{
TypeMeta: metav1.TypeMeta{
APIVersion: virtv1.SchemeGroupVersion.String(),
Kind: "VirtualMachineInstanceMigration",
Expand All @@ -73,7 +74,14 @@ func (s MigrationService) CreateMigration(ctx context.Context, vmop *v1alpha2.Vi
Spec: virtv1.VirtualMachineInstanceMigrationSpec{
VMIName: vmop.Spec.VirtualMachine,
},
}))
}

if vmop.Spec.Migrate != nil && vmop.Spec.Migrate.NodeSelector != nil {
vmim.Spec.AddedNodeSelector = make(map[string]string, len(vmop.Spec.Migrate.NodeSelector))
maps.Copy(vmim.Spec.AddedNodeSelector, vmop.Spec.Migrate.NodeSelector)
}

return client.IgnoreAlreadyExists(s.client.Create(ctx, vmim))
}

func (s MigrationService) DeleteMigration(ctx context.Context, vmop *v1alpha2.VirtualMachineOperation) error {
Expand Down
Loading