Skip to content

Commit 8717e90

Browse files
committed
Add support for InstanceTerminationAction.
1 parent 67ff6d8 commit 8717e90

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

api/v1beta1/gcpmachine_types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,24 @@ type GCPMachineSpec struct {
329329
// RootDiskEncryptionKey defines the KMS key to be used to encrypt the root disk.
330330
// +optional
331331
RootDiskEncryptionKey *CustomerEncryptionKey `json:"rootDiskEncryptionKey,omitempty"`
332+
333+
// InstanceTerminationAction specifies the termination action for the instance upon preemption.
334+
// GCP API defaults to "Unspecified", which defaults the action to "Stop".
335+
// +kubebuilder:validation:Enum=Delete;Stop
336+
// +optional
337+
InstanceTerminationAction *InstanceTerminationAction `json:"instanceTerminationAction,omitempty"`
332338
}
333339

340+
// InstanceTerminationAction is a type for specifying to Delete or Stop a VM upon preemption.
341+
type InstanceTerminationAction string
342+
343+
const (
344+
// InstanceTerminationActionDelete means to delete an instance upon preemption termination.
345+
InstanceTerminationActionDelete InstanceTerminationAction = "Delete"
346+
// InstanceTerminationActionStop means to stop an instance upon preemption termination.
347+
InstanceTerminationActionStop InstanceTerminationAction = "Stop"
348+
)
349+
334350
// MetadataItem defines a single piece of metadata associated with an instance.
335351
type MetadataItem struct {
336352
// Key is the identifier for the metadata entry.

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/scope/machine.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
435435

436436
instance.Scheduling.OnHostMaintenance = strings.ToUpper(string(*m.GCPMachine.Spec.OnHostMaintenance))
437437
}
438+
if m.GCPMachine.Spec.InstanceTerminationAction != nil {
439+
switch *m.GCPMachine.Spec.InstanceTerminationAction {
440+
case infrav1.InstanceTerminationActionDelete:
441+
instance.Scheduling.InstanceTerminationAction = "DELETE"
442+
case infrav1.InstanceTerminationActionStop:
443+
instance.Scheduling.InstanceTerminationAction = "STOP"
444+
default:
445+
log.Error(errors.New("Invalid value"), "Unknown InstanceTerminationAction value", "Spec.InstanceTerminationAction", *m.GCPMachine.Spec.InstanceTerminationAction)
446+
}
447+
}
438448
if m.GCPMachine.Spec.ConfidentialCompute != nil {
439449
enabled := *m.GCPMachine.Spec.ConfidentialCompute == infrav1.ConfidentialComputePolicyEnabled
440450
instance.ConfidentialInstanceConfig = &compute.ConfidentialInstanceConfig{

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachines.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ spec:
207207
description: ImageFamily is the full reference to a valid image family
208208
to be used for this machine.
209209
type: string
210+
instanceTerminationAction:
211+
description: |-
212+
InstanceTerminationAction specifies the termination action for the instance upon preemption.
213+
GCP API defaults to "Unspecified", which defaults the action to "Stop".
214+
enum:
215+
- Delete
216+
- Stop
217+
type: string
210218
instanceType:
211219
description: 'InstanceType is the type of instance to create. Example:
212220
n1.standard-2'

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachinetemplates.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ spec:
222222
description: ImageFamily is the full reference to a valid
223223
image family to be used for this machine.
224224
type: string
225+
instanceTerminationAction:
226+
description: |-
227+
InstanceTerminationAction specifies the termination action for the instance upon preemption.
228+
GCP API defaults to "Unspecified", which defaults the action to "Stop".
229+
enum:
230+
- Delete
231+
- Stop
232+
type: string
225233
instanceType:
226234
description: 'InstanceType is the type of instance to create.
227235
Example: n1.standard-2'

0 commit comments

Comments
 (0)