Merge pull request #646 from GoogleCloudPlatform/sruffilli/spot-vm

Adds Spot VM support to compute-vm
This commit is contained in:
Julio Castillo
2022-05-04 19:08:23 +02:00
committed by GitHub
8 changed files with 53 additions and 13 deletions

View File

@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
## Unreleased
- add support for [Spot VMs](https://cloud.google.com/compute/docs/instances/spot) to `gke-nodepool` module
- **incompatible change** add support for [Spot VMs](https://cloud.google.com/compute/docs/instances/spot) to `compute-vm` module
- SQL Server AlwaysOn availability groups example
- fixed Terraform change detection in CloudSQL when backup is disabled
- allow multiple CIDR blocks in the ip_range for Apigee Instance

View File

@@ -60,6 +60,7 @@ module "nva-template-ew1" {
deletion_protection = false
# Creates preemptible instances, cheaper than regular one. Only suitable for testing.
preemptible = true
spot = true
}
metadata = {
startup-script = templatefile(

View File

@@ -31,6 +31,33 @@ module "simple-vm-example" {
```
### Spot VM
[Spot VMs](https://cloud.google.com/compute/docs/instances/spot) are ephemeral compute instances suitable for batch jobs and fault-tolerant workloads. Spot VMs provide new features that [preemptible instances](https://cloud.google.com/compute/docs/instances/preemptible) do not support, such as the absence of a maximum runtime.
```hcl
module "spot-vm-example" {
source = "./modules/compute-vm"
project_id = var.project_id
zone = "europe-west1-b"
name = "test"
options = {
allow_stopping_for_update = true
deletion_protection = false
spot = true
}
network_interfaces = [{
network = var.vpc.self_link
subnetwork = var.subnet.self_link
nat = false
addresses = null
}]
service_account_create = true
}
# tftest modules=1 resources=2
```
### Disk sources
Attached disks can be created and optionally initialized from a pre-existing source, or attached to VMs when pre-existing. The `source` and `source_type` attributes of the `attached_disks` variable allows several modes of operation:
@@ -320,7 +347,7 @@ module "instance-group" {
| [metadata](variables.tf#L148) | Instance metadata. | <code>map&#40;string&#41;</code> | | <code>&#123;&#125;</code> |
| [min_cpu_platform](variables.tf#L154) | Minimum CPU platform. | <code>string</code> | | <code>null</code> |
| [network_interface_options](variables.tf#L165) | Network interfaces extended options. The key is the index of the inteface to configure. The value is an object with alias_ips and nic_type. Set alias_ips or nic_type to null if you need only one of them. | <code title="map&#40;object&#40;&#123;&#10; alias_ips &#61; map&#40;string&#41;&#10; nic_type &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [options](variables.tf#L187) | Instance options. | <code title="object&#40;&#123;&#10; allow_stopping_for_update &#61; bool&#10; deletion_protection &#61; bool&#10; preemptible &#61; bool&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; allow_stopping_for_update &#61; true&#10; deletion_protection &#61; false&#10; preemptible &#61; false&#10;&#125;">&#123;&#8230;&#125;</code> |
| [options](variables.tf#L187) | Instance options. | <code title="object&#40;&#123;&#10; allow_stopping_for_update &#61; bool&#10; deletion_protection &#61; bool&#10; spot &#61; bool&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; allow_stopping_for_update &#61; true&#10; deletion_protection &#61; false&#10; spot &#61; false&#10;&#125;">&#123;&#8230;&#125;</code> |
| [scratch_disks](variables.tf#L206) | Scratch disks configuration. | <code title="object&#40;&#123;&#10; count &#61; number&#10; interface &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; count &#61; 0&#10; interface &#61; &#34;NVME&#34;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [service_account](variables.tf#L218) | Service account email. Unused if service account is auto-created. | <code>string</code> | | <code>null</code> |
| [service_account_create](variables.tf#L224) | Auto-create service account. | <code>bool</code> | | <code>false</code> |

View File

@@ -30,7 +30,7 @@ locals {
k => v if try(v.options.replica_zone, null) == null
}
on_host_maintenance = (
var.options.preemptible || var.confidential_compute
var.options.spot || var.confidential_compute
? "TERMINATE"
: "MIGRATE"
)
@@ -212,9 +212,10 @@ resource "google_compute_instance" "default" {
}
scheduling {
automatic_restart = !var.options.preemptible
automatic_restart = !var.options.spot
on_host_maintenance = local.on_host_maintenance
preemptible = var.options.preemptible
preemptible = var.options.spot
provisioning_model = var.options.spot ? "SPOT" : "STANDARD"
}
dynamic "scratch_disk" {
@@ -338,9 +339,10 @@ resource "google_compute_instance_template" "default" {
}
scheduling {
automatic_restart = !var.options.preemptible
automatic_restart = !var.options.spot
on_host_maintenance = local.on_host_maintenance
preemptible = var.options.preemptible
preemptible = var.options.spot
provisioning_model = var.options.spot ? "SPOT" : "STANDARD"
}
service_account {

View File

@@ -189,12 +189,12 @@ variable "options" {
type = object({
allow_stopping_for_update = bool
deletion_protection = bool
preemptible = bool
spot = bool
})
default = {
allow_stopping_for_update = true
deletion_protection = false
preemptible = false
spot = false
}
}

View File

@@ -41,7 +41,7 @@ module "cluster-1-nodepool-1" {
|---|---|:---:|:---:|:---:|
| [cluster_name](variables.tf#L26) | Cluster name. | <code>string</code> | ✓ | |
| [location](variables.tf#L59) | Cluster location. | <code>string</code> | ✓ | |
| [project_id](variables.tf#L210) | Cluster project id. | <code>string</code> | ✓ | |
| [project_id](variables.tf#L216) | Cluster project id. | <code>string</code> | ✓ | |
| [autoscaling_config](variables.tf#L17) | Optional autoscaling configuration. | <code title="object&#40;&#123;&#10; min_node_count &#61; number&#10; max_node_count &#61; number&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [gke_version](variables.tf#L31) | Kubernetes nodes version. Ignored if auto_upgrade is set in management_config. | <code>string</code> | | <code>null</code> |
| [initial_node_count](variables.tf#L37) | Initial number of nodes for the pool. | <code>number</code> | | <code>1</code> |
@@ -68,10 +68,11 @@ module "cluster-1-nodepool-1" {
| [node_service_account_create](variables.tf#L174) | Auto-create service account. | <code>bool</code> | | <code>false</code> |
| [node_service_account_scopes](variables.tf#L182) | Scopes applied to service account. Default to: 'cloud-platform' when creating a service account; 'devstorage.read_only', 'logging.write', 'monitoring.write' otherwise. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
| [node_shielded_instance_config](variables.tf#L188) | Shielded instance options. | <code title="object&#40;&#123;&#10; enable_secure_boot &#61; bool&#10; enable_integrity_monitoring &#61; bool&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [node_tags](variables.tf#L197) | Network tags applied to nodes. | <code>list&#40;string&#41;</code> | | <code>null</code> |
| [node_taints](variables.tf#L203) | Kubernetes taints applied to nodes. E.g. type=blue:NoSchedule. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
| [upgrade_config](variables.tf#L215) | Optional node upgrade configuration. | <code title="object&#40;&#123;&#10; max_surge &#61; number&#10; max_unavailable &#61; number&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [workload_metadata_config](variables.tf#L224) | Metadata configuration to expose to workloads on the node pool. | <code>string</code> | | <code>&#34;GKE_METADATA&#34;</code> |
| [node_spot](variables.tf#L197) | Use Spot VMs for nodes. | <code>bool</code> | | <code>null</code> |
| [node_tags](variables.tf#L203) | Network tags applied to nodes. | <code>list&#40;string&#41;</code> | | <code>null</code> |
| [node_taints](variables.tf#L209) | Kubernetes taints applied to nodes. E.g. type=blue:NoSchedule. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
| [upgrade_config](variables.tf#L221) | Optional node upgrade configuration. | <code title="object&#40;&#123;&#10; max_surge &#61; number&#10; max_unavailable &#61; number&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [workload_metadata_config](variables.tf#L230) | Metadata configuration to expose to workloads on the node pool. | <code>string</code> | | <code>&#34;GKE_METADATA&#34;</code> |
## Outputs

View File

@@ -105,6 +105,7 @@ resource "google_container_node_pool" "nodepool" {
service_account = local.service_account_email
tags = var.node_tags
boot_disk_kms_key = var.node_boot_disk_kms_key
spot = var.node_spot
dynamic "guest_accelerator" {
for_each = var.node_guest_accelerator

View File

@@ -194,6 +194,12 @@ variable "node_shielded_instance_config" {
default = null
}
variable "node_spot" {
description = "Use Spot VMs for nodes."
type = bool
default = null
}
variable "node_tags" {
description = "Network tags applied to nodes."
type = list(string)