From 8717e90eab2dd3b1c963e060d3afc1c07b7bf656 Mon Sep 17 00:00:00 2001 From: Jordan May Date: Thu, 19 Sep 2024 11:43:29 -0400 Subject: [PATCH] Add support for InstanceTerminationAction. --- api/v1beta1/gcpmachine_types.go | 16 ++++++++++++++++ api/v1beta1/zz_generated.deepcopy.go | 5 +++++ cloud/scope/machine.go | 10 ++++++++++ ...rastructure.cluster.x-k8s.io_gcpmachines.yaml | 8 ++++++++ ...ure.cluster.x-k8s.io_gcpmachinetemplates.yaml | 8 ++++++++ 5 files changed, 47 insertions(+) diff --git a/api/v1beta1/gcpmachine_types.go b/api/v1beta1/gcpmachine_types.go index 7e27e7845..5a09c579a 100644 --- a/api/v1beta1/gcpmachine_types.go +++ b/api/v1beta1/gcpmachine_types.go @@ -329,8 +329,24 @@ type GCPMachineSpec struct { // RootDiskEncryptionKey defines the KMS key to be used to encrypt the root disk. // +optional RootDiskEncryptionKey *CustomerEncryptionKey `json:"rootDiskEncryptionKey,omitempty"` + + // InstanceTerminationAction specifies the termination action for the instance upon preemption. + // GCP API defaults to "Unspecified", which defaults the action to "Stop". + // +kubebuilder:validation:Enum=Delete;Stop + // +optional + InstanceTerminationAction *InstanceTerminationAction `json:"instanceTerminationAction,omitempty"` } +// InstanceTerminationAction is a type for specifying to Delete or Stop a VM upon preemption. +type InstanceTerminationAction string + +const ( + // InstanceTerminationActionDelete means to delete an instance upon preemption termination. + InstanceTerminationActionDelete InstanceTerminationAction = "Delete" + // InstanceTerminationActionStop means to stop an instance upon preemption termination. + InstanceTerminationActionStop InstanceTerminationAction = "Stop" +) + // MetadataItem defines a single piece of metadata associated with an instance. type MetadataItem struct { // Key is the identifier for the metadata entry. diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index 78c0f8571..8e7e4e7ad 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -500,6 +500,11 @@ func (in *GCPMachineSpec) DeepCopyInto(out *GCPMachineSpec) { *out = new(CustomerEncryptionKey) (*in).DeepCopyInto(*out) } + if in.InstanceTerminationAction != nil { + in, out := &in.InstanceTerminationAction, &out.InstanceTerminationAction + *out = new(InstanceTerminationAction) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GCPMachineSpec. diff --git a/cloud/scope/machine.go b/cloud/scope/machine.go index 96f745822..0a9d660c7 100644 --- a/cloud/scope/machine.go +++ b/cloud/scope/machine.go @@ -435,6 +435,16 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance { instance.Scheduling.OnHostMaintenance = strings.ToUpper(string(*m.GCPMachine.Spec.OnHostMaintenance)) } + if m.GCPMachine.Spec.InstanceTerminationAction != nil { + switch *m.GCPMachine.Spec.InstanceTerminationAction { + case infrav1.InstanceTerminationActionDelete: + instance.Scheduling.InstanceTerminationAction = "DELETE" + case infrav1.InstanceTerminationActionStop: + instance.Scheduling.InstanceTerminationAction = "STOP" + default: + log.Error(errors.New("Invalid value"), "Unknown InstanceTerminationAction value", "Spec.InstanceTerminationAction", *m.GCPMachine.Spec.InstanceTerminationAction) + } + } if m.GCPMachine.Spec.ConfidentialCompute != nil { enabled := *m.GCPMachine.Spec.ConfidentialCompute == infrav1.ConfidentialComputePolicyEnabled instance.ConfidentialInstanceConfig = &compute.ConfidentialInstanceConfig{ diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachines.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachines.yaml index 24955802d..5461a3a8a 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachines.yaml @@ -207,6 +207,14 @@ spec: description: ImageFamily is the full reference to a valid image family to be used for this machine. type: string + instanceTerminationAction: + description: |- + InstanceTerminationAction specifies the termination action for the instance upon preemption. + GCP API defaults to "Unspecified", which defaults the action to "Stop". + enum: + - Delete + - Stop + type: string instanceType: description: 'InstanceType is the type of instance to create. Example: n1.standard-2' diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachinetemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachinetemplates.yaml index 5cc470597..84c3de22c 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachinetemplates.yaml @@ -222,6 +222,14 @@ spec: description: ImageFamily is the full reference to a valid image family to be used for this machine. type: string + instanceTerminationAction: + description: |- + InstanceTerminationAction specifies the termination action for the instance upon preemption. + GCP API defaults to "Unspecified", which defaults the action to "Stop". + enum: + - Delete + - Stop + type: string instanceType: description: 'InstanceType is the type of instance to create. Example: n1.standard-2'