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

Add support for InstanceTerminationAction for instance preemption. #1315

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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