Skip to content

Commit 576b0db

Browse files
committed
Add support for Spot VMs
1 parent 67ff6d8 commit 576b0db

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

api/v1beta1/gcpmachine_types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ type CustomerEncryptionKey struct {
216216
// +optional
217217
SuppliedKey *SuppliedKey `json:"suppliedKey,omitempty"`
218218
}
219+
type ProvisioningModel string
220+
221+
const (
222+
ProvisioningModelStandard ProvisioningModel = "Standard"
223+
ProvisioningModelSpot ProvisioningModel = "Spot"
224+
)
219225

220226
// GCPMachineSpec defines the desired state of GCPMachine.
221227
type GCPMachineSpec struct {
@@ -302,6 +308,13 @@ type GCPMachineSpec struct {
302308
// +optional
303309
Preemptible bool `json:"preemptible,omitempty"`
304310

311+
// ProvisioningModel defines if instance is spot.
312+
// If set to "Standard" while preemptible is true, then the VM will be of type "Preemptible".
313+
// If "Spot", VM type is "Spot". When unspecified, defaults to "Standard".
314+
// +kubebuilder:validation:Enum=Standard;Spot
315+
// +optional
316+
ProvisioningModel *ProvisioningModel `json:"provisioningModel,omitempty"`
317+
305318
// IPForwarding Allows this instance to send and receive packets with non-matching destination or source IPs.
306319
// This is required if you plan to use this instance to forward routes. Defaults to enabled.
307320
// +kubebuilder:validation:Enum=Enabled;Disabled

cloud/scope/machine.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,16 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
402402
Preemptible: m.GCPMachine.Spec.Preemptible,
403403
},
404404
}
405+
if m.GCPMachine.Spec.ProvisioningModel != nil {
406+
switch *m.GCPMachine.Spec.ProvisioningModel {
407+
case infrav1.ProvisioningModelSpot:
408+
instance.Scheduling.ProvisioningModel = "SPOT"
409+
case infrav1.ProvisioningModelStandard:
410+
instance.Scheduling.ProvisioningModel = "STANDARD"
411+
default:
412+
log.Error(errors.New("Invalid value"), "Unknown ProvisioningModel value", "Spec.ProvisioningModel", *m.GCPMachine.Spec.ProvisioningModel)
413+
}
414+
}
405415

406416
instance.CanIpForward = true
407417
if m.GCPMachine.Spec.IPForwarding != nil && *m.GCPMachine.Spec.IPForwarding == infrav1.IPForwardingDisabled {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ spec:
231231
preemptible:
232232
description: Preemptible defines if instance is preemptible
233233
type: boolean
234+
provisioningModel:
235+
description: |-
236+
ProvisioningModel defines if instance is spot.
237+
If set to "Standard" while preemptible is true, then the VM will be of type "Preemptible".
238+
If "Spot", VM type is "Spot". When unspecified, defaults to "Standard".
239+
enum:
240+
- Standard
241+
- Spot
242+
type: string
234243
providerID:
235244
description: ProviderID is the unique identifier as specified by the
236245
cloud provider.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ spec:
246246
preemptible:
247247
description: Preemptible defines if instance is preemptible
248248
type: boolean
249+
provisioningModel:
250+
description: |-
251+
ProvisioningModel defines if instance is spot.
252+
If set to "Standard" while preemptible is true, then the VM will be of type "Preemptible".
253+
If "Spot", VM type is "Spot". When unspecified, defaults to "Standard".
254+
enum:
255+
- Standard
256+
- Spot
257+
type: string
249258
providerID:
250259
description: ProviderID is the unique identifier as specified
251260
by the cloud provider.

docs/book/src/topics/preemptible-vms.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,25 @@ spec:
2828
vmSize: E2
2929
preemptible: true
3030
```
31+
32+
## Spot VMs
33+
[Spot VMs are the latest version of preemptible VMs.](https://cloud.google.com/compute/docs/instances/spot)
34+
35+
To use a Spot VM instead of a Preemptible VM, add `provisioningModel` to `GCPMachineTemplate` and set it to `Spot`.
36+
37+
```yaml
38+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
39+
kind: GCPMachineTemplate
40+
metadata:
41+
name: capg-md-0
42+
spec:
43+
region: us-west-1
44+
template:
45+
osDisk:
46+
diskSizeGB: 30
47+
managedDisk:
48+
storageAccountType: STANDARD
49+
osType: Linux
50+
vmSize: E2
51+
provisioningModel: Spot
52+
```

0 commit comments

Comments
 (0)