Skip to content

Commit

Permalink
fix: random zones only when zones are not provided (#1709)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyz authored Aug 22, 2023
1 parent fd233e5 commit 50ea965
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 34 deletions.
10 changes: 7 additions & 3 deletions autogen/main/main.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

{% if beta_cluster %}
provider = google-beta
{% else %}
Expand All @@ -31,7 +33,9 @@ data "google_compute_zones" "available" {
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -43,7 +47,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result),[])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -245,6 +249,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
2 changes: 1 addition & 1 deletion examples/simple_zonal_with_acm/acm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module "acm" {

secret_type = "ssh"

policy_bundles = ["https://github.com/GoogleCloudPlatform/acm-policy-controller-library/bundles/policy-essentials-v2022?ref=59f4695394285078f7c2029ec7d0f9ed1d6d700a"]
policy_bundles = ["https://github.com/GoogleCloudPlatform/acm-policy-controller-library/bundles/policy-essentials-v2022?ref=dd3f932eefa4c4c44c548144be1c2331d8594689"]

create_metrics_gcp_sa = true
}
10 changes: 7 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

provider = google

project = var.project_id
region = local.region
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -39,7 +43,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -175,6 +179,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
10 changes: 7 additions & 3 deletions modules/beta-autopilot-private-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

provider = google-beta

project = var.project_id
region = local.region
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -39,7 +43,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -143,6 +147,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
10 changes: 7 additions & 3 deletions modules/beta-autopilot-public-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

provider = google-beta

project = var.project_id
region = local.region
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -39,7 +43,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -142,6 +146,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
10 changes: 7 additions & 3 deletions modules/beta-private-cluster-update-variant/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

provider = google-beta

project = var.project_id
region = local.region
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -39,7 +43,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -203,6 +207,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
10 changes: 7 additions & 3 deletions modules/beta-private-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

provider = google-beta

project = var.project_id
region = local.region
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -39,7 +43,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -203,6 +207,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
10 changes: 7 additions & 3 deletions modules/beta-public-cluster-update-variant/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

provider = google-beta

project = var.project_id
region = local.region
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -39,7 +43,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -202,6 +206,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
10 changes: 7 additions & 3 deletions modules/beta-public-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

provider = google-beta

project = var.project_id
region = local.region
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -39,7 +43,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -202,6 +206,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
10 changes: 7 additions & 3 deletions modules/private-cluster-update-variant/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

provider = google

project = var.project_id
region = local.region
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -39,7 +43,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -176,6 +180,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
10 changes: 7 additions & 3 deletions modules/private-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
Get available zones in region
*****************************************/
data "google_compute_zones" "available" {
count = local.zone_count == 0 ? 1 : 0

provider = google

project = var.project_id
region = local.region
}

resource "random_shuffle" "available_zones" {
input = data.google_compute_zones.available.names
count = local.zone_count == 0 ? 1 : 0

input = data.google_compute_zones.available[0].names
result_count = 3
}

Expand All @@ -39,7 +43,7 @@ locals {
location = var.regional ? var.region : var.zones[0]
region = var.regional ? var.region : join("-", slice(split("-", var.zones[0]), 0, 2))
// for regional cluster - use var.zones if provided, use available otherwise, for zonal cluster use var.zones with first element extracted
node_locations = var.regional ? coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result)) : slice(var.zones, 1, length(var.zones))
node_locations = var.regional ? coalescelist(compact(var.zones), try(sort(random_shuffle.available_zones[0].result), [])) : slice(var.zones, 1, length(var.zones))
// Kubernetes version
master_version_regional = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_master_version
master_version_zonal = var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.zone.latest_master_version
Expand Down Expand Up @@ -176,6 +180,6 @@ data "google_container_engine_versions" "zone" {
//
// data.google_container_engine_versions.zone: Cannot determine zone: set in this resource, or set provider-level zone.
//
location = local.zone_count == 0 ? data.google_compute_zones.available.names[0] : var.zones[0]
location = local.zone_count == 0 ? data.google_compute_zones.available[0].names[0] : var.zones[0]
project = var.project_id
}
3 changes: 3 additions & 0 deletions test/integration/beta_cluster/testdata/TestBetaCluster.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
"enableComponents": [
"SYSTEM_COMPONENTS"
]
},
"managedPrometheusConfig": {
"enabled": true
}
},
"monitoringService": "monitoring.googleapis.com/kubernetes",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"addonsConfig": {
"configConnectorConfig": {},
"dnsCacheConfig": {},
"gcePersistentDiskCsiDriverConfig": {},
"gcePersistentDiskCsiDriverConfig": {
"enabled": true
},
"gcpFilestoreCsiDriverConfig": {},
"gkeBackupAgentConfig": {},
"horizontalPodAutoscaling": {},
Expand Down
Loading

0 comments on commit 50ea965

Please sign in to comment.