From 6d135afdeb2ca4ff621238ec9caecacb152bc304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Fortin?= Date: Thu, 12 Nov 2026 11:34:21 -0500 Subject: [PATCH] Make gcp automatic zone selection similar to AWS' --- CHANGELOG.md | 2 ++ docs/README.md | 17 +++++++++-- gcp/infrastructure.tf | 65 +++++++++++++++++++++++++++++++++++++------ gcp/versions.tf | 6 ++++ 4 files changed, 78 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ea4ab1b2..16cfc9d28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Fixed - [AWS] Persisted the selected availability zone to prevent unintended cluster rebuilds when AWS zone or instance-type offerings change. (PR #458) +- [GCP] Persisted the selected zone to prevent unintended instance and disk rebuilds when the available zone list changes, and rejected unavailable zones instead of silently selecting another zone. +- [GCP] Kept all instances and managed disks in one zone offering every requested instance type, including autoscaling nodes, and validated the stored zone against current offerings. Requires Google provider 5.15 or later. ## [16.1.2] 2026-11-04 diff --git a/docs/README.md b/docs/README.md index fce956508..0dc2bcd77 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1171,18 +1171,29 @@ assigned number. Defines the name of the specific geographical location where the cluster resources will be hosted. -**Requirement**: Must be a valid Google Cloud region. Refer to [Google Cloud documentation](https://cloud.google.com/compute/docs/regions-zones#available) +**Requirement**: Must be a Google Cloud region with an available zone offering +all selected instance types. Custom N1 machine types are checked by +family. Refer to [Google Cloud documentation](https://cloud.google.com/compute/docs/regions-zones#available) for the list of available regions and their characteristics. #### 5.3.3 zone (optional) **default value**: None -Defines the name of the zone within the region where the cluster resources will be hosted. +Defines the zone within the region where all cluster instances and managed disks +will be hosted. If omitted, a zone offering all selected instance types is chosen +at random. The selected zone is stored in Terraform state and retained when zone +or machine-type offerings change. An unavailable or incompatible zone causes an +error instead of selecting another zone. -**Requirement**: Must be a valid Google Cloud zone. Refer to [Google Cloud documentation](https://cloud.google.com/compute/docs/regions-zones#available) +**Requirement**: Must be an available Google Cloud zone in the selected region. +It must offer all selected instance types, including autoscaling nodes. +Refer to [Google Cloud documentation](https://cloud.google.com/compute/docs/regions-zones#available) for the list of available zones and their characteristics. +**Post build modification effect**: changing or clearing this value resets the +selection and may rebuild instances and managed disks at next `terraform apply`. + ### 5.4 OpenStack and OVH #### 5.4.1 os_floating_ips (optional) diff --git a/gcp/infrastructure.tf b/gcp/infrastructure.tf index 34e94dca2..3f99eef54 100644 --- a/gcp/infrastructure.tf +++ b/gcp/infrastructure.tf @@ -51,20 +51,67 @@ data "google_compute_zones" "available" { status = "UP" } +locals { + instance_types = distinct([for instance in var.instances : instance.type]) + # Custom N1 shapes are not listed by the API; check their machine family. + machine_type_names = { + for type in local.instance_types : type => startswith(type, "custom-") ? "n1-standard-1" : type + } +} + +data "google_compute_machine_types" "available" { + for_each = toset(data.google_compute_zones.available.names) + project = var.project + zone = each.key + filter = join(" OR ", [for type in distinct(values(local.machine_type_names)) : "name = ${jsonencode(type)}"]) +} + +locals { + zone_choices = toset([ + for zone, offerings in data.google_compute_machine_types.available : zone + if alltrue([ + for type in local.instance_types : contains(offerings.machine_types[*].name, local.machine_type_names[type]) + ]) + ]) +} + resource "random_shuffle" "random_zone" { - input = data.google_compute_zones.available.names + input = local.zone_choices result_count = 1 } locals { - zone = ( - (var.zone != "" && - contains(data.google_compute_zones.available.names, - var.zone) - ? - var.zone : random_shuffle.random_zone.result[0] - ) - ) + zone = terraform_data.zone.output +} + +resource "terraform_data" "zone" { + input = var.zone != "" ? var.zone : random_shuffle.random_zone.result[0] + triggers_replace = [var.zone] + + lifecycle { + ignore_changes = [input] + + precondition { + condition = length(data.google_compute_zones.available.names) > 0 + error_message = "There are no available zones in ${var.region}." + } + precondition { + condition = length(local.zone_choices) > 0 + error_message = "There is not a single available zone in ${var.region} that provides all selected instance types ${jsonencode(local.instance_types)}." + } + precondition { + condition = var.zone == "" || contains(data.google_compute_zones.available.names, var.zone) + error_message = "The selected zone ${var.zone} is not available in ${var.region}. Select one of ${jsonencode(data.google_compute_zones.available.names)} or leave zone undefined." + } + precondition { + condition = var.zone == "" || contains(local.zone_choices, var.zone) + error_message = "The selected zone ${var.zone} does not provide all selected instance types. Select one of ${jsonencode(local.zone_choices)} or leave zone undefined." + } + postcondition { + condition = contains(local.zone_choices, self.output) + error_message = "The cluster zone ${self.output} is not available or does not provide all selected instance types. Select instance types available in this zone or explicitly change zone to one of ${jsonencode(local.zone_choices)}." + } + } } data "external" "machine_type" { diff --git a/gcp/versions.tf b/gcp/versions.tf index ce57ed791..af6657327 100644 --- a/gcp/versions.tf +++ b/gcp/versions.tf @@ -1,4 +1,10 @@ terraform { required_version = ">= 1.5.7" + required_providers { + google = { + source = "hashicorp/google" + version = ">= 5.15.0" + } + } }