diff --git a/autogen/main/cluster.tf.tmpl b/autogen/main/cluster.tf.tmpl index bb42580d4..c8fb7a88a 100644 --- a/autogen/main/cluster.tf.tmpl +++ b/autogen/main/cluster.tf.tmpl @@ -274,13 +274,13 @@ locals { # resources where "ForceNew" is "true". schemaNodeConfig can be found in node_config.go at # https://github.com/terraform-providers/terraform-provider-google/blob/master/google/node_config.go#L22 resource "random_id" "name" { - count = length(var.node_pools) + for_each = local.node_pools byte_length = 2 - prefix = format("%s-", lookup(var.node_pools[count.index], "name")) + prefix = format("%s-", lookup(each.value, "name")) keepers = merge( zipmap( local.force_node_pool_recreation_resources, - [for keeper in local.force_node_pool_recreation_resources : lookup(var.node_pools[count.index], keeper, "")] + [for keeper in local.force_node_pool_recreation_resources : lookup(each.value, keeper, "")] ), { labels = join(",", @@ -288,8 +288,8 @@ resource "random_id" "name" { concat( keys(local.node_pools_labels["all"]), values(local.node_pools_labels["all"]), - keys(local.node_pools_labels[var.node_pools[count.index]["name"]]), - values(local.node_pools_labels[var.node_pools[count.index]["name"]]) + keys(local.node_pools_labels[each.value["name"]]), + values(local.node_pools_labels[each.value["name"]]) ) ) ) @@ -300,8 +300,8 @@ resource "random_id" "name" { concat( keys(local.node_pools_metadata["all"]), values(local.node_pools_metadata["all"]), - keys(local.node_pools_metadata[var.node_pools[count.index]["name"]]), - values(local.node_pools_metadata[var.node_pools[count.index]["name"]]) + keys(local.node_pools_metadata[each.value["name"]]), + values(local.node_pools_metadata[each.value["name"]]) ) ) ) @@ -311,7 +311,7 @@ resource "random_id" "name" { sort( concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]] + local.node_pools_oauth_scopes[each.value["name"]] ) ) ) @@ -321,7 +321,7 @@ resource "random_id" "name" { sort( concat( local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]] + local.node_pools_tags[each.value["name"]] ) ) ) @@ -336,41 +336,41 @@ resource "google_container_node_pool" "pools" { {% else %} provider = google {% endif %} - count = length(var.node_pools) + for_each = local.node_pools {% if update_variant %} - name = random_id.name.*.hex[count.index] + name = random_id.name.*.hex[each.key] {% else %} - name = var.node_pools[count.index]["name"] + name = each.key {% endif %} project = var.project_id location = local.location {% if beta_cluster %} // use node_locations if provided, defaults to cluster level node_locations if not specified - node_locations = lookup(var.node_pools[count.index], "node_locations", "") != "" ? split(",", var.node_pools[count.index]["node_locations"]) : null + node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null {% endif %} cluster = google_container_cluster.primary.name - version = lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup( - var.node_pools[count.index], + version = lookup(each.value, "auto_upgrade", false) ? "" : lookup( + each.value, "version", local.node_version, ) - initial_node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? lookup( - var.node_pools[count.index], + initial_node_count = lookup(each.value, "autoscaling", true) ? lookup( + each.value, "initial_node_count", - lookup(var.node_pools[count.index], "min_count", 1) + lookup(each.value, "min_count", 1) ) : null {% if beta_cluster %} - max_pods_per_node = lookup(var.node_pools[count.index], "max_pods_per_node", null) + max_pods_per_node = lookup(each.value, "max_pods_per_node", null) {% endif %} - node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? null : lookup(var.node_pools[count.index], "node_count", 1) + node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1) dynamic "autoscaling" { - for_each = lookup(var.node_pools[count.index], "autoscaling", true) ? [var.node_pools[count.index]] : [] + for_each = lookup(each.value, "autoscaling", true) ? [each.value] : [] content { min_node_count = lookup(autoscaling.value, "min_count", 1) max_node_count = lookup(autoscaling.value, "max_count", 100) @@ -378,24 +378,24 @@ resource "google_container_node_pool" "pools" { } management { - auto_repair = lookup(var.node_pools[count.index], "auto_repair", true) - auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", local.default_auto_upgrade) + auto_repair = lookup(each.value, "auto_repair", true) + auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade) } node_config { - image_type = lookup(var.node_pools[count.index], "image_type", "COS") - machine_type = lookup(var.node_pools[count.index], "machine_type", "n1-standard-2") + image_type = lookup(each.value, "image_type", "COS") + machine_type = lookup(each.value, "machine_type", "n1-standard-2") labels = merge( lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_labels["all"], - local.node_pools_labels[var.node_pools[count.index]["name"]], + local.node_pools_labels[each.value["name"]], ) metadata = merge( lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_metadata["all"], - local.node_pools_metadata[var.node_pools[count.index]["name"]], + local.node_pools_metadata[each.value["name"]], { "disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints }, @@ -404,7 +404,7 @@ resource "google_container_node_pool" "pools" { dynamic "taint" { for_each = concat( local.node_pools_taints["all"], - local.node_pools_taints[var.node_pools[count.index]["name"]], + local.node_pools_taints[each.value["name"]], ) content { effect = taint.value.effect @@ -415,31 +415,31 @@ resource "google_container_node_pool" "pools" { {% endif %} tags = concat( lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [], - lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${var.node_pools[count.index]["name"]}"] : [], + lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [], local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]], + local.node_pools_tags[each.value["name"]], ) - local_ssd_count = lookup(var.node_pools[count.index], "local_ssd_count", 0) - disk_size_gb = lookup(var.node_pools[count.index], "disk_size_gb", 100) - disk_type = lookup(var.node_pools[count.index], "disk_type", "pd-standard") + local_ssd_count = lookup(each.value, "local_ssd_count", 0) + disk_size_gb = lookup(each.value, "disk_size_gb", 100) + disk_type = lookup(each.value, "disk_type", "pd-standard") service_account = lookup( - var.node_pools[count.index], + each.value, "service_account", local.service_account, ) - preemptible = lookup(var.node_pools[count.index], "preemptible", false) + preemptible = lookup(each.value, "preemptible", false) oauth_scopes = concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]], + local.node_pools_oauth_scopes[each.value["name"]], ) guest_accelerator = [ - for guest_accelerator in lookup(var.node_pools[count.index], "accelerator_count", 0) > 0 ? [{ - type = lookup(var.node_pools[count.index], "accelerator_type", "") - count = lookup(var.node_pools[count.index], "accelerator_count", 0) + for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{ + type = lookup(each.value, "accelerator_type", "") + count = lookup(each.value, "accelerator_count", 0) }] : [] : { type = guest_accelerator["type"] count = guest_accelerator["count"] @@ -451,7 +451,7 @@ resource "google_container_node_pool" "pools" { for_each = local.cluster_node_metadata_config content { - node_metadata = lookup(var.node_pools[count.index], "node_metadata", workload_metadata_config.value.node_metadata) + node_metadata = lookup(each.value, "node_metadata", workload_metadata_config.value.node_metadata) } } diff --git a/autogen/main/main.tf.tmpl b/autogen/main/main.tf.tmpl index 3cc66a9f7..b024d11ba 100644 --- a/autogen/main/main.tf.tmpl +++ b/autogen/main/main.tf.tmpl @@ -48,6 +48,11 @@ locals { node_version_zonal = var.node_version != "" && ! var.regional ? var.node_version : local.master_version_zonal master_version = var.regional ? local.master_version_regional : local.master_version_zonal node_version = var.regional ? local.node_version_regional : local.node_version_zonal + + // Build a map of maps of node pools from a list of objects + node_pool_names = [for np in toset(var.node_pools) : np.name] + node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools))) + {% if beta_cluster %} release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : [] @@ -129,8 +134,8 @@ locals { cidr_blocks : var.master_authorized_networks }] - cluster_output_node_pools_names = concat(google_container_node_pool.pools.*.name, [""]) - cluster_output_node_pools_versions = concat(google_container_node_pool.pools.*.version, [""]) + cluster_output_node_pools_names = concat([for np in google_container_node_pool.pools : np.name], [""]) + cluster_output_node_pools_versions = concat([for np in google_container_node_pool.pools : np.version], [""]) cluster_master_auth_list_layer1 = local.cluster_output_master_auth cluster_master_auth_list_layer2 = local.cluster_master_auth_list_layer1[0] diff --git a/autogen/main/versions.tf.tmpl b/autogen/main/versions.tf.tmpl index 4471f9215..f15cd3211 100644 --- a/autogen/main/versions.tf.tmpl +++ b/autogen/main/versions.tf.tmpl @@ -15,7 +15,7 @@ */ terraform { - required_version = ">= 0.12" + required_version = "~> 0.12.6" required_providers { {% if beta_cluster %} diff --git a/cluster.tf b/cluster.tf index 1e2692f94..5a5734033 100644 --- a/cluster.tf +++ b/cluster.tf @@ -123,30 +123,30 @@ resource "google_container_cluster" "primary" { *****************************************/ resource "google_container_node_pool" "pools" { provider = google - count = length(var.node_pools) - name = var.node_pools[count.index]["name"] + for_each = local.node_pools + name = each.key project = var.project_id location = local.location cluster = google_container_cluster.primary.name - version = lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup( - var.node_pools[count.index], + version = lookup(each.value, "auto_upgrade", false) ? "" : lookup( + each.value, "version", local.node_version, ) - initial_node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? lookup( - var.node_pools[count.index], + initial_node_count = lookup(each.value, "autoscaling", true) ? lookup( + each.value, "initial_node_count", - lookup(var.node_pools[count.index], "min_count", 1) + lookup(each.value, "min_count", 1) ) : null - node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? null : lookup(var.node_pools[count.index], "node_count", 1) + node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1) dynamic "autoscaling" { - for_each = lookup(var.node_pools[count.index], "autoscaling", true) ? [var.node_pools[count.index]] : [] + for_each = lookup(each.value, "autoscaling", true) ? [each.value] : [] content { min_node_count = lookup(autoscaling.value, "min_count", 1) max_node_count = lookup(autoscaling.value, "max_count", 100) @@ -154,55 +154,55 @@ resource "google_container_node_pool" "pools" { } management { - auto_repair = lookup(var.node_pools[count.index], "auto_repair", true) - auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", local.default_auto_upgrade) + auto_repair = lookup(each.value, "auto_repair", true) + auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade) } node_config { - image_type = lookup(var.node_pools[count.index], "image_type", "COS") - machine_type = lookup(var.node_pools[count.index], "machine_type", "n1-standard-2") + image_type = lookup(each.value, "image_type", "COS") + machine_type = lookup(each.value, "machine_type", "n1-standard-2") labels = merge( lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_labels["all"], - local.node_pools_labels[var.node_pools[count.index]["name"]], + local.node_pools_labels[each.value["name"]], ) metadata = merge( lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_metadata["all"], - local.node_pools_metadata[var.node_pools[count.index]["name"]], + local.node_pools_metadata[each.value["name"]], { "disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints }, ) tags = concat( lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [], - lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${var.node_pools[count.index]["name"]}"] : [], + lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [], local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]], + local.node_pools_tags[each.value["name"]], ) - local_ssd_count = lookup(var.node_pools[count.index], "local_ssd_count", 0) - disk_size_gb = lookup(var.node_pools[count.index], "disk_size_gb", 100) - disk_type = lookup(var.node_pools[count.index], "disk_type", "pd-standard") + local_ssd_count = lookup(each.value, "local_ssd_count", 0) + disk_size_gb = lookup(each.value, "disk_size_gb", 100) + disk_type = lookup(each.value, "disk_type", "pd-standard") service_account = lookup( - var.node_pools[count.index], + each.value, "service_account", local.service_account, ) - preemptible = lookup(var.node_pools[count.index], "preemptible", false) + preemptible = lookup(each.value, "preemptible", false) oauth_scopes = concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]], + local.node_pools_oauth_scopes[each.value["name"]], ) guest_accelerator = [ - for guest_accelerator in lookup(var.node_pools[count.index], "accelerator_count", 0) > 0 ? [{ - type = lookup(var.node_pools[count.index], "accelerator_type", "") - count = lookup(var.node_pools[count.index], "accelerator_count", 0) + for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{ + type = lookup(each.value, "accelerator_type", "") + count = lookup(each.value, "accelerator_count", 0) }] : [] : { type = guest_accelerator["type"] count = guest_accelerator["count"] diff --git a/main.tf b/main.tf index 893cc4f6d..b8e84b314 100644 --- a/main.tf +++ b/main.tf @@ -45,6 +45,11 @@ locals { master_version = var.regional ? local.master_version_regional : local.master_version_zonal node_version = var.regional ? local.node_version_regional : local.node_version_zonal + // Build a map of maps of node pools from a list of objects + node_pool_names = [for np in toset(var.node_pools) : np.name] + node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools))) + + custom_kube_dns_config = length(keys(var.stub_domains)) > 0 upstream_nameservers_config = length(var.upstream_nameservers) > 0 @@ -84,8 +89,8 @@ locals { cidr_blocks : var.master_authorized_networks }] - cluster_output_node_pools_names = concat(google_container_node_pool.pools.*.name, [""]) - cluster_output_node_pools_versions = concat(google_container_node_pool.pools.*.version, [""]) + cluster_output_node_pools_names = concat([for np in google_container_node_pool.pools : np.name], [""]) + cluster_output_node_pools_versions = concat([for np in google_container_node_pool.pools : np.version], [""]) cluster_master_auth_list_layer1 = local.cluster_output_master_auth cluster_master_auth_list_layer2 = local.cluster_master_auth_list_layer1[0] diff --git a/modules/beta-private-cluster-update-variant/cluster.tf b/modules/beta-private-cluster-update-variant/cluster.tf index 8fe852d88..c7c6fa55f 100644 --- a/modules/beta-private-cluster-update-variant/cluster.tf +++ b/modules/beta-private-cluster-update-variant/cluster.tf @@ -251,13 +251,13 @@ locals { # resources where "ForceNew" is "true". schemaNodeConfig can be found in node_config.go at # https://github.com/terraform-providers/terraform-provider-google/blob/master/google/node_config.go#L22 resource "random_id" "name" { - count = length(var.node_pools) + for_each = local.node_pools byte_length = 2 - prefix = format("%s-", lookup(var.node_pools[count.index], "name")) + prefix = format("%s-", lookup(each.value, "name")) keepers = merge( zipmap( local.force_node_pool_recreation_resources, - [for keeper in local.force_node_pool_recreation_resources : lookup(var.node_pools[count.index], keeper, "")] + [for keeper in local.force_node_pool_recreation_resources : lookup(each.value, keeper, "")] ), { labels = join(",", @@ -265,8 +265,8 @@ resource "random_id" "name" { concat( keys(local.node_pools_labels["all"]), values(local.node_pools_labels["all"]), - keys(local.node_pools_labels[var.node_pools[count.index]["name"]]), - values(local.node_pools_labels[var.node_pools[count.index]["name"]]) + keys(local.node_pools_labels[each.value["name"]]), + values(local.node_pools_labels[each.value["name"]]) ) ) ) @@ -277,8 +277,8 @@ resource "random_id" "name" { concat( keys(local.node_pools_metadata["all"]), values(local.node_pools_metadata["all"]), - keys(local.node_pools_metadata[var.node_pools[count.index]["name"]]), - values(local.node_pools_metadata[var.node_pools[count.index]["name"]]) + keys(local.node_pools_metadata[each.value["name"]]), + values(local.node_pools_metadata[each.value["name"]]) ) ) ) @@ -288,7 +288,7 @@ resource "random_id" "name" { sort( concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]] + local.node_pools_oauth_scopes[each.value["name"]] ) ) ) @@ -298,7 +298,7 @@ resource "random_id" "name" { sort( concat( local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]] + local.node_pools_tags[each.value["name"]] ) ) ) @@ -308,33 +308,33 @@ resource "random_id" "name" { resource "google_container_node_pool" "pools" { provider = google-beta - count = length(var.node_pools) - name = random_id.name.*.hex[count.index] + for_each = local.node_pools + name = random_id.name.*.hex[each.key] project = var.project_id location = local.location // use node_locations if provided, defaults to cluster level node_locations if not specified - node_locations = lookup(var.node_pools[count.index], "node_locations", "") != "" ? split(",", var.node_pools[count.index]["node_locations"]) : null + node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null cluster = google_container_cluster.primary.name - version = lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup( - var.node_pools[count.index], + version = lookup(each.value, "auto_upgrade", false) ? "" : lookup( + each.value, "version", local.node_version, ) - initial_node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? lookup( - var.node_pools[count.index], + initial_node_count = lookup(each.value, "autoscaling", true) ? lookup( + each.value, "initial_node_count", - lookup(var.node_pools[count.index], "min_count", 1) + lookup(each.value, "min_count", 1) ) : null - max_pods_per_node = lookup(var.node_pools[count.index], "max_pods_per_node", null) + max_pods_per_node = lookup(each.value, "max_pods_per_node", null) - node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? null : lookup(var.node_pools[count.index], "node_count", 1) + node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1) dynamic "autoscaling" { - for_each = lookup(var.node_pools[count.index], "autoscaling", true) ? [var.node_pools[count.index]] : [] + for_each = lookup(each.value, "autoscaling", true) ? [each.value] : [] content { min_node_count = lookup(autoscaling.value, "min_count", 1) max_node_count = lookup(autoscaling.value, "max_count", 100) @@ -342,24 +342,24 @@ resource "google_container_node_pool" "pools" { } management { - auto_repair = lookup(var.node_pools[count.index], "auto_repair", true) - auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", local.default_auto_upgrade) + auto_repair = lookup(each.value, "auto_repair", true) + auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade) } node_config { - image_type = lookup(var.node_pools[count.index], "image_type", "COS") - machine_type = lookup(var.node_pools[count.index], "machine_type", "n1-standard-2") + image_type = lookup(each.value, "image_type", "COS") + machine_type = lookup(each.value, "machine_type", "n1-standard-2") labels = merge( lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_labels["all"], - local.node_pools_labels[var.node_pools[count.index]["name"]], + local.node_pools_labels[each.value["name"]], ) metadata = merge( lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_metadata["all"], - local.node_pools_metadata[var.node_pools[count.index]["name"]], + local.node_pools_metadata[each.value["name"]], { "disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints }, @@ -367,7 +367,7 @@ resource "google_container_node_pool" "pools" { dynamic "taint" { for_each = concat( local.node_pools_taints["all"], - local.node_pools_taints[var.node_pools[count.index]["name"]], + local.node_pools_taints[each.value["name"]], ) content { effect = taint.value.effect @@ -377,31 +377,31 @@ resource "google_container_node_pool" "pools" { } tags = concat( lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [], - lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${var.node_pools[count.index]["name"]}"] : [], + lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [], local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]], + local.node_pools_tags[each.value["name"]], ) - local_ssd_count = lookup(var.node_pools[count.index], "local_ssd_count", 0) - disk_size_gb = lookup(var.node_pools[count.index], "disk_size_gb", 100) - disk_type = lookup(var.node_pools[count.index], "disk_type", "pd-standard") + local_ssd_count = lookup(each.value, "local_ssd_count", 0) + disk_size_gb = lookup(each.value, "disk_size_gb", 100) + disk_type = lookup(each.value, "disk_type", "pd-standard") service_account = lookup( - var.node_pools[count.index], + each.value, "service_account", local.service_account, ) - preemptible = lookup(var.node_pools[count.index], "preemptible", false) + preemptible = lookup(each.value, "preemptible", false) oauth_scopes = concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]], + local.node_pools_oauth_scopes[each.value["name"]], ) guest_accelerator = [ - for guest_accelerator in lookup(var.node_pools[count.index], "accelerator_count", 0) > 0 ? [{ - type = lookup(var.node_pools[count.index], "accelerator_type", "") - count = lookup(var.node_pools[count.index], "accelerator_count", 0) + for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{ + type = lookup(each.value, "accelerator_type", "") + count = lookup(each.value, "accelerator_count", 0) }] : [] : { type = guest_accelerator["type"] count = guest_accelerator["count"] @@ -412,7 +412,7 @@ resource "google_container_node_pool" "pools" { for_each = local.cluster_node_metadata_config content { - node_metadata = lookup(var.node_pools[count.index], "node_metadata", workload_metadata_config.value.node_metadata) + node_metadata = lookup(each.value, "node_metadata", workload_metadata_config.value.node_metadata) } } diff --git a/modules/beta-private-cluster-update-variant/main.tf b/modules/beta-private-cluster-update-variant/main.tf index 7dbdacd25..0097a20d4 100644 --- a/modules/beta-private-cluster-update-variant/main.tf +++ b/modules/beta-private-cluster-update-variant/main.tf @@ -44,7 +44,12 @@ locals { node_version_zonal = var.node_version != "" && ! var.regional ? var.node_version : local.master_version_zonal master_version = var.regional ? local.master_version_regional : local.master_version_zonal node_version = var.regional ? local.node_version_regional : local.node_version_zonal - release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : [] + + // Build a map of maps of node pools from a list of objects + node_pool_names = [for np in toset(var.node_pools) : np.name] + node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools))) + + release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : [] autoscalling_resource_limits = var.cluster_autoscaling.enabled ? [{ resource_type = "cpu" @@ -115,8 +120,8 @@ locals { cidr_blocks : var.master_authorized_networks }] - cluster_output_node_pools_names = concat(google_container_node_pool.pools.*.name, [""]) - cluster_output_node_pools_versions = concat(google_container_node_pool.pools.*.version, [""]) + cluster_output_node_pools_names = concat([for np in google_container_node_pool.pools : np.name], [""]) + cluster_output_node_pools_versions = concat([for np in google_container_node_pool.pools : np.version], [""]) cluster_master_auth_list_layer1 = local.cluster_output_master_auth cluster_master_auth_list_layer2 = local.cluster_master_auth_list_layer1[0] diff --git a/modules/beta-private-cluster-update-variant/versions.tf b/modules/beta-private-cluster-update-variant/versions.tf index 89cd6ff0d..0c5d256a5 100644 --- a/modules/beta-private-cluster-update-variant/versions.tf +++ b/modules/beta-private-cluster-update-variant/versions.tf @@ -15,7 +15,7 @@ */ terraform { - required_version = ">= 0.12" + required_version = "~> 0.12.6" required_providers { google-beta = ">= 2.18, <4.0.0" diff --git a/modules/beta-private-cluster/cluster.tf b/modules/beta-private-cluster/cluster.tf index cbc920ceb..90fd8ad8b 100644 --- a/modules/beta-private-cluster/cluster.tf +++ b/modules/beta-private-cluster/cluster.tf @@ -236,33 +236,33 @@ resource "google_container_cluster" "primary" { *****************************************/ resource "google_container_node_pool" "pools" { provider = google-beta - count = length(var.node_pools) - name = var.node_pools[count.index]["name"] + for_each = local.node_pools + name = each.key project = var.project_id location = local.location // use node_locations if provided, defaults to cluster level node_locations if not specified - node_locations = lookup(var.node_pools[count.index], "node_locations", "") != "" ? split(",", var.node_pools[count.index]["node_locations"]) : null + node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null cluster = google_container_cluster.primary.name - version = lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup( - var.node_pools[count.index], + version = lookup(each.value, "auto_upgrade", false) ? "" : lookup( + each.value, "version", local.node_version, ) - initial_node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? lookup( - var.node_pools[count.index], + initial_node_count = lookup(each.value, "autoscaling", true) ? lookup( + each.value, "initial_node_count", - lookup(var.node_pools[count.index], "min_count", 1) + lookup(each.value, "min_count", 1) ) : null - max_pods_per_node = lookup(var.node_pools[count.index], "max_pods_per_node", null) + max_pods_per_node = lookup(each.value, "max_pods_per_node", null) - node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? null : lookup(var.node_pools[count.index], "node_count", 1) + node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1) dynamic "autoscaling" { - for_each = lookup(var.node_pools[count.index], "autoscaling", true) ? [var.node_pools[count.index]] : [] + for_each = lookup(each.value, "autoscaling", true) ? [each.value] : [] content { min_node_count = lookup(autoscaling.value, "min_count", 1) max_node_count = lookup(autoscaling.value, "max_count", 100) @@ -270,24 +270,24 @@ resource "google_container_node_pool" "pools" { } management { - auto_repair = lookup(var.node_pools[count.index], "auto_repair", true) - auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", local.default_auto_upgrade) + auto_repair = lookup(each.value, "auto_repair", true) + auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade) } node_config { - image_type = lookup(var.node_pools[count.index], "image_type", "COS") - machine_type = lookup(var.node_pools[count.index], "machine_type", "n1-standard-2") + image_type = lookup(each.value, "image_type", "COS") + machine_type = lookup(each.value, "machine_type", "n1-standard-2") labels = merge( lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_labels["all"], - local.node_pools_labels[var.node_pools[count.index]["name"]], + local.node_pools_labels[each.value["name"]], ) metadata = merge( lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_metadata["all"], - local.node_pools_metadata[var.node_pools[count.index]["name"]], + local.node_pools_metadata[each.value["name"]], { "disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints }, @@ -295,7 +295,7 @@ resource "google_container_node_pool" "pools" { dynamic "taint" { for_each = concat( local.node_pools_taints["all"], - local.node_pools_taints[var.node_pools[count.index]["name"]], + local.node_pools_taints[each.value["name"]], ) content { effect = taint.value.effect @@ -305,31 +305,31 @@ resource "google_container_node_pool" "pools" { } tags = concat( lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [], - lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${var.node_pools[count.index]["name"]}"] : [], + lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [], local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]], + local.node_pools_tags[each.value["name"]], ) - local_ssd_count = lookup(var.node_pools[count.index], "local_ssd_count", 0) - disk_size_gb = lookup(var.node_pools[count.index], "disk_size_gb", 100) - disk_type = lookup(var.node_pools[count.index], "disk_type", "pd-standard") + local_ssd_count = lookup(each.value, "local_ssd_count", 0) + disk_size_gb = lookup(each.value, "disk_size_gb", 100) + disk_type = lookup(each.value, "disk_type", "pd-standard") service_account = lookup( - var.node_pools[count.index], + each.value, "service_account", local.service_account, ) - preemptible = lookup(var.node_pools[count.index], "preemptible", false) + preemptible = lookup(each.value, "preemptible", false) oauth_scopes = concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]], + local.node_pools_oauth_scopes[each.value["name"]], ) guest_accelerator = [ - for guest_accelerator in lookup(var.node_pools[count.index], "accelerator_count", 0) > 0 ? [{ - type = lookup(var.node_pools[count.index], "accelerator_type", "") - count = lookup(var.node_pools[count.index], "accelerator_count", 0) + for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{ + type = lookup(each.value, "accelerator_type", "") + count = lookup(each.value, "accelerator_count", 0) }] : [] : { type = guest_accelerator["type"] count = guest_accelerator["count"] @@ -340,7 +340,7 @@ resource "google_container_node_pool" "pools" { for_each = local.cluster_node_metadata_config content { - node_metadata = lookup(var.node_pools[count.index], "node_metadata", workload_metadata_config.value.node_metadata) + node_metadata = lookup(each.value, "node_metadata", workload_metadata_config.value.node_metadata) } } diff --git a/modules/beta-private-cluster/main.tf b/modules/beta-private-cluster/main.tf index 7dbdacd25..0097a20d4 100644 --- a/modules/beta-private-cluster/main.tf +++ b/modules/beta-private-cluster/main.tf @@ -44,7 +44,12 @@ locals { node_version_zonal = var.node_version != "" && ! var.regional ? var.node_version : local.master_version_zonal master_version = var.regional ? local.master_version_regional : local.master_version_zonal node_version = var.regional ? local.node_version_regional : local.node_version_zonal - release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : [] + + // Build a map of maps of node pools from a list of objects + node_pool_names = [for np in toset(var.node_pools) : np.name] + node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools))) + + release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : [] autoscalling_resource_limits = var.cluster_autoscaling.enabled ? [{ resource_type = "cpu" @@ -115,8 +120,8 @@ locals { cidr_blocks : var.master_authorized_networks }] - cluster_output_node_pools_names = concat(google_container_node_pool.pools.*.name, [""]) - cluster_output_node_pools_versions = concat(google_container_node_pool.pools.*.version, [""]) + cluster_output_node_pools_names = concat([for np in google_container_node_pool.pools : np.name], [""]) + cluster_output_node_pools_versions = concat([for np in google_container_node_pool.pools : np.version], [""]) cluster_master_auth_list_layer1 = local.cluster_output_master_auth cluster_master_auth_list_layer2 = local.cluster_master_auth_list_layer1[0] diff --git a/modules/beta-private-cluster/versions.tf b/modules/beta-private-cluster/versions.tf index 89cd6ff0d..0c5d256a5 100644 --- a/modules/beta-private-cluster/versions.tf +++ b/modules/beta-private-cluster/versions.tf @@ -15,7 +15,7 @@ */ terraform { - required_version = ">= 0.12" + required_version = "~> 0.12.6" required_providers { google-beta = ">= 2.18, <4.0.0" diff --git a/modules/beta-public-cluster/cluster.tf b/modules/beta-public-cluster/cluster.tf index 7c554fbb7..708f9325d 100644 --- a/modules/beta-public-cluster/cluster.tf +++ b/modules/beta-public-cluster/cluster.tf @@ -223,33 +223,33 @@ resource "google_container_cluster" "primary" { *****************************************/ resource "google_container_node_pool" "pools" { provider = google-beta - count = length(var.node_pools) - name = var.node_pools[count.index]["name"] + for_each = local.node_pools + name = each.key project = var.project_id location = local.location // use node_locations if provided, defaults to cluster level node_locations if not specified - node_locations = lookup(var.node_pools[count.index], "node_locations", "") != "" ? split(",", var.node_pools[count.index]["node_locations"]) : null + node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null cluster = google_container_cluster.primary.name - version = lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup( - var.node_pools[count.index], + version = lookup(each.value, "auto_upgrade", false) ? "" : lookup( + each.value, "version", local.node_version, ) - initial_node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? lookup( - var.node_pools[count.index], + initial_node_count = lookup(each.value, "autoscaling", true) ? lookup( + each.value, "initial_node_count", - lookup(var.node_pools[count.index], "min_count", 1) + lookup(each.value, "min_count", 1) ) : null - max_pods_per_node = lookup(var.node_pools[count.index], "max_pods_per_node", null) + max_pods_per_node = lookup(each.value, "max_pods_per_node", null) - node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? null : lookup(var.node_pools[count.index], "node_count", 1) + node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1) dynamic "autoscaling" { - for_each = lookup(var.node_pools[count.index], "autoscaling", true) ? [var.node_pools[count.index]] : [] + for_each = lookup(each.value, "autoscaling", true) ? [each.value] : [] content { min_node_count = lookup(autoscaling.value, "min_count", 1) max_node_count = lookup(autoscaling.value, "max_count", 100) @@ -257,24 +257,24 @@ resource "google_container_node_pool" "pools" { } management { - auto_repair = lookup(var.node_pools[count.index], "auto_repair", true) - auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", local.default_auto_upgrade) + auto_repair = lookup(each.value, "auto_repair", true) + auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade) } node_config { - image_type = lookup(var.node_pools[count.index], "image_type", "COS") - machine_type = lookup(var.node_pools[count.index], "machine_type", "n1-standard-2") + image_type = lookup(each.value, "image_type", "COS") + machine_type = lookup(each.value, "machine_type", "n1-standard-2") labels = merge( lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_labels["all"], - local.node_pools_labels[var.node_pools[count.index]["name"]], + local.node_pools_labels[each.value["name"]], ) metadata = merge( lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_metadata["all"], - local.node_pools_metadata[var.node_pools[count.index]["name"]], + local.node_pools_metadata[each.value["name"]], { "disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints }, @@ -282,7 +282,7 @@ resource "google_container_node_pool" "pools" { dynamic "taint" { for_each = concat( local.node_pools_taints["all"], - local.node_pools_taints[var.node_pools[count.index]["name"]], + local.node_pools_taints[each.value["name"]], ) content { effect = taint.value.effect @@ -292,31 +292,31 @@ resource "google_container_node_pool" "pools" { } tags = concat( lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [], - lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${var.node_pools[count.index]["name"]}"] : [], + lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [], local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]], + local.node_pools_tags[each.value["name"]], ) - local_ssd_count = lookup(var.node_pools[count.index], "local_ssd_count", 0) - disk_size_gb = lookup(var.node_pools[count.index], "disk_size_gb", 100) - disk_type = lookup(var.node_pools[count.index], "disk_type", "pd-standard") + local_ssd_count = lookup(each.value, "local_ssd_count", 0) + disk_size_gb = lookup(each.value, "disk_size_gb", 100) + disk_type = lookup(each.value, "disk_type", "pd-standard") service_account = lookup( - var.node_pools[count.index], + each.value, "service_account", local.service_account, ) - preemptible = lookup(var.node_pools[count.index], "preemptible", false) + preemptible = lookup(each.value, "preemptible", false) oauth_scopes = concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]], + local.node_pools_oauth_scopes[each.value["name"]], ) guest_accelerator = [ - for guest_accelerator in lookup(var.node_pools[count.index], "accelerator_count", 0) > 0 ? [{ - type = lookup(var.node_pools[count.index], "accelerator_type", "") - count = lookup(var.node_pools[count.index], "accelerator_count", 0) + for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{ + type = lookup(each.value, "accelerator_type", "") + count = lookup(each.value, "accelerator_count", 0) }] : [] : { type = guest_accelerator["type"] count = guest_accelerator["count"] @@ -327,7 +327,7 @@ resource "google_container_node_pool" "pools" { for_each = local.cluster_node_metadata_config content { - node_metadata = lookup(var.node_pools[count.index], "node_metadata", workload_metadata_config.value.node_metadata) + node_metadata = lookup(each.value, "node_metadata", workload_metadata_config.value.node_metadata) } } diff --git a/modules/beta-public-cluster/main.tf b/modules/beta-public-cluster/main.tf index 963f256c9..4b03ff52d 100644 --- a/modules/beta-public-cluster/main.tf +++ b/modules/beta-public-cluster/main.tf @@ -44,7 +44,12 @@ locals { node_version_zonal = var.node_version != "" && ! var.regional ? var.node_version : local.master_version_zonal master_version = var.regional ? local.master_version_regional : local.master_version_zonal node_version = var.regional ? local.node_version_regional : local.node_version_zonal - release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : [] + + // Build a map of maps of node pools from a list of objects + node_pool_names = [for np in toset(var.node_pools) : np.name] + node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools))) + + release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : [] autoscalling_resource_limits = var.cluster_autoscaling.enabled ? [{ resource_type = "cpu" @@ -115,8 +120,8 @@ locals { cidr_blocks : var.master_authorized_networks }] - cluster_output_node_pools_names = concat(google_container_node_pool.pools.*.name, [""]) - cluster_output_node_pools_versions = concat(google_container_node_pool.pools.*.version, [""]) + cluster_output_node_pools_names = concat([for np in google_container_node_pool.pools : np.name], [""]) + cluster_output_node_pools_versions = concat([for np in google_container_node_pool.pools : np.version], [""]) cluster_master_auth_list_layer1 = local.cluster_output_master_auth cluster_master_auth_list_layer2 = local.cluster_master_auth_list_layer1[0] diff --git a/modules/beta-public-cluster/versions.tf b/modules/beta-public-cluster/versions.tf index 89cd6ff0d..0c5d256a5 100644 --- a/modules/beta-public-cluster/versions.tf +++ b/modules/beta-public-cluster/versions.tf @@ -15,7 +15,7 @@ */ terraform { - required_version = ">= 0.12" + required_version = "~> 0.12.6" required_providers { google-beta = ">= 2.18, <4.0.0" diff --git a/modules/private-cluster-update-variant/cluster.tf b/modules/private-cluster-update-variant/cluster.tf index f9fe81061..7bf369be8 100644 --- a/modules/private-cluster-update-variant/cluster.tf +++ b/modules/private-cluster-update-variant/cluster.tf @@ -151,13 +151,13 @@ locals { # resources where "ForceNew" is "true". schemaNodeConfig can be found in node_config.go at # https://github.com/terraform-providers/terraform-provider-google/blob/master/google/node_config.go#L22 resource "random_id" "name" { - count = length(var.node_pools) + for_each = local.node_pools byte_length = 2 - prefix = format("%s-", lookup(var.node_pools[count.index], "name")) + prefix = format("%s-", lookup(each.value, "name")) keepers = merge( zipmap( local.force_node_pool_recreation_resources, - [for keeper in local.force_node_pool_recreation_resources : lookup(var.node_pools[count.index], keeper, "")] + [for keeper in local.force_node_pool_recreation_resources : lookup(each.value, keeper, "")] ), { labels = join(",", @@ -165,8 +165,8 @@ resource "random_id" "name" { concat( keys(local.node_pools_labels["all"]), values(local.node_pools_labels["all"]), - keys(local.node_pools_labels[var.node_pools[count.index]["name"]]), - values(local.node_pools_labels[var.node_pools[count.index]["name"]]) + keys(local.node_pools_labels[each.value["name"]]), + values(local.node_pools_labels[each.value["name"]]) ) ) ) @@ -177,8 +177,8 @@ resource "random_id" "name" { concat( keys(local.node_pools_metadata["all"]), values(local.node_pools_metadata["all"]), - keys(local.node_pools_metadata[var.node_pools[count.index]["name"]]), - values(local.node_pools_metadata[var.node_pools[count.index]["name"]]) + keys(local.node_pools_metadata[each.value["name"]]), + values(local.node_pools_metadata[each.value["name"]]) ) ) ) @@ -188,7 +188,7 @@ resource "random_id" "name" { sort( concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]] + local.node_pools_oauth_scopes[each.value["name"]] ) ) ) @@ -198,7 +198,7 @@ resource "random_id" "name" { sort( concat( local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]] + local.node_pools_tags[each.value["name"]] ) ) ) @@ -208,30 +208,30 @@ resource "random_id" "name" { resource "google_container_node_pool" "pools" { provider = google - count = length(var.node_pools) - name = random_id.name.*.hex[count.index] + for_each = local.node_pools + name = random_id.name.*.hex[each.key] project = var.project_id location = local.location cluster = google_container_cluster.primary.name - version = lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup( - var.node_pools[count.index], + version = lookup(each.value, "auto_upgrade", false) ? "" : lookup( + each.value, "version", local.node_version, ) - initial_node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? lookup( - var.node_pools[count.index], + initial_node_count = lookup(each.value, "autoscaling", true) ? lookup( + each.value, "initial_node_count", - lookup(var.node_pools[count.index], "min_count", 1) + lookup(each.value, "min_count", 1) ) : null - node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? null : lookup(var.node_pools[count.index], "node_count", 1) + node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1) dynamic "autoscaling" { - for_each = lookup(var.node_pools[count.index], "autoscaling", true) ? [var.node_pools[count.index]] : [] + for_each = lookup(each.value, "autoscaling", true) ? [each.value] : [] content { min_node_count = lookup(autoscaling.value, "min_count", 1) max_node_count = lookup(autoscaling.value, "max_count", 100) @@ -239,55 +239,55 @@ resource "google_container_node_pool" "pools" { } management { - auto_repair = lookup(var.node_pools[count.index], "auto_repair", true) - auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", local.default_auto_upgrade) + auto_repair = lookup(each.value, "auto_repair", true) + auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade) } node_config { - image_type = lookup(var.node_pools[count.index], "image_type", "COS") - machine_type = lookup(var.node_pools[count.index], "machine_type", "n1-standard-2") + image_type = lookup(each.value, "image_type", "COS") + machine_type = lookup(each.value, "machine_type", "n1-standard-2") labels = merge( lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_labels["all"], - local.node_pools_labels[var.node_pools[count.index]["name"]], + local.node_pools_labels[each.value["name"]], ) metadata = merge( lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_metadata["all"], - local.node_pools_metadata[var.node_pools[count.index]["name"]], + local.node_pools_metadata[each.value["name"]], { "disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints }, ) tags = concat( lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [], - lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${var.node_pools[count.index]["name"]}"] : [], + lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [], local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]], + local.node_pools_tags[each.value["name"]], ) - local_ssd_count = lookup(var.node_pools[count.index], "local_ssd_count", 0) - disk_size_gb = lookup(var.node_pools[count.index], "disk_size_gb", 100) - disk_type = lookup(var.node_pools[count.index], "disk_type", "pd-standard") + local_ssd_count = lookup(each.value, "local_ssd_count", 0) + disk_size_gb = lookup(each.value, "disk_size_gb", 100) + disk_type = lookup(each.value, "disk_type", "pd-standard") service_account = lookup( - var.node_pools[count.index], + each.value, "service_account", local.service_account, ) - preemptible = lookup(var.node_pools[count.index], "preemptible", false) + preemptible = lookup(each.value, "preemptible", false) oauth_scopes = concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]], + local.node_pools_oauth_scopes[each.value["name"]], ) guest_accelerator = [ - for guest_accelerator in lookup(var.node_pools[count.index], "accelerator_count", 0) > 0 ? [{ - type = lookup(var.node_pools[count.index], "accelerator_type", "") - count = lookup(var.node_pools[count.index], "accelerator_count", 0) + for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{ + type = lookup(each.value, "accelerator_type", "") + count = lookup(each.value, "accelerator_count", 0) }] : [] : { type = guest_accelerator["type"] count = guest_accelerator["count"] diff --git a/modules/private-cluster-update-variant/main.tf b/modules/private-cluster-update-variant/main.tf index 3307ea82b..e40715599 100644 --- a/modules/private-cluster-update-variant/main.tf +++ b/modules/private-cluster-update-variant/main.tf @@ -45,6 +45,11 @@ locals { master_version = var.regional ? local.master_version_regional : local.master_version_zonal node_version = var.regional ? local.node_version_regional : local.node_version_zonal + // Build a map of maps of node pools from a list of objects + node_pool_names = [for np in toset(var.node_pools) : np.name] + node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools))) + + custom_kube_dns_config = length(keys(var.stub_domains)) > 0 upstream_nameservers_config = length(var.upstream_nameservers) > 0 @@ -84,8 +89,8 @@ locals { cidr_blocks : var.master_authorized_networks }] - cluster_output_node_pools_names = concat(google_container_node_pool.pools.*.name, [""]) - cluster_output_node_pools_versions = concat(google_container_node_pool.pools.*.version, [""]) + cluster_output_node_pools_names = concat([for np in google_container_node_pool.pools : np.name], [""]) + cluster_output_node_pools_versions = concat([for np in google_container_node_pool.pools : np.version], [""]) cluster_master_auth_list_layer1 = local.cluster_output_master_auth cluster_master_auth_list_layer2 = local.cluster_master_auth_list_layer1[0] diff --git a/modules/private-cluster-update-variant/versions.tf b/modules/private-cluster-update-variant/versions.tf index 9257d4240..1bdc2f911 100644 --- a/modules/private-cluster-update-variant/versions.tf +++ b/modules/private-cluster-update-variant/versions.tf @@ -15,7 +15,7 @@ */ terraform { - required_version = ">= 0.12" + required_version = "~> 0.12.6" required_providers { google = ">= 2.18, <4.0.0" diff --git a/modules/private-cluster/cluster.tf b/modules/private-cluster/cluster.tf index c1a4f3fd1..b1fb0d2ff 100644 --- a/modules/private-cluster/cluster.tf +++ b/modules/private-cluster/cluster.tf @@ -136,30 +136,30 @@ resource "google_container_cluster" "primary" { *****************************************/ resource "google_container_node_pool" "pools" { provider = google - count = length(var.node_pools) - name = var.node_pools[count.index]["name"] + for_each = local.node_pools + name = each.key project = var.project_id location = local.location cluster = google_container_cluster.primary.name - version = lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup( - var.node_pools[count.index], + version = lookup(each.value, "auto_upgrade", false) ? "" : lookup( + each.value, "version", local.node_version, ) - initial_node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? lookup( - var.node_pools[count.index], + initial_node_count = lookup(each.value, "autoscaling", true) ? lookup( + each.value, "initial_node_count", - lookup(var.node_pools[count.index], "min_count", 1) + lookup(each.value, "min_count", 1) ) : null - node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? null : lookup(var.node_pools[count.index], "node_count", 1) + node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1) dynamic "autoscaling" { - for_each = lookup(var.node_pools[count.index], "autoscaling", true) ? [var.node_pools[count.index]] : [] + for_each = lookup(each.value, "autoscaling", true) ? [each.value] : [] content { min_node_count = lookup(autoscaling.value, "min_count", 1) max_node_count = lookup(autoscaling.value, "max_count", 100) @@ -167,55 +167,55 @@ resource "google_container_node_pool" "pools" { } management { - auto_repair = lookup(var.node_pools[count.index], "auto_repair", true) - auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", local.default_auto_upgrade) + auto_repair = lookup(each.value, "auto_repair", true) + auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade) } node_config { - image_type = lookup(var.node_pools[count.index], "image_type", "COS") - machine_type = lookup(var.node_pools[count.index], "machine_type", "n1-standard-2") + image_type = lookup(each.value, "image_type", "COS") + machine_type = lookup(each.value, "machine_type", "n1-standard-2") labels = merge( lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_labels["all"], - local.node_pools_labels[var.node_pools[count.index]["name"]], + local.node_pools_labels[each.value["name"]], ) metadata = merge( lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {}, - lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {}, + lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {}, local.node_pools_metadata["all"], - local.node_pools_metadata[var.node_pools[count.index]["name"]], + local.node_pools_metadata[each.value["name"]], { "disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints }, ) tags = concat( lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [], - lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${var.node_pools[count.index]["name"]}"] : [], + lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [], local.node_pools_tags["all"], - local.node_pools_tags[var.node_pools[count.index]["name"]], + local.node_pools_tags[each.value["name"]], ) - local_ssd_count = lookup(var.node_pools[count.index], "local_ssd_count", 0) - disk_size_gb = lookup(var.node_pools[count.index], "disk_size_gb", 100) - disk_type = lookup(var.node_pools[count.index], "disk_type", "pd-standard") + local_ssd_count = lookup(each.value, "local_ssd_count", 0) + disk_size_gb = lookup(each.value, "disk_size_gb", 100) + disk_type = lookup(each.value, "disk_type", "pd-standard") service_account = lookup( - var.node_pools[count.index], + each.value, "service_account", local.service_account, ) - preemptible = lookup(var.node_pools[count.index], "preemptible", false) + preemptible = lookup(each.value, "preemptible", false) oauth_scopes = concat( local.node_pools_oauth_scopes["all"], - local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]], + local.node_pools_oauth_scopes[each.value["name"]], ) guest_accelerator = [ - for guest_accelerator in lookup(var.node_pools[count.index], "accelerator_count", 0) > 0 ? [{ - type = lookup(var.node_pools[count.index], "accelerator_type", "") - count = lookup(var.node_pools[count.index], "accelerator_count", 0) + for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{ + type = lookup(each.value, "accelerator_type", "") + count = lookup(each.value, "accelerator_count", 0) }] : [] : { type = guest_accelerator["type"] count = guest_accelerator["count"] diff --git a/modules/private-cluster/main.tf b/modules/private-cluster/main.tf index 3307ea82b..e40715599 100644 --- a/modules/private-cluster/main.tf +++ b/modules/private-cluster/main.tf @@ -45,6 +45,11 @@ locals { master_version = var.regional ? local.master_version_regional : local.master_version_zonal node_version = var.regional ? local.node_version_regional : local.node_version_zonal + // Build a map of maps of node pools from a list of objects + node_pool_names = [for np in toset(var.node_pools) : np.name] + node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools))) + + custom_kube_dns_config = length(keys(var.stub_domains)) > 0 upstream_nameservers_config = length(var.upstream_nameservers) > 0 @@ -84,8 +89,8 @@ locals { cidr_blocks : var.master_authorized_networks }] - cluster_output_node_pools_names = concat(google_container_node_pool.pools.*.name, [""]) - cluster_output_node_pools_versions = concat(google_container_node_pool.pools.*.version, [""]) + cluster_output_node_pools_names = concat([for np in google_container_node_pool.pools : np.name], [""]) + cluster_output_node_pools_versions = concat([for np in google_container_node_pool.pools : np.version], [""]) cluster_master_auth_list_layer1 = local.cluster_output_master_auth cluster_master_auth_list_layer2 = local.cluster_master_auth_list_layer1[0] diff --git a/modules/private-cluster/versions.tf b/modules/private-cluster/versions.tf index 9257d4240..1bdc2f911 100644 --- a/modules/private-cluster/versions.tf +++ b/modules/private-cluster/versions.tf @@ -15,7 +15,7 @@ */ terraform { - required_version = ">= 0.12" + required_version = "~> 0.12.6" required_providers { google = ">= 2.18, <4.0.0" diff --git a/versions.tf b/versions.tf index 9257d4240..1bdc2f911 100644 --- a/versions.tf +++ b/versions.tf @@ -15,7 +15,7 @@ */ terraform { - required_version = ">= 0.12" + required_version = "~> 0.12.6" required_providers { google = ">= 2.18, <4.0.0"