Skip to content

Commit 9cec971

Browse files
committed
Add support for Spot VMs
1 parent 67ff6d8 commit 9cec971

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

api/v1beta1/gcpmachine_types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ type CustomerEncryptionKey struct {
217217
SuppliedKey *SuppliedKey `json:"suppliedKey,omitempty"`
218218
}
219219

220+
// ProvisioningModel is a type for Spot VM enablement.
221+
type ProvisioningModel string
222+
223+
const (
224+
// ProvisioningModelStandard specifies the VM type to NOT be Spot.
225+
ProvisioningModelStandard ProvisioningModel = "Standard"
226+
// ProvisioningModelSpot specifies the VM type to be Spot.
227+
ProvisioningModelSpot ProvisioningModel = "Spot"
228+
)
229+
220230
// GCPMachineSpec defines the desired state of GCPMachine.
221231
type GCPMachineSpec struct {
222232
// InstanceType is the type of instance to create. Example: n1.standard-2
@@ -302,6 +312,13 @@ type GCPMachineSpec struct {
302312
// +optional
303313
Preemptible bool `json:"preemptible,omitempty"`
304314

315+
// ProvisioningModel defines if instance is spot.
316+
// If set to "Standard" while preemptible is true, then the VM will be of type "Preemptible".
317+
// If "Spot", VM type is "Spot". When unspecified, defaults to "Standard".
318+
// +kubebuilder:validation:Enum=Standard;Spot
319+
// +optional
320+
ProvisioningModel *ProvisioningModel `json:"provisioningModel,omitempty"`
321+
305322
// IPForwarding Allows this instance to send and receive packets with non-matching destination or source IPs.
306323
// This is required if you plan to use this instance to forward routes. Defaults to enabled.
307324
// +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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,27 @@ 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+
```
53+
54+
NOTE: specifying `preemptible: true` and `provisioningModel: Spot` is equivalent to only `provisioningModel: Spot`. Spot takes priority.

0 commit comments

Comments
 (0)