Skip to content

Commit 7d0c9aa

Browse files
Dev25morgante
authored andcommitted
feat: move to using for_each for node pools (#257)
BREAKING CHANGE: moves node pool state location to allow using for_each on them
1 parent 7cc2792 commit 7d0c9aa

File tree

21 files changed

+299
-264
lines changed

21 files changed

+299
-264
lines changed

autogen/main/cluster.tf.tmpl

+42-42
Original file line numberDiff line numberDiff line change
@@ -274,22 +274,22 @@ locals {
274274
# resources where "ForceNew" is "true". schemaNodeConfig can be found in node_config.go at
275275
# https://github.com/terraform-providers/terraform-provider-google/blob/master/google/node_config.go#L22
276276
resource "random_id" "name" {
277-
count = length(var.node_pools)
277+
for_each = local.node_pools
278278
byte_length = 2
279-
prefix = format("%s-", lookup(var.node_pools[count.index], "name"))
279+
prefix = format("%s-", lookup(each.value, "name"))
280280
keepers = merge(
281281
zipmap(
282282
local.force_node_pool_recreation_resources,
283-
[for keeper in local.force_node_pool_recreation_resources : lookup(var.node_pools[count.index], keeper, "")]
283+
[for keeper in local.force_node_pool_recreation_resources : lookup(each.value, keeper, "")]
284284
),
285285
{
286286
labels = join(",",
287287
sort(
288288
concat(
289289
keys(local.node_pools_labels["all"]),
290290
values(local.node_pools_labels["all"]),
291-
keys(local.node_pools_labels[var.node_pools[count.index]["name"]]),
292-
values(local.node_pools_labels[var.node_pools[count.index]["name"]])
291+
keys(local.node_pools_labels[each.value["name"]]),
292+
values(local.node_pools_labels[each.value["name"]])
293293
)
294294
)
295295
)
@@ -300,8 +300,8 @@ resource "random_id" "name" {
300300
concat(
301301
keys(local.node_pools_metadata["all"]),
302302
values(local.node_pools_metadata["all"]),
303-
keys(local.node_pools_metadata[var.node_pools[count.index]["name"]]),
304-
values(local.node_pools_metadata[var.node_pools[count.index]["name"]])
303+
keys(local.node_pools_metadata[each.value["name"]]),
304+
values(local.node_pools_metadata[each.value["name"]])
305305
)
306306
)
307307
)
@@ -311,7 +311,7 @@ resource "random_id" "name" {
311311
sort(
312312
concat(
313313
local.node_pools_oauth_scopes["all"],
314-
local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]]
314+
local.node_pools_oauth_scopes[each.value["name"]]
315315
)
316316
)
317317
)
@@ -321,7 +321,7 @@ resource "random_id" "name" {
321321
sort(
322322
concat(
323323
local.node_pools_tags["all"],
324-
local.node_pools_tags[var.node_pools[count.index]["name"]]
324+
local.node_pools_tags[each.value["name"]]
325325
)
326326
)
327327
)
@@ -336,66 +336,66 @@ resource "google_container_node_pool" "pools" {
336336
{% else %}
337337
provider = google
338338
{% endif %}
339-
count = length(var.node_pools)
339+
for_each = local.node_pools
340340
{% if update_variant %}
341-
name = random_id.name.*.hex[count.index]
341+
name = random_id.name.*.hex[each.key]
342342
{% else %}
343-
name = var.node_pools[count.index]["name"]
343+
name = each.key
344344
{% endif %}
345345
project = var.project_id
346346
location = local.location
347347
{% if beta_cluster %}
348348
// use node_locations if provided, defaults to cluster level node_locations if not specified
349-
node_locations = lookup(var.node_pools[count.index], "node_locations", "") != "" ? split(",", var.node_pools[count.index]["node_locations"]) : null
349+
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
350350
{% endif %}
351351

352352
cluster = google_container_cluster.primary.name
353353

354-
version = lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup(
355-
var.node_pools[count.index],
354+
version = lookup(each.value, "auto_upgrade", false) ? "" : lookup(
355+
each.value,
356356
"version",
357357
local.node_version,
358358
)
359359

360-
initial_node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? lookup(
361-
var.node_pools[count.index],
360+
initial_node_count = lookup(each.value, "autoscaling", true) ? lookup(
361+
each.value,
362362
"initial_node_count",
363-
lookup(var.node_pools[count.index], "min_count", 1)
363+
lookup(each.value, "min_count", 1)
364364
) : null
365365

366366
{% if beta_cluster %}
367-
max_pods_per_node = lookup(var.node_pools[count.index], "max_pods_per_node", null)
367+
max_pods_per_node = lookup(each.value, "max_pods_per_node", null)
368368
{% endif %}
369369

370-
node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? null : lookup(var.node_pools[count.index], "node_count", 1)
370+
node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1)
371371

372372
dynamic "autoscaling" {
373-
for_each = lookup(var.node_pools[count.index], "autoscaling", true) ? [var.node_pools[count.index]] : []
373+
for_each = lookup(each.value, "autoscaling", true) ? [each.value] : []
374374
content {
375375
min_node_count = lookup(autoscaling.value, "min_count", 1)
376376
max_node_count = lookup(autoscaling.value, "max_count", 100)
377377
}
378378
}
379379

380380
management {
381-
auto_repair = lookup(var.node_pools[count.index], "auto_repair", true)
382-
auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", local.default_auto_upgrade)
381+
auto_repair = lookup(each.value, "auto_repair", true)
382+
auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade)
383383
}
384384

385385
node_config {
386-
image_type = lookup(var.node_pools[count.index], "image_type", "COS")
387-
machine_type = lookup(var.node_pools[count.index], "machine_type", "n1-standard-2")
386+
image_type = lookup(each.value, "image_type", "COS")
387+
machine_type = lookup(each.value, "machine_type", "n1-standard-2")
388388
labels = merge(
389389
lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {},
390-
lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {},
390+
lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {},
391391
local.node_pools_labels["all"],
392-
local.node_pools_labels[var.node_pools[count.index]["name"]],
392+
local.node_pools_labels[each.value["name"]],
393393
)
394394
metadata = merge(
395395
lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {},
396-
lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {},
396+
lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {},
397397
local.node_pools_metadata["all"],
398-
local.node_pools_metadata[var.node_pools[count.index]["name"]],
398+
local.node_pools_metadata[each.value["name"]],
399399
{
400400
"disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints
401401
},
@@ -404,7 +404,7 @@ resource "google_container_node_pool" "pools" {
404404
dynamic "taint" {
405405
for_each = concat(
406406
local.node_pools_taints["all"],
407-
local.node_pools_taints[var.node_pools[count.index]["name"]],
407+
local.node_pools_taints[each.value["name"]],
408408
)
409409
content {
410410
effect = taint.value.effect
@@ -415,31 +415,31 @@ resource "google_container_node_pool" "pools" {
415415
{% endif %}
416416
tags = concat(
417417
lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [],
418-
lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${var.node_pools[count.index]["name"]}"] : [],
418+
lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [],
419419
local.node_pools_tags["all"],
420-
local.node_pools_tags[var.node_pools[count.index]["name"]],
420+
local.node_pools_tags[each.value["name"]],
421421
)
422422

423-
local_ssd_count = lookup(var.node_pools[count.index], "local_ssd_count", 0)
424-
disk_size_gb = lookup(var.node_pools[count.index], "disk_size_gb", 100)
425-
disk_type = lookup(var.node_pools[count.index], "disk_type", "pd-standard")
423+
local_ssd_count = lookup(each.value, "local_ssd_count", 0)
424+
disk_size_gb = lookup(each.value, "disk_size_gb", 100)
425+
disk_type = lookup(each.value, "disk_type", "pd-standard")
426426

427427
service_account = lookup(
428-
var.node_pools[count.index],
428+
each.value,
429429
"service_account",
430430
local.service_account,
431431
)
432-
preemptible = lookup(var.node_pools[count.index], "preemptible", false)
432+
preemptible = lookup(each.value, "preemptible", false)
433433

434434
oauth_scopes = concat(
435435
local.node_pools_oauth_scopes["all"],
436-
local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]],
436+
local.node_pools_oauth_scopes[each.value["name"]],
437437
)
438438

439439
guest_accelerator = [
440-
for guest_accelerator in lookup(var.node_pools[count.index], "accelerator_count", 0) > 0 ? [{
441-
type = lookup(var.node_pools[count.index], "accelerator_type", "")
442-
count = lookup(var.node_pools[count.index], "accelerator_count", 0)
440+
for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{
441+
type = lookup(each.value, "accelerator_type", "")
442+
count = lookup(each.value, "accelerator_count", 0)
443443
}] : [] : {
444444
type = guest_accelerator["type"]
445445
count = guest_accelerator["count"]
@@ -451,7 +451,7 @@ resource "google_container_node_pool" "pools" {
451451
for_each = local.cluster_node_metadata_config
452452

453453
content {
454-
node_metadata = lookup(var.node_pools[count.index], "node_metadata", workload_metadata_config.value.node_metadata)
454+
node_metadata = lookup(each.value, "node_metadata", workload_metadata_config.value.node_metadata)
455455
}
456456
}
457457

autogen/main/main.tf.tmpl

+7-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ locals {
4848
node_version_zonal = var.node_version != "" && ! var.regional ? var.node_version : local.master_version_zonal
4949
master_version = var.regional ? local.master_version_regional : local.master_version_zonal
5050
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
51+
52+
// Build a map of maps of node pools from a list of objects
53+
node_pool_names = [for np in toset(var.node_pools) : np.name]
54+
node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools)))
55+
5156
{% if beta_cluster %}
5257
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
5358

@@ -129,8 +134,8 @@ locals {
129134
cidr_blocks : var.master_authorized_networks
130135
}]
131136

132-
cluster_output_node_pools_names = concat(google_container_node_pool.pools.*.name, [""])
133-
cluster_output_node_pools_versions = concat(google_container_node_pool.pools.*.version, [""])
137+
cluster_output_node_pools_names = concat([for np in google_container_node_pool.pools : np.name], [""])
138+
cluster_output_node_pools_versions = concat([for np in google_container_node_pool.pools : np.version], [""])
134139

135140
cluster_master_auth_list_layer1 = local.cluster_output_master_auth
136141
cluster_master_auth_list_layer2 = local.cluster_master_auth_list_layer1[0]

autogen/main/versions.tf.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
terraform {
18-
required_version = ">= 0.12"
18+
required_version = "~> 0.12.6"
1919

2020
required_providers {
2121
{% if beta_cluster %}

cluster.tf

+28-28
Original file line numberDiff line numberDiff line change
@@ -123,86 +123,86 @@ resource "google_container_cluster" "primary" {
123123
*****************************************/
124124
resource "google_container_node_pool" "pools" {
125125
provider = google
126-
count = length(var.node_pools)
127-
name = var.node_pools[count.index]["name"]
126+
for_each = local.node_pools
127+
name = each.key
128128
project = var.project_id
129129
location = local.location
130130

131131
cluster = google_container_cluster.primary.name
132132

133-
version = lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup(
134-
var.node_pools[count.index],
133+
version = lookup(each.value, "auto_upgrade", false) ? "" : lookup(
134+
each.value,
135135
"version",
136136
local.node_version,
137137
)
138138

139-
initial_node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? lookup(
140-
var.node_pools[count.index],
139+
initial_node_count = lookup(each.value, "autoscaling", true) ? lookup(
140+
each.value,
141141
"initial_node_count",
142-
lookup(var.node_pools[count.index], "min_count", 1)
142+
lookup(each.value, "min_count", 1)
143143
) : null
144144

145145

146-
node_count = lookup(var.node_pools[count.index], "autoscaling", true) ? null : lookup(var.node_pools[count.index], "node_count", 1)
146+
node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1)
147147

148148
dynamic "autoscaling" {
149-
for_each = lookup(var.node_pools[count.index], "autoscaling", true) ? [var.node_pools[count.index]] : []
149+
for_each = lookup(each.value, "autoscaling", true) ? [each.value] : []
150150
content {
151151
min_node_count = lookup(autoscaling.value, "min_count", 1)
152152
max_node_count = lookup(autoscaling.value, "max_count", 100)
153153
}
154154
}
155155

156156
management {
157-
auto_repair = lookup(var.node_pools[count.index], "auto_repair", true)
158-
auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", local.default_auto_upgrade)
157+
auto_repair = lookup(each.value, "auto_repair", true)
158+
auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade)
159159
}
160160

161161
node_config {
162-
image_type = lookup(var.node_pools[count.index], "image_type", "COS")
163-
machine_type = lookup(var.node_pools[count.index], "machine_type", "n1-standard-2")
162+
image_type = lookup(each.value, "image_type", "COS")
163+
machine_type = lookup(each.value, "machine_type", "n1-standard-2")
164164
labels = merge(
165165
lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {},
166-
lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {},
166+
lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {},
167167
local.node_pools_labels["all"],
168-
local.node_pools_labels[var.node_pools[count.index]["name"]],
168+
local.node_pools_labels[each.value["name"]],
169169
)
170170
metadata = merge(
171171
lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {},
172-
lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = var.node_pools[count.index]["name"] } : {},
172+
lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {},
173173
local.node_pools_metadata["all"],
174-
local.node_pools_metadata[var.node_pools[count.index]["name"]],
174+
local.node_pools_metadata[each.value["name"]],
175175
{
176176
"disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints
177177
},
178178
)
179179
tags = concat(
180180
lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [],
181-
lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${var.node_pools[count.index]["name"]}"] : [],
181+
lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [],
182182
local.node_pools_tags["all"],
183-
local.node_pools_tags[var.node_pools[count.index]["name"]],
183+
local.node_pools_tags[each.value["name"]],
184184
)
185185

186-
local_ssd_count = lookup(var.node_pools[count.index], "local_ssd_count", 0)
187-
disk_size_gb = lookup(var.node_pools[count.index], "disk_size_gb", 100)
188-
disk_type = lookup(var.node_pools[count.index], "disk_type", "pd-standard")
186+
local_ssd_count = lookup(each.value, "local_ssd_count", 0)
187+
disk_size_gb = lookup(each.value, "disk_size_gb", 100)
188+
disk_type = lookup(each.value, "disk_type", "pd-standard")
189189

190190
service_account = lookup(
191-
var.node_pools[count.index],
191+
each.value,
192192
"service_account",
193193
local.service_account,
194194
)
195-
preemptible = lookup(var.node_pools[count.index], "preemptible", false)
195+
preemptible = lookup(each.value, "preemptible", false)
196196

197197
oauth_scopes = concat(
198198
local.node_pools_oauth_scopes["all"],
199-
local.node_pools_oauth_scopes[var.node_pools[count.index]["name"]],
199+
local.node_pools_oauth_scopes[each.value["name"]],
200200
)
201201

202202
guest_accelerator = [
203-
for guest_accelerator in lookup(var.node_pools[count.index], "accelerator_count", 0) > 0 ? [{
204-
type = lookup(var.node_pools[count.index], "accelerator_type", "")
205-
count = lookup(var.node_pools[count.index], "accelerator_count", 0)
203+
for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{
204+
type = lookup(each.value, "accelerator_type", "")
205+
count = lookup(each.value, "accelerator_count", 0)
206206
}] : [] : {
207207
type = guest_accelerator["type"]
208208
count = guest_accelerator["count"]

main.tf

+7-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ locals {
4545
master_version = var.regional ? local.master_version_regional : local.master_version_zonal
4646
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
4747

48+
// Build a map of maps of node pools from a list of objects
49+
node_pool_names = [for np in toset(var.node_pools) : np.name]
50+
node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools)))
51+
52+
4853

4954
custom_kube_dns_config = length(keys(var.stub_domains)) > 0
5055
upstream_nameservers_config = length(var.upstream_nameservers) > 0
@@ -84,8 +89,8 @@ locals {
8489
cidr_blocks : var.master_authorized_networks
8590
}]
8691

87-
cluster_output_node_pools_names = concat(google_container_node_pool.pools.*.name, [""])
88-
cluster_output_node_pools_versions = concat(google_container_node_pool.pools.*.version, [""])
92+
cluster_output_node_pools_names = concat([for np in google_container_node_pool.pools : np.name], [""])
93+
cluster_output_node_pools_versions = concat([for np in google_container_node_pool.pools : np.version], [""])
8994

9095
cluster_master_auth_list_layer1 = local.cluster_output_master_auth
9196
cluster_master_auth_list_layer2 = local.cluster_master_auth_list_layer1[0]

0 commit comments

Comments
 (0)