From 3562d3378b81c815cfebd3e871d411310ed73065 Mon Sep 17 00:00:00 2001 From: Mathieu Parent Date: Mon, 31 Jan 2022 09:30:24 +0100 Subject: [PATCH] terraform/gcp: Allow to use preemptible VM instances (#8480) --- contrib/terraform/gcp/README.md | 4 ++++ contrib/terraform/gcp/main.tf | 10 ++++++---- .../terraform/gcp/modules/kubernetes-cluster/main.tf | 10 ++++++++++ .../gcp/modules/kubernetes-cluster/variables.tf | 8 ++++++++ contrib/terraform/gcp/variables.tf | 10 ++++++++++ 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/contrib/terraform/gcp/README.md b/contrib/terraform/gcp/README.md index b036c5b71..a7f23b354 100644 --- a/contrib/terraform/gcp/README.md +++ b/contrib/terraform/gcp/README.md @@ -80,8 +80,12 @@ ansible-playbook -i contrib/terraform/gcs/inventory.ini cluster.yml -b -v * `prefix`: Prefix to use for all resources, required to be unique for all clusters in the same project *(Defaults to `default`)* * `master_sa_email`: Service account email to use for the master nodes *(Defaults to `""`, auto generate one)* * `master_sa_scopes`: Service account email to use for the master nodes *(Defaults to `["https://www.googleapis.com/auth/cloud-platform"]`)* +* `master_preemptible`: Enable [preemptible](https://cloud.google.com/compute/docs/instances/preemptible) + for the master nodes *(Defaults to `false`)* * `worker_sa_email`: Service account email to use for the worker nodes *(Defaults to `""`, auto generate one)* * `worker_sa_scopes`: Service account email to use for the worker nodes *(Defaults to `["https://www.googleapis.com/auth/cloud-platform"]`)* +* `worker_preemptible`: Enable [preemptible](https://cloud.google.com/compute/docs/instances/preemptible) + for the worker nodes *(Defaults to `false`)* An example variables file can be found `tfvars.json` diff --git a/contrib/terraform/gcp/main.tf b/contrib/terraform/gcp/main.tf index 0367d49b6..ce221d0c1 100644 --- a/contrib/terraform/gcp/main.tf +++ b/contrib/terraform/gcp/main.tf @@ -21,10 +21,12 @@ module "kubernetes" { machines = var.machines ssh_pub_key = var.ssh_pub_key - master_sa_email = var.master_sa_email - master_sa_scopes = var.master_sa_scopes - worker_sa_email = var.worker_sa_email - worker_sa_scopes = var.worker_sa_scopes + master_sa_email = var.master_sa_email + master_sa_scopes = var.master_sa_scopes + master_preemptible = var.master_preemptible + worker_sa_email = var.worker_sa_email + worker_sa_scopes = var.worker_sa_scopes + worker_preemptible = var.worker_preemptible ssh_whitelist = var.ssh_whitelist api_server_whitelist = var.api_server_whitelist diff --git a/contrib/terraform/gcp/modules/kubernetes-cluster/main.tf b/contrib/terraform/gcp/modules/kubernetes-cluster/main.tf index 9e0d32637..a9cbacbaa 100644 --- a/contrib/terraform/gcp/modules/kubernetes-cluster/main.tf +++ b/contrib/terraform/gcp/modules/kubernetes-cluster/main.tf @@ -231,6 +231,11 @@ resource "google_compute_instance" "master" { lifecycle { ignore_changes = [attached_disk] } + + scheduling { + preemptible = var.master_preemptible + automatic_restart = !var.master_preemptible + } } resource "google_compute_forwarding_rule" "master_lb" { @@ -328,6 +333,11 @@ resource "google_compute_instance" "worker" { lifecycle { ignore_changes = [attached_disk] } + + scheduling { + preemptible = var.worker_preemptible + automatic_restart = !var.worker_preemptible + } } resource "google_compute_address" "worker_lb" { diff --git a/contrib/terraform/gcp/modules/kubernetes-cluster/variables.tf b/contrib/terraform/gcp/modules/kubernetes-cluster/variables.tf index d6632ac4b..5fddca26d 100644 --- a/contrib/terraform/gcp/modules/kubernetes-cluster/variables.tf +++ b/contrib/terraform/gcp/modules/kubernetes-cluster/variables.tf @@ -27,6 +27,10 @@ variable "master_sa_scopes" { type = list(string) } +variable "master_preemptible" { + type = bool +} + variable "worker_sa_email" { type = string } @@ -35,6 +39,10 @@ variable "worker_sa_scopes" { type = list(string) } +variable "worker_preemptible" { + type = bool +} + variable "ssh_pub_key" {} variable "ssh_whitelist" { diff --git a/contrib/terraform/gcp/variables.tf b/contrib/terraform/gcp/variables.tf index 9850e61c5..207a80321 100644 --- a/contrib/terraform/gcp/variables.tf +++ b/contrib/terraform/gcp/variables.tf @@ -44,6 +44,11 @@ variable "master_sa_scopes" { default = ["https://www.googleapis.com/auth/cloud-platform"] } +variable "master_preemptible" { + type = bool + default = false +} + variable "worker_sa_email" { type = string default = "" @@ -54,6 +59,11 @@ variable "worker_sa_scopes" { default = ["https://www.googleapis.com/auth/cloud-platform"] } +variable "worker_preemptible" { + type = bool + default = false +} + variable ssh_pub_key { description = "Path to public SSH key file which is injected into the VMs." type = string