Skip to content

Commit 605ea38

Browse files
committed
Add support for Spot VMs
1 parent 67ff6d8 commit 605ea38

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-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

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
@@ -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
@@ -235,6 +235,15 @@ spec:
235235
description: ProviderID is the unique identifier as specified by the
236236
cloud provider.
237237
type: string
238+
provisioningModel:
239+
description: |-
240+
ProvisioningModel defines if instance is spot.
241+
If set to "Standard" while preemptible is true, then the VM will be of type "Preemptible".
242+
If "Spot", VM type is "Spot". When unspecified, defaults to "Standard".
243+
enum:
244+
- Standard
245+
- Spot
246+
type: string
238247
publicIP:
239248
description: |-
240249
PublicIP specifies whether the instance should get a public IP.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ spec:
250250
description: ProviderID is the unique identifier as specified
251251
by the cloud provider.
252252
type: string
253+
provisioningModel:
254+
description: |-
255+
ProvisioningModel defines if instance is spot.
256+
If set to "Standard" while preemptible is true, then the VM will be of type "Preemptible".
257+
If "Spot", VM type is "Spot". When unspecified, defaults to "Standard".
258+
enum:
259+
- Standard
260+
- Spot
261+
type: string
253262
publicIP:
254263
description: |-
255264
PublicIP specifies whether the instance should get a public IP.

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)