Skip to content

Commit 6a16a8f

Browse files
committed
fix: random zones only when zones are not provided
1 parent fd233e5 commit 6a16a8f

File tree

20 files changed

+95
-55
lines changed

20 files changed

+95
-55
lines changed

autogen/main/cluster.tf.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ resource "google_container_cluster" "primary" {
8383
disabled = var.disable_default_snat
8484
}
8585

86-
min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
86+
min_master_version = var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
8787

8888
{% if beta_cluster and autopilot_cluster != true %}
8989
dynamic "cluster_telemetry" {
@@ -649,7 +649,7 @@ resource "google_container_node_pool" "windows_pools" {
649649
project = var.project_id
650650
location = local.location
651651
// use node_locations if provided, defaults to cluster level node_locations if not specified
652-
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
652+
node_locations = local.node_locations
653653

654654
cluster = google_container_cluster.primary.name
655655

autogen/main/main.tf.tmpl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Get available zones in region
2121
*****************************************/
2222
data "google_compute_zones" "available" {
23+
count = length(var.zones) == 0 ? 1 : 0
24+
2325
{% if beta_cluster %}
2426
provider = google-beta
2527
{% else %}
@@ -31,7 +33,9 @@ data "google_compute_zones" "available" {
3133
}
3234

3335
resource "random_shuffle" "available_zones" {
34-
input = data.google_compute_zones.available.names
36+
count = length(var.zones) == 0 ? 1 : 0
37+
38+
input = data.google_compute_zones.available[0].names
3539
result_count = 3
3640
}
3741

@@ -43,7 +47,7 @@ locals {
4347
location = var.regional ? var.region : var.zones[0]
4448
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
4549
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
46-
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
50+
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
4751
// Kubernetes version
4852
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
4953
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
@@ -245,6 +249,6 @@ data "google_container_engine_versions" "zone" {
245249
//
246250
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
247251
//
248-
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
252+
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names : var.zones[0]
249253
project = var.project_id
250254
}

cluster.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ resource "google_container_cluster" "primary" {
6969
disabled = var.disable_default_snat
7070
}
7171

72-
min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
72+
min_master_version = var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
7373

7474
# only one of logging/monitoring_service or logging/monitoring_config can be specified
7575
logging_service = local.logmon_config_is_set ? null : var.logging_service
@@ -371,7 +371,7 @@ resource "google_container_node_pool" "pools" {
371371
project = var.project_id
372372
location = local.location
373373
// use node_locations if provided, defaults to cluster level node_locations if not specified
374-
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
374+
node_locations = local.node_locations
375375

376376
cluster = google_container_cluster.primary.name
377377

@@ -557,7 +557,7 @@ resource "google_container_node_pool" "windows_pools" {
557557
project = var.project_id
558558
location = local.location
559559
// use node_locations if provided, defaults to cluster level node_locations if not specified
560-
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
560+
node_locations = local.node_locations
561561

562562
cluster = google_container_cluster.primary.name
563563

main.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
Get available zones in region
2121
*****************************************/
2222
data "google_compute_zones" "available" {
23+
count = length(var.zones) == 0 ? 1 : 0
24+
2325
provider = google
2426

2527
project = var.project_id
2628
region = local.region
2729
}
2830

2931
resource "random_shuffle" "available_zones" {
30-
input = data.google_compute_zones.available.names
32+
count = length(var.zones) == 0 ? 1 : 0
33+
34+
input = data.google_compute_zones.available[0].names
3135
result_count = 3
3236
}
3337

@@ -39,7 +43,7 @@ locals {
3943
location = var.regional ? var.region : var.zones[0]
4044
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
4145
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
42-
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
46+
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
4347
// Kubernetes version
4448
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
4549
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
@@ -175,6 +179,6 @@ data "google_container_engine_versions" "zone" {
175179
//
176180
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
177181
//
178-
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
182+
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names : var.zones[0]
179183
project = var.project_id
180184
}

modules/beta-autopilot-private-cluster/cluster.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ resource "google_container_cluster" "primary" {
6767
disabled = var.disable_default_snat
6868
}
6969

70-
min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
70+
min_master_version = var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
7171

7272
cluster_autoscaling {
7373
dynamic "auto_provisioning_defaults" {

modules/beta-autopilot-private-cluster/main.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
Get available zones in region
2121
*****************************************/
2222
data "google_compute_zones" "available" {
23+
count = length(var.zones) == 0 ? 1 : 0
24+
2325
provider = google-beta
2426

2527
project = var.project_id
2628
region = local.region
2729
}
2830

2931
resource "random_shuffle" "available_zones" {
30-
input = data.google_compute_zones.available.names
32+
count = length(var.zones) == 0 ? 1 : 0
33+
34+
input = data.google_compute_zones.available[0].names
3135
result_count = 3
3236
}
3337

@@ -39,7 +43,7 @@ locals {
3943
location = var.regional ? var.region : var.zones[0]
4044
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
4145
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
42-
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
46+
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
4347
// Kubernetes version
4448
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
4549
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
@@ -143,6 +147,6 @@ data "google_container_engine_versions" "zone" {
143147
//
144148
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
145149
//
146-
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
150+
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names : var.zones[0]
147151
project = var.project_id
148152
}

modules/beta-autopilot-public-cluster/cluster.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ resource "google_container_cluster" "primary" {
6767
disabled = var.disable_default_snat
6868
}
6969

70-
min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
70+
min_master_version = var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
7171

7272
cluster_autoscaling {
7373
dynamic "auto_provisioning_defaults" {

modules/beta-autopilot-public-cluster/main.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
Get available zones in region
2121
*****************************************/
2222
data "google_compute_zones" "available" {
23+
count = length(var.zones) == 0 ? 1 : 0
24+
2325
provider = google-beta
2426

2527
project = var.project_id
2628
region = local.region
2729
}
2830

2931
resource "random_shuffle" "available_zones" {
30-
input = data.google_compute_zones.available.names
32+
count = length(var.zones) == 0 ? 1 : 0
33+
34+
input = data.google_compute_zones.available[0].names
3135
result_count = 3
3236
}
3337

@@ -39,7 +43,7 @@ locals {
3943
location = var.regional ? var.region : var.zones[0]
4044
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
4145
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
42-
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
46+
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
4347
// Kubernetes version
4448
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
4549
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
@@ -142,6 +146,6 @@ data "google_container_engine_versions" "zone" {
142146
//
143147
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
144148
//
145-
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
149+
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names : var.zones[0]
146150
project = var.project_id
147151
}

modules/beta-private-cluster-update-variant/cluster.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ resource "google_container_cluster" "primary" {
7575
disabled = var.disable_default_snat
7676
}
7777

78-
min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
78+
min_master_version = var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
7979

8080
dynamic "cluster_telemetry" {
8181
for_each = local.cluster_telemetry_type_is_set ? [1] : []
@@ -553,7 +553,7 @@ resource "google_container_node_pool" "pools" {
553553
project = var.project_id
554554
location = local.location
555555
// use node_locations if provided, defaults to cluster level node_locations if not specified
556-
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
556+
node_locations = local.node_locations
557557

558558
cluster = google_container_cluster.primary.name
559559

@@ -779,7 +779,7 @@ resource "google_container_node_pool" "windows_pools" {
779779
project = var.project_id
780780
location = local.location
781781
// use node_locations if provided, defaults to cluster level node_locations if not specified
782-
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
782+
node_locations = local.node_locations
783783

784784
cluster = google_container_cluster.primary.name
785785

modules/beta-private-cluster-update-variant/main.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
Get available zones in region
2121
*****************************************/
2222
data "google_compute_zones" "available" {
23+
count = length(var.zones) == 0 ? 1 : 0
24+
2325
provider = google-beta
2426

2527
project = var.project_id
2628
region = local.region
2729
}
2830

2931
resource "random_shuffle" "available_zones" {
30-
input = data.google_compute_zones.available.names
32+
count = length(var.zones) == 0 ? 1 : 0
33+
34+
input = data.google_compute_zones.available[0].names
3135
result_count = 3
3236
}
3337

@@ -39,7 +43,7 @@ locals {
3943
location = var.regional ? var.region : var.zones[0]
4044
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
4145
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
42-
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
46+
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
4347
// Kubernetes version
4448
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
4549
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
@@ -203,6 +207,6 @@ data "google_container_engine_versions" "zone" {
203207
//
204208
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
205209
//
206-
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
210+
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names : var.zones[0]
207211
project = var.project_id
208212
}

modules/beta-private-cluster/cluster.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ resource "google_container_cluster" "primary" {
7575
disabled = var.disable_default_snat
7676
}
7777

78-
min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
78+
min_master_version = var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
7979

8080
dynamic "cluster_telemetry" {
8181
for_each = local.cluster_telemetry_type_is_set ? [1] : []
@@ -459,7 +459,7 @@ resource "google_container_node_pool" "pools" {
459459
project = var.project_id
460460
location = local.location
461461
// use node_locations if provided, defaults to cluster level node_locations if not specified
462-
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
462+
node_locations = local.node_locations
463463

464464
cluster = google_container_cluster.primary.name
465465

@@ -684,7 +684,7 @@ resource "google_container_node_pool" "windows_pools" {
684684
project = var.project_id
685685
location = local.location
686686
// use node_locations if provided, defaults to cluster level node_locations if not specified
687-
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
687+
node_locations = local.node_locations
688688

689689
cluster = google_container_cluster.primary.name
690690

modules/beta-private-cluster/main.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
Get available zones in region
2121
*****************************************/
2222
data "google_compute_zones" "available" {
23+
count = length(var.zones) == 0 ? 1 : 0
24+
2325
provider = google-beta
2426

2527
project = var.project_id
2628
region = local.region
2729
}
2830

2931
resource "random_shuffle" "available_zones" {
30-
input = data.google_compute_zones.available.names
32+
count = length(var.zones) == 0 ? 1 : 0
33+
34+
input = data.google_compute_zones.available[0].names
3135
result_count = 3
3236
}
3337

@@ -39,7 +43,7 @@ locals {
3943
location = var.regional ? var.region : var.zones[0]
4044
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
4145
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
42-
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
46+
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
4347
// Kubernetes version
4448
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
4549
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
@@ -203,6 +207,6 @@ data "google_container_engine_versions" "zone" {
203207
//
204208
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
205209
//
206-
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
210+
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names : var.zones[0]
207211
project = var.project_id
208212
}

modules/beta-public-cluster-update-variant/cluster.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ resource "google_container_cluster" "primary" {
7575
disabled = var.disable_default_snat
7676
}
7777

78-
min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
78+
min_master_version = var.release_channel == "UNSPECIFIED" ? local.master_version : var.kubernetes_version == "latest" ? null : var.kubernetes_version
7979

8080
dynamic "cluster_telemetry" {
8181
for_each = local.cluster_telemetry_type_is_set ? [1] : []
@@ -534,7 +534,7 @@ resource "google_container_node_pool" "pools" {
534534
project = var.project_id
535535
location = local.location
536536
// use node_locations if provided, defaults to cluster level node_locations if not specified
537-
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
537+
node_locations = local.node_locations
538538

539539
cluster = google_container_cluster.primary.name
540540

@@ -760,7 +760,7 @@ resource "google_container_node_pool" "windows_pools" {
760760
project = var.project_id
761761
location = local.location
762762
// use node_locations if provided, defaults to cluster level node_locations if not specified
763-
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
763+
node_locations = local.node_locations
764764

765765
cluster = google_container_cluster.primary.name
766766

0 commit comments

Comments
 (0)