Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vmop): add operation to migrate vm #386

Merged
merged 10 commits into from
Oct 1, 2024
Merged
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
1 change: 1 addition & 0 deletions api/client/kubeclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type VirtualMachineInterface interface {
PortForward(name string, opts v1alpha2.VirtualMachinePortForward) (StreamInterface, error)
Freeze(ctx context.Context, name string, opts v1alpha2.VirtualMachineFreeze) error
Unfreeze(ctx context.Context, name string) error
Migrate(ctx context.Context, name string, opts v1alpha2.VirtualMachineMigrate) error
}

type client struct {
Expand Down
4 changes: 2 additions & 2 deletions api/client/kubeclient/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"k8s.io/client-go/rest"
)

const operationURLTpl = "/apis/subresources.virtualization.deckhouse.io/v1alpha2/namespaces/%s/%s/%s/%s"
const subresourceURLTpl = "/apis/subresources.virtualization.deckhouse.io/v1alpha2/namespaces/%s/%s/%s/%s"

func RequestFromConfig(config *rest.Config, resource, name, namespace, subresource string,
queryParams url.Values) (*http.Request, error) {
Expand All @@ -48,7 +48,7 @@ func RequestFromConfig(config *rest.Config, resource, name, namespace, subresour

u.Path = path.Join(
u.Path,
fmt.Sprintf(operationURLTpl, namespace, resource, name, subresource),
fmt.Sprintf(subresourceURLTpl, namespace, resource, name, subresource),
)
if len(queryParams) > 0 {
u.RawQuery = queryParams.Encode()
Expand Down
26 changes: 15 additions & 11 deletions api/client/kubeclient/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (v vm) PortForward(name string, opts v1alpha2.VirtualMachinePortForward) (S
}

func (v vm) Freeze(ctx context.Context, name string, opts v1alpha2.VirtualMachineFreeze) error {
path := fmt.Sprintf(operationURLTpl, v.namespace, v.resource, name, "freeze")
path := fmt.Sprintf(subresourceURLTpl, v.namespace, v.resource, name, "freeze")

unfreezeTimeout := virtv1.FreezeUnfreezeTimeout{
UnfreezeTimeout: &metav1.Duration{},
Expand All @@ -127,21 +127,25 @@ func (v vm) Freeze(ctx context.Context, name string, opts v1alpha2.VirtualMachin
return err
}

err = v.restClient.Put().AbsPath(path).Body(body).Do(ctx).Error()
if err != nil {
return err
}

return nil
return v.restClient.Put().AbsPath(path).Body(body).Do(ctx).Error()
}

func (v vm) Unfreeze(ctx context.Context, name string) error {
path := fmt.Sprintf(operationURLTpl, v.namespace, v.resource, name, "unfreeze")
path := fmt.Sprintf(subresourceURLTpl, v.namespace, v.resource, name, "unfreeze")

return v.restClient.Put().AbsPath(path).Do(ctx).Error()
}

func (v vm) Migrate(ctx context.Context, name string, opts v1alpha2.VirtualMachineMigrate) error {
path := fmt.Sprintf(subresourceURLTpl, v.namespace, v.resource, name, "migrate")

err := v.restClient.Put().AbsPath(path).Do(ctx).Error()
migrateOpts := virtv1.MigrateOptions{
DryRun: opts.DryRun,
}

body, err := json.Marshal(&migrateOpts)
if err != nil {
return err
}

return nil
return v.restClient.Put().AbsPath(path).Body(body).Do(ctx).Error()
}
42 changes: 35 additions & 7 deletions api/core/v1alpha2/virtual_machine_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ const (
VMOPResource = "virtualmachineoperations"
)

// VirtualMachineOperation is operation performed on the VirtualMachine.
// VirtualMachineOperation resource provides the ability to declaratively manage state changes of virtual machines.
// +kubebuilder:object:root=true
// +kubebuilder:metadata:labels={heritage=deckhouse,module=virtualization}
// +kubebuilder:subresource:status
// +kubebuilder:resource:categories={virtualization,all},scope=Namespaced,shortName={vmop,vmops},singular=virtualmachineoperation
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="VirtualMachineOperation phase."
// +kubebuilder:printcolumn:name="Type",type="string",JSONPath=".spec.type",description="VirtualMachineOperation type."
// +kubebuilder:printcolumn:name="VirtualMachine",type="string",JSONPath=".spec.virtualMachineName",description="VirtualMachine name."
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time of creation resource."
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type VirtualMachineOperation struct {
Expand All @@ -34,16 +42,22 @@ type VirtualMachineOperation struct {
Status VirtualMachineOperationStatus `json:"status,omitempty"`
}

// +kubebuilder:validation:XValidation:rule="self.type == 'Start' ? !has(self.force) || !self.force : true",message="The `Start` operation cannot be performed forcibly."
// +kubebuilder:validation:XValidation:rule="self.type == 'Migrate' ? !has(self.force) || !self.force : true",message="The `Migrate` operation cannot be performed forcibly."
type VirtualMachineOperationSpec struct {
Type VMOPType `json:"type"`
VirtualMachine string `json:"virtualMachineName"`
Force bool `json:"force,omitempty"`
Type VMOPType `json:"type"`
// The name of the virtual machine for which the operation is performed.
VirtualMachine string `json:"virtualMachineName"`
// Force the execution of the operation. Applies only for Restart and Stop. In this case, the action on the virtual machine is performed immediately.
Force bool `json:"force,omitempty"`
}

type VirtualMachineOperationStatus struct {
Phase VMOPPhase `json:"phase"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
Phase VMOPPhase `json:"phase"`
// The latest detailed observations of the VirtualMachineOperation resource.
Conditions []metav1.Condition `json:"conditions,omitempty"`
// The generation last processed by the controller.
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
}

// VirtualMachineOperationList contains a list of VirtualMachineOperation
Expand All @@ -54,6 +68,13 @@ type VirtualMachineOperationList struct {
Items []VirtualMachineOperation `json:"items"`
}

// Represents the current phase of resource:
// * Pending - the operation is queued for execution.
// * InProgress - operation in progress.
// * Completed - the operation was successful.
// * Failed - the operation failed. Check conditions and events for more information.
// * Terminating - the operation is deleted.
// +kubebuilder:validation:Enum={Pending,InProgress,Completed,Failed,Terminating}
type VMOPPhase string

const (
Expand All @@ -64,10 +85,17 @@ const (
VMOPPhaseTerminating VMOPPhase = "Terminating"
)

// Type is operation over the virtualmachine:
// * Start - start the virtualmachine.
// * Stop - stop the virtualmachine.
// * Restart - restart the virtualmachine.
// * Migrate - migrate the virtualmachine.
// +kubebuilder:validation:Enum={Restart,Start,Stop,Migrate}
type VMOPType string

const (
VMOPTypeRestart VMOPType = "Restart"
VMOPTypeStart VMOPType = "Start"
VMOPTypeStop VMOPType = "Stop"
VMOPTypeMigrate VMOPType = "Migrate"
)
3 changes: 3 additions & 0 deletions api/core/v1alpha2/vmopcondition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const (
// ReasonStopInProgress is a ReasonCompleted indicating that the stop signal has been sent and stop is in progress.
ReasonStopInProgress ReasonCompleted = "StopInProgress"

// ReasonMigrationInProgress is a ReasonCompleted indicating that the migrate signal has been sent and stop is in progress.
ReasonMigrationInProgress ReasonCompleted = "MigrationInProgress"

// ReasonOperationFailed is a ReasonCompleted indicating that operation has failed.
ReasonOperationFailed ReasonCompleted = "OperationFailed"

Expand Down
63 changes: 54 additions & 9 deletions api/pkg/apiserver/api/generated/openapi/zz_generated.openapi.go

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

4 changes: 3 additions & 1 deletion api/scripts/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function source::settings {
MODULE="github.com/deckhouse/virtualization/api"
PREFIX_GROUP="virtualization.deckhouse.io_"
# TODO: Temporary filter until all CRDs become auto-generated.
ALLOWED_RESOURCE_GEN_CRD=("VirtualMachineClass" "VirtualMachineBlockDeviceAttachment" "ExampleKind1" "ExampleKind2")
ALLOWED_RESOURCE_GEN_CRD=("VirtualMachineClass"
"VirtualMachineBlockDeviceAttachment"
"VirtualMachineOperation")
source "${CODEGEN_PKG}/kube_codegen.sh"
}

Expand Down
1 change: 1 addition & 0 deletions api/subresources/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&VirtualMachineRemoveVolume{},
&VirtualMachineFreeze{},
&VirtualMachineUnfreeze{},
&VirtualMachineMigrate{},
&virtv2.VirtualMachine{},
&virtv2.VirtualMachineList{},
)
Expand Down
9 changes: 9 additions & 0 deletions api/subresources/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ type VirtualMachineFreeze struct {
type VirtualMachineUnfreeze struct {
metav1.TypeMeta
}

// +genclient
// +genclient:readonly
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type VirtualMachineMigrate struct {
metav1.TypeMeta
DryRun []string
}
1 change: 1 addition & 0 deletions api/subresources/v1alpha2/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&VirtualMachineRemoveVolume{},
&VirtualMachineFreeze{},
&VirtualMachineUnfreeze{},
&VirtualMachineMigrate{},
&virtv2.VirtualMachine{},
&virtv2.VirtualMachineList{},
)
Expand Down
10 changes: 10 additions & 0 deletions api/subresources/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ type VirtualMachineFreeze struct {
type VirtualMachineUnfreeze struct {
metav1.TypeMeta `json:",inline"`
}

// +genclient
// +genclient:readonly
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:conversion-gen:explicit-from=net/url.Values

type VirtualMachineMigrate struct {
metav1.TypeMeta `json:",inline"`
DryRun []string `json:"dryRun,omitempty"`
}
51 changes: 51 additions & 0 deletions api/subresources/v1alpha2/zz_generated.conversion.go

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

Loading