Skip to content

Commit

Permalink
fix: make GKE module cluster_name computed attribute (#1189)
Browse files Browse the repository at this point in the history
* Make GKE module cluster_name computed attribute

* Use cluster ID for name output

* Fix splat operator

* Use values(...) to fix glob expression

* fix README

* refactor locals

* remove cluster_name local

Co-authored-by: Bharath KKB <bharathkrishnakb@gmail.com>
  • Loading branch information
Sam Naser and bharathkkb committed Apr 5, 2022
1 parent bbd9b77 commit 7a09acd
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 22 deletions.
9 changes: 8 additions & 1 deletion autogen/main/main.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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/<project-id>/locations/<location>/clusters/<cluster-name>/nodePools/<nodepool-name>
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
Expand Down
14 changes: 13 additions & 1 deletion autogen/main/outputs.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_zonal_with_asm/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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/<project-id>/locations/<location>/clusters/<cluster-name>/nodePools/<nodepool-name>
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
Expand Down
2 changes: 1 addition & 1 deletion modules/asm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/beta-autopilot-private-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion modules/beta-autopilot-private-cluster/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
3 changes: 2 additions & 1 deletion modules/beta-autopilot-public-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion modules/beta-autopilot-public-cluster/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
4 changes: 3 additions & 1 deletion modules/beta-private-cluster-update-variant/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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/<project-id>/locations/<location>/clusters/<cluster-name>/nodePools/<nodepool-name>
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
Expand Down
12 changes: 11 additions & 1 deletion modules/beta-private-cluster-update-variant/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
4 changes: 3 additions & 1 deletion modules/beta-private-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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/<project-id>/locations/<location>/clusters/<cluster-name>/nodePools/<nodepool-name>
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
Expand Down
12 changes: 11 additions & 1 deletion modules/beta-private-cluster/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
4 changes: 3 additions & 1 deletion modules/beta-public-cluster-update-variant/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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/<project-id>/locations/<location>/clusters/<cluster-name>/nodePools/<nodepool-name>
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
Expand Down
12 changes: 11 additions & 1 deletion modules/beta-public-cluster-update-variant/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
4 changes: 3 additions & 1 deletion modules/beta-public-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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/<project-id>/locations/<location>/clusters/<cluster-name>/nodePools/<nodepool-name>
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
Expand Down
12 changes: 11 additions & 1 deletion modules/beta-public-cluster/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
4 changes: 3 additions & 1 deletion modules/private-cluster-update-variant/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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/<project-id>/locations/<location>/clusters/<cluster-name>/nodePools/<nodepool-name>
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
Expand Down
12 changes: 11 additions & 1 deletion modules/private-cluster-update-variant/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
4 changes: 3 additions & 1 deletion modules/private-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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/<project-id>/locations/<location>/clusters/<cluster-name>/nodePools/<nodepool-name>
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
Expand Down
12 changes: 11 additions & 1 deletion modules/private-cluster/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
12 changes: 11 additions & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 7a09acd

Please sign in to comment.