Skip to content

Commit

Permalink
Add support for Spot VMs
Browse files Browse the repository at this point in the history
  • Loading branch information
jwmay2012 committed Sep 19, 2024
1 parent 67ff6d8 commit 576b0db
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/v1beta1/gcpmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ type CustomerEncryptionKey struct {
// +optional
SuppliedKey *SuppliedKey `json:"suppliedKey,omitempty"`
}
type ProvisioningModel string

Check failure on line 219 in api/v1beta1/gcpmachine_types.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type ProvisioningModel should have comment or be unexported (revive)

const (
ProvisioningModelStandard ProvisioningModel = "Standard"

Check failure on line 222 in api/v1beta1/gcpmachine_types.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported const ProvisioningModelStandard should have comment (or a comment on this block) or be unexported (revive)
ProvisioningModelSpot ProvisioningModel = "Spot"
)

// GCPMachineSpec defines the desired state of GCPMachine.
type GCPMachineSpec struct {
Expand Down Expand Up @@ -302,6 +308,13 @@ type GCPMachineSpec struct {
// +optional
Preemptible bool `json:"preemptible,omitempty"`

// ProvisioningModel defines if instance is spot.
// If set to "Standard" while preemptible is true, then the VM will be of type "Preemptible".
// If "Spot", VM type is "Spot". When unspecified, defaults to "Standard".
// +kubebuilder:validation:Enum=Standard;Spot
// +optional
ProvisioningModel *ProvisioningModel `json:"provisioningModel,omitempty"`

// IPForwarding Allows this instance to send and receive packets with non-matching destination or source IPs.
// This is required if you plan to use this instance to forward routes. Defaults to enabled.
// +kubebuilder:validation:Enum=Enabled;Disabled
Expand Down
10 changes: 10 additions & 0 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
Preemptible: m.GCPMachine.Spec.Preemptible,
},
}
if m.GCPMachine.Spec.ProvisioningModel != nil {
switch *m.GCPMachine.Spec.ProvisioningModel {
case infrav1.ProvisioningModelSpot:
instance.Scheduling.ProvisioningModel = "SPOT"
case infrav1.ProvisioningModelStandard:
instance.Scheduling.ProvisioningModel = "STANDARD"
default:
log.Error(errors.New("Invalid value"), "Unknown ProvisioningModel value", "Spec.ProvisioningModel", *m.GCPMachine.Spec.ProvisioningModel)
}
}

instance.CanIpForward = true
if m.GCPMachine.Spec.IPForwarding != nil && *m.GCPMachine.Spec.IPForwarding == infrav1.IPForwardingDisabled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ spec:
preemptible:
description: Preemptible defines if instance is preemptible
type: boolean
provisioningModel:
description: |-
ProvisioningModel defines if instance is spot.
If set to "Standard" while preemptible is true, then the VM will be of type "Preemptible".
If "Spot", VM type is "Spot". When unspecified, defaults to "Standard".
enum:
- Standard
- Spot
type: string
providerID:
description: ProviderID is the unique identifier as specified by the
cloud provider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ spec:
preemptible:
description: Preemptible defines if instance is preemptible
type: boolean
provisioningModel:
description: |-
ProvisioningModel defines if instance is spot.
If set to "Standard" while preemptible is true, then the VM will be of type "Preemptible".
If "Spot", VM type is "Spot". When unspecified, defaults to "Standard".
enum:
- Standard
- Spot
type: string
providerID:
description: ProviderID is the unique identifier as specified
by the cloud provider.
Expand Down
22 changes: 22 additions & 0 deletions docs/book/src/topics/preemptible-vms.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@ spec:
vmSize: E2
preemptible: true
```
## Spot VMs
[Spot VMs are the latest version of preemptible VMs.](https://cloud.google.com/compute/docs/instances/spot)
To use a Spot VM instead of a Preemptible VM, add `provisioningModel` to `GCPMachineTemplate` and set it to `Spot`.

```yaml
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: GCPMachineTemplate
metadata:
name: capg-md-0
spec:
region: us-west-1
template:
osDisk:
diskSizeGB: 30
managedDisk:
storageAccountType: STANDARD
osType: Linux
vmSize: E2
provisioningModel: Spot
```

0 comments on commit 576b0db

Please sign in to comment.