Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to using for_each for node pools #257

Merged
merged 9 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions autogen/main/cluster.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,22 @@ 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(",",
sort(
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"]])
)
)
)
Expand All @@ -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"]])
)
)
)
Expand All @@ -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"]]
)
)
)
Expand All @@ -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"]]
)
)
)
Expand All @@ -336,66 +336,66 @@ 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)
}
}

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
},
Expand All @@ -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
Expand All @@ -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"]
Expand All @@ -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)
}
}

Expand Down
9 changes: 7 additions & 2 deletions autogen/main/main.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }] : []

Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion autogen/main/versions.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

terraform {
required_version = ">= 0.12"
required_version = "~> 0.12.6"

required_providers {
{% if beta_cluster %}
Expand Down
56 changes: 28 additions & 28 deletions cluster.tf
Original file line number Diff line number Diff line change
Expand Up @@ -123,86 +123,86 @@ 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)
}
}

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"]
Expand Down
9 changes: 7 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
Loading