diff --git a/autogen/main/main.tf.tmpl b/autogen/main/main.tf.tmpl index 5afd74e75..a9ba1803e 100644 --- a/autogen/main/main.tf.tmpl +++ b/autogen/main/main.tf.tmpl @@ -174,7 +174,14 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + {% if autopilot_cluster != true %} + // node pool ID is in the form projects//locations//clusters//nodePools/ + cluster_name_parts_from_nodepool = split("/", element(values(google_container_node_pool.pools)[*].id, 0)) + cluster_name_computed = element(local.cluster_name_parts_from_nodepool, length(local.cluster_name_parts_from_nodepool) - 3) + {% else %} + // cluster ID is in the form project/location/name + cluster_name_computed = element(split("/", local.cluster_id), length(split("/", local.cluster_id)) - 1) + {% endif %} cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/autogen/main/outputs.tf.tmpl b/autogen/main/outputs.tf.tmpl index bb3b09f1c..1aa6dadae 100644 --- a/autogen/main/outputs.tf.tmpl +++ b/autogen/main/outputs.tf.tmpl @@ -23,7 +23,19 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + {% if autopilot_cluster != true %} + google_container_node_pool.pools, + {% endif %} + ] } output "type" { diff --git a/examples/simple_zonal_with_asm/main.tf b/examples/simple_zonal_with_asm/main.tf index 8953c8b55..c7351435c 100644 --- a/examples/simple_zonal_with_asm/main.tf +++ b/examples/simple_zonal_with_asm/main.tf @@ -33,7 +33,7 @@ data "google_project" "project" { module "gke" { source = "../../" project_id = var.project_id - name = "${local.cluster_type}-cluster${var.cluster_name_suffix}" + name = "test-prefix-cluster-test-suffix" regional = false region = var.region zones = var.zones diff --git a/main.tf b/main.tf index 24297b180..ff906bf81 100644 --- a/main.tf +++ b/main.tf @@ -124,7 +124,9 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + // node pool ID is in the form projects//locations//clusters//nodePools/ + cluster_name_parts_from_nodepool = split("/", element(values(google_container_node_pool.pools)[*].id, 0)) + cluster_name_computed = element(local.cluster_name_parts_from_nodepool, length(local.cluster_name_parts_from_nodepool) - 3) cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/modules/asm/README.md b/modules/asm/README.md index 3bbc93bc0..2ca47d5d9 100644 --- a/modules/asm/README.md +++ b/modules/asm/README.md @@ -14,7 +14,7 @@ There is a full example provided [here](../../examples/simple_zonal_with_asm). D ```tf module "asm" { - source = "../../modules/asm" + source = "terraform-google-modules/kubernetes-engine/google//modules/asm" project_id = var.project_id cluster_name = module.gke.name cluster_location = module.gke.location diff --git a/modules/beta-autopilot-private-cluster/main.tf b/modules/beta-autopilot-private-cluster/main.tf index 779c6eb03..fd72a8fb3 100644 --- a/modules/beta-autopilot-private-cluster/main.tf +++ b/modules/beta-autopilot-private-cluster/main.tf @@ -107,7 +107,8 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + // cluster ID is in the form project/location/name + cluster_name_computed = element(split("/", local.cluster_id), length(split("/", local.cluster_id)) - 1) cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/modules/beta-autopilot-private-cluster/outputs.tf b/modules/beta-autopilot-private-cluster/outputs.tf index 6cdab1a5c..b3b21d905 100644 --- a/modules/beta-autopilot-private-cluster/outputs.tf +++ b/modules/beta-autopilot-private-cluster/outputs.tf @@ -23,7 +23,16 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + ] } output "type" { diff --git a/modules/beta-autopilot-public-cluster/main.tf b/modules/beta-autopilot-public-cluster/main.tf index cae98fa3c..32fab5a8a 100644 --- a/modules/beta-autopilot-public-cluster/main.tf +++ b/modules/beta-autopilot-public-cluster/main.tf @@ -106,7 +106,8 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + // cluster ID is in the form project/location/name + cluster_name_computed = element(split("/", local.cluster_id), length(split("/", local.cluster_id)) - 1) cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/modules/beta-autopilot-public-cluster/outputs.tf b/modules/beta-autopilot-public-cluster/outputs.tf index 3d0d04073..0950df951 100644 --- a/modules/beta-autopilot-public-cluster/outputs.tf +++ b/modules/beta-autopilot-public-cluster/outputs.tf @@ -23,7 +23,16 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + ] } output "type" { diff --git a/modules/beta-private-cluster-update-variant/main.tf b/modules/beta-private-cluster-update-variant/main.tf index acdae6152..c115e1a3c 100644 --- a/modules/beta-private-cluster-update-variant/main.tf +++ b/modules/beta-private-cluster-update-variant/main.tf @@ -147,7 +147,9 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + // node pool ID is in the form projects//locations//clusters//nodePools/ + cluster_name_parts_from_nodepool = split("/", element(values(google_container_node_pool.pools)[*].id, 0)) + cluster_name_computed = element(local.cluster_name_parts_from_nodepool, length(local.cluster_name_parts_from_nodepool) - 3) cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/modules/beta-private-cluster-update-variant/outputs.tf b/modules/beta-private-cluster-update-variant/outputs.tf index 4377e2ee3..41152fa2b 100644 --- a/modules/beta-private-cluster-update-variant/outputs.tf +++ b/modules/beta-private-cluster-update-variant/outputs.tf @@ -23,7 +23,17 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + google_container_node_pool.pools, + ] } output "type" { diff --git a/modules/beta-private-cluster/main.tf b/modules/beta-private-cluster/main.tf index acdae6152..c115e1a3c 100644 --- a/modules/beta-private-cluster/main.tf +++ b/modules/beta-private-cluster/main.tf @@ -147,7 +147,9 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + // node pool ID is in the form projects//locations//clusters//nodePools/ + cluster_name_parts_from_nodepool = split("/", element(values(google_container_node_pool.pools)[*].id, 0)) + cluster_name_computed = element(local.cluster_name_parts_from_nodepool, length(local.cluster_name_parts_from_nodepool) - 3) cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/modules/beta-private-cluster/outputs.tf b/modules/beta-private-cluster/outputs.tf index 4377e2ee3..41152fa2b 100644 --- a/modules/beta-private-cluster/outputs.tf +++ b/modules/beta-private-cluster/outputs.tf @@ -23,7 +23,17 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + google_container_node_pool.pools, + ] } output "type" { diff --git a/modules/beta-public-cluster-update-variant/main.tf b/modules/beta-public-cluster-update-variant/main.tf index 0f9683068..a3af1a009 100644 --- a/modules/beta-public-cluster-update-variant/main.tf +++ b/modules/beta-public-cluster-update-variant/main.tf @@ -146,7 +146,9 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + // node pool ID is in the form projects//locations//clusters//nodePools/ + cluster_name_parts_from_nodepool = split("/", element(values(google_container_node_pool.pools)[*].id, 0)) + cluster_name_computed = element(local.cluster_name_parts_from_nodepool, length(local.cluster_name_parts_from_nodepool) - 3) cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/modules/beta-public-cluster-update-variant/outputs.tf b/modules/beta-public-cluster-update-variant/outputs.tf index 1fbf1d6ab..d05e54294 100644 --- a/modules/beta-public-cluster-update-variant/outputs.tf +++ b/modules/beta-public-cluster-update-variant/outputs.tf @@ -23,7 +23,17 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + google_container_node_pool.pools, + ] } output "type" { diff --git a/modules/beta-public-cluster/main.tf b/modules/beta-public-cluster/main.tf index 0f9683068..a3af1a009 100644 --- a/modules/beta-public-cluster/main.tf +++ b/modules/beta-public-cluster/main.tf @@ -146,7 +146,9 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + // node pool ID is in the form projects//locations//clusters//nodePools/ + cluster_name_parts_from_nodepool = split("/", element(values(google_container_node_pool.pools)[*].id, 0)) + cluster_name_computed = element(local.cluster_name_parts_from_nodepool, length(local.cluster_name_parts_from_nodepool) - 3) cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/modules/beta-public-cluster/outputs.tf b/modules/beta-public-cluster/outputs.tf index 1fbf1d6ab..d05e54294 100644 --- a/modules/beta-public-cluster/outputs.tf +++ b/modules/beta-public-cluster/outputs.tf @@ -23,7 +23,17 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + google_container_node_pool.pools, + ] } output "type" { diff --git a/modules/private-cluster-update-variant/main.tf b/modules/private-cluster-update-variant/main.tf index 5702f92d8..c6e02efd5 100644 --- a/modules/private-cluster-update-variant/main.tf +++ b/modules/private-cluster-update-variant/main.tf @@ -125,7 +125,9 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + // node pool ID is in the form projects//locations//clusters//nodePools/ + cluster_name_parts_from_nodepool = split("/", element(values(google_container_node_pool.pools)[*].id, 0)) + cluster_name_computed = element(local.cluster_name_parts_from_nodepool, length(local.cluster_name_parts_from_nodepool) - 3) cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/modules/private-cluster-update-variant/outputs.tf b/modules/private-cluster-update-variant/outputs.tf index 62f205cda..4c1782210 100644 --- a/modules/private-cluster-update-variant/outputs.tf +++ b/modules/private-cluster-update-variant/outputs.tf @@ -23,7 +23,17 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + google_container_node_pool.pools, + ] } output "type" { diff --git a/modules/private-cluster/main.tf b/modules/private-cluster/main.tf index 5702f92d8..c6e02efd5 100644 --- a/modules/private-cluster/main.tf +++ b/modules/private-cluster/main.tf @@ -125,7 +125,9 @@ locals { cluster_region = var.regional ? var.region : join("-", slice(split("-", local.cluster_location), 0, 2)) cluster_zones = sort(local.cluster_output_zones) - cluster_name = local.cluster_output_name + // node pool ID is in the form projects//locations//clusters//nodePools/ + cluster_name_parts_from_nodepool = split("/", element(values(google_container_node_pool.pools)[*].id, 0)) + cluster_name_computed = element(local.cluster_name_parts_from_nodepool, length(local.cluster_name_parts_from_nodepool) - 3) cluster_network_tag = "gke-${var.name}" cluster_ca_certificate = local.cluster_master_auth_map["cluster_ca_certificate"] cluster_master_version = local.cluster_output_master_version diff --git a/modules/private-cluster/outputs.tf b/modules/private-cluster/outputs.tf index 62f205cda..4c1782210 100644 --- a/modules/private-cluster/outputs.tf +++ b/modules/private-cluster/outputs.tf @@ -23,7 +23,17 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + google_container_node_pool.pools, + ] } output "type" { diff --git a/outputs.tf b/outputs.tf index c28719639..414e662a1 100644 --- a/outputs.tf +++ b/outputs.tf @@ -23,7 +23,17 @@ output "cluster_id" { output "name" { description = "Cluster name" - value = local.cluster_name + value = local.cluster_name_computed + depends_on = [ + /* Nominally, the cluster name is populated as soon as it is known to Terraform. + * However, the cluster may not be in a usable state yet. Therefore any + * resources dependent on the cluster being up will fail to deploy. With + * this explicit dependency, dependent resources can wait for the cluster + * to be up. + */ + google_container_cluster.primary, + google_container_node_pool.pools, + ] } output "type" {