Skip to content

Commit

Permalink
Add support for InstanceTerminationAction.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwmay2012 committed Sep 24, 2024
1 parent 67ff6d8 commit 8717e90
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions api/v1beta1/gcpmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

10 changes: 10 additions & 0 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 8717e90

Please sign in to comment.