Skip to content

Commit 0e7a249

Browse files
authored
feat: add node_affinity (#2295)
1 parent d49c3bf commit 0e7a249

File tree

17 files changed

+374
-0
lines changed

17 files changed

+374
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ The node_pools variable takes the following parameters:
356356
| placement_policy | Placement type to set for nodes in a node pool. Can be set as [COMPACT](https://cloud.google.com/kubernetes-engine/docs/how-to/compact-placement#overview) if desired | | Optional |
357357
| pod_range | The name of the secondary range for pod IPs. | | Optional |
358358
| enable_private_nodes | Whether nodes have internal IP addresses only. | | Optional |
359+
| node_affinity | The node affinty in the format `"{\"key\": \"compute.googleapis.com/node-group-name\", \"operator\": \"IN\", \"values\": [\"node-group-name\"]}"`. | | Optional |
359360
| node_count | The number of nodes in the nodepool when autoscaling is false. Otherwise defaults to 1. Only valid for non-autoscaling clusters | | Required |
360361
| node_locations | The list of zones in which the cluster's nodes are located. Nodes must be in the region of their regional cluster or in the same region as their cluster's zone for zonal clusters. Defaults to cluster level node locations if nothing is specified | " " | Optional |
361362
| node_metadata | Options to expose the node metadata to the workload running on the node | | Optional |

autogen/main/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ The node_pools variable takes the following parameters:
245245
{% if not private_cluster %}
246246
| enable_private_nodes | Whether nodes have internal IP addresses only. | | Optional |
247247
{% endif %}
248+
| node_affinity | The node affinty in the format `"{\"key\": \"compute.googleapis.com/node-group-name\", \"operator\": \"IN\", \"values\": [\"node-group-name\"]}"`. | | Optional |
248249
| node_count | The number of nodes in the nodepool when autoscaling is false. Otherwise defaults to 1. Only valid for non-autoscaling clusters | | Required |
249250
| node_locations | The list of zones in which the cluster's nodes are located. Nodes must be in the region of their regional cluster or in the same region as their cluster's zone for zonal clusters. Defaults to cluster level node locations if nothing is specified | " " | Optional |
250251
| node_metadata | Options to expose the node metadata to the workload running on the node | | Optional |

autogen/main/cluster.tf.tmpl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,21 @@ resource "google_container_cluster" "primary" {
565565
}
566566
}
567567

568+
dynamic "sole_tenant_config" {
569+
# node_affinity is currently the only member of sole_tenant_config
570+
for_each = lookup(var.node_pools[0], "node_affinity", null) != null ? [true] : []
571+
content {
572+
dynamic "node_affinity" {
573+
for_each = lookup(var.node_pools[0], "node_affinity", null) != null ? [lookup(var.node_pools[0], "node_affinity", null)] : []
574+
content {
575+
key = lookup(jsondecode(node_affinity.value), "key", null)
576+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
577+
values = lookup(jsondecode(node_affinity.value), "values", [])
578+
}
579+
}
580+
}
581+
}
582+
568583
service_account = lookup(var.node_pools[0], "service_account", local.service_account)
569584

570585
tags = concat(
@@ -1094,6 +1109,21 @@ resource "google_container_node_pool" "windows_pools" {
10941109
pod_pids_limit = lookup(each.value, "pod_pids_limit", null)
10951110
}
10961111
}
1112+
1113+
dynamic "sole_tenant_config" {
1114+
# node_affinity is currently the only member of sole_tenant_config
1115+
for_each = lookup(each.value, "node_affinity", null) != null ? [true] : []
1116+
content {
1117+
dynamic "node_affinity" {
1118+
for_each = lookup(each.value, "node_affinity", null) != null ? [lookup(each.value, "node_affinity", null)] : []
1119+
content {
1120+
key = lookup(jsondecode(node_affinity.value), "key", null)
1121+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
1122+
values = lookup(jsondecode(node_affinity.value), "values", [])
1123+
}
1124+
}
1125+
}
1126+
}
10971127
{% if beta_cluster %}
10981128

10991129
dynamic "sandbox_config" {

cluster.tf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,21 @@ resource "google_container_cluster" "primary" {
441441
}
442442
}
443443

444+
dynamic "sole_tenant_config" {
445+
# node_affinity is currently the only member of sole_tenant_config
446+
for_each = lookup(var.node_pools[0], "node_affinity", null) != null ? [true] : []
447+
content {
448+
dynamic "node_affinity" {
449+
for_each = lookup(var.node_pools[0], "node_affinity", null) != null ? [lookup(var.node_pools[0], "node_affinity", null)] : []
450+
content {
451+
key = lookup(jsondecode(node_affinity.value), "key", null)
452+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
453+
values = lookup(jsondecode(node_affinity.value), "values", [])
454+
}
455+
}
456+
}
457+
}
458+
444459
service_account = lookup(var.node_pools[0], "service_account", local.service_account)
445460

446461
tags = concat(
@@ -802,6 +817,21 @@ resource "google_container_node_pool" "pools" {
802817
}
803818
}
804819

820+
dynamic "sole_tenant_config" {
821+
# node_affinity is currently the only member of sole_tenant_config
822+
for_each = lookup(each.value, "node_affinity", null) != null ? [true] : []
823+
content {
824+
dynamic "node_affinity" {
825+
for_each = lookup(each.value, "node_affinity", null) != null ? [lookup(each.value, "node_affinity", null)] : []
826+
content {
827+
key = lookup(jsondecode(node_affinity.value), "key", null)
828+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
829+
values = lookup(jsondecode(node_affinity.value), "values", [])
830+
}
831+
}
832+
}
833+
}
834+
805835
dynamic "linux_node_config" {
806836
for_each = length(merge(
807837
local.node_pools_linux_node_configs_sysctls["all"],
@@ -1103,6 +1133,21 @@ resource "google_container_node_pool" "windows_pools" {
11031133
}
11041134
}
11051135

1136+
dynamic "sole_tenant_config" {
1137+
# node_affinity is currently the only member of sole_tenant_config
1138+
for_each = lookup(each.value, "node_affinity", null) != null ? [true] : []
1139+
content {
1140+
dynamic "node_affinity" {
1141+
for_each = lookup(each.value, "node_affinity", null) != null ? [lookup(each.value, "node_affinity", null)] : []
1142+
content {
1143+
key = lookup(jsondecode(node_affinity.value), "key", null)
1144+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
1145+
values = lookup(jsondecode(node_affinity.value), "values", [])
1146+
}
1147+
}
1148+
}
1149+
}
1150+
11061151

11071152
boot_disk_kms_key = lookup(each.value, "boot_disk_kms_key", "")
11081153

examples/node_pool/main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ module "gke" {
9898
node_count = 1
9999
enable_nested_virtualization = true
100100
},
101+
{
102+
name = "pool-06"
103+
node_count = 1
104+
machine_type = "n1-highmem-96"
105+
node_affinity = "{\"key\": \"compute.googleapis.com/node-group-name\", \"operator\": \"IN\", \"values\": [\"${google_compute_node_group.soletenant-nodes.name}\"]}"
106+
},
101107
]
102108

103109
node_pools_metadata = {
@@ -158,3 +164,18 @@ module "gke" {
158164
pool-01 = "CGROUP_MODE_V2"
159165
}
160166
}
167+
168+
resource "google_compute_node_template" "soletenant-tmpl" {
169+
name = "soletenant-tmpl-${var.cluster_name_suffix}"
170+
region = var.region
171+
172+
node_type = "n1-node-96-624"
173+
}
174+
175+
resource "google_compute_node_group" "soletenant-nodes" {
176+
name = "soletenant-node-group-${var.cluster_name_suffix}"
177+
zone = var.zones[0]
178+
179+
initial_size = 1
180+
node_template = google_compute_node_template.soletenant-tmpl.id
181+
}

modules/beta-private-cluster-update-variant/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ The node_pools variable takes the following parameters:
408408
| name | The name of the node pool | | Required |
409409
| placement_policy | Placement type to set for nodes in a node pool. Can be set as [COMPACT](https://cloud.google.com/kubernetes-engine/docs/how-to/compact-placement#overview) if desired | | Optional |
410410
| pod_range | The name of the secondary range for pod IPs. | | Optional |
411+
| node_affinity | The node affinty in the format `"{\"key\": \"compute.googleapis.com/node-group-name\", \"operator\": \"IN\", \"values\": [\"node-group-name\"]}"`. | | Optional |
411412
| node_count | The number of nodes in the nodepool when autoscaling is false. Otherwise defaults to 1. Only valid for non-autoscaling clusters | | Required |
412413
| node_locations | The list of zones in which the cluster's nodes are located. Nodes must be in the region of their regional cluster or in the same region as their cluster's zone for zonal clusters. Defaults to cluster level node locations if nothing is specified | " " | Optional |
413414
| node_metadata | Options to expose the node metadata to the workload running on the node | | Optional |

modules/beta-private-cluster-update-variant/cluster.tf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,21 @@ resource "google_container_cluster" "primary" {
478478
}
479479
}
480480

481+
dynamic "sole_tenant_config" {
482+
# node_affinity is currently the only member of sole_tenant_config
483+
for_each = lookup(var.node_pools[0], "node_affinity", null) != null ? [true] : []
484+
content {
485+
dynamic "node_affinity" {
486+
for_each = lookup(var.node_pools[0], "node_affinity", null) != null ? [lookup(var.node_pools[0], "node_affinity", null)] : []
487+
content {
488+
key = lookup(jsondecode(node_affinity.value), "key", null)
489+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
490+
values = lookup(jsondecode(node_affinity.value), "values", [])
491+
}
492+
}
493+
}
494+
}
495+
481496
service_account = lookup(var.node_pools[0], "service_account", local.service_account)
482497

483498
tags = concat(
@@ -965,6 +980,21 @@ resource "google_container_node_pool" "pools" {
965980
}
966981
}
967982

983+
dynamic "sole_tenant_config" {
984+
# node_affinity is currently the only member of sole_tenant_config
985+
for_each = lookup(each.value, "node_affinity", null) != null ? [true] : []
986+
content {
987+
dynamic "node_affinity" {
988+
for_each = lookup(each.value, "node_affinity", null) != null ? [lookup(each.value, "node_affinity", null)] : []
989+
content {
990+
key = lookup(jsondecode(node_affinity.value), "key", null)
991+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
992+
values = lookup(jsondecode(node_affinity.value), "values", [])
993+
}
994+
}
995+
}
996+
}
997+
968998
dynamic "sandbox_config" {
969999
for_each = tobool((lookup(each.value, "sandbox_enabled", var.sandbox_enabled))) ? ["gvisor"] : []
9701000
content {
@@ -1280,6 +1310,21 @@ resource "google_container_node_pool" "windows_pools" {
12801310
}
12811311
}
12821312

1313+
dynamic "sole_tenant_config" {
1314+
# node_affinity is currently the only member of sole_tenant_config
1315+
for_each = lookup(each.value, "node_affinity", null) != null ? [true] : []
1316+
content {
1317+
dynamic "node_affinity" {
1318+
for_each = lookup(each.value, "node_affinity", null) != null ? [lookup(each.value, "node_affinity", null)] : []
1319+
content {
1320+
key = lookup(jsondecode(node_affinity.value), "key", null)
1321+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
1322+
values = lookup(jsondecode(node_affinity.value), "values", [])
1323+
}
1324+
}
1325+
}
1326+
}
1327+
12831328
dynamic "sandbox_config" {
12841329
for_each = tobool((lookup(each.value, "sandbox_enabled", var.sandbox_enabled))) ? ["gvisor"] : []
12851330
content {

modules/beta-private-cluster/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ The node_pools variable takes the following parameters:
386386
| name | The name of the node pool | | Required |
387387
| placement_policy | Placement type to set for nodes in a node pool. Can be set as [COMPACT](https://cloud.google.com/kubernetes-engine/docs/how-to/compact-placement#overview) if desired | | Optional |
388388
| pod_range | The name of the secondary range for pod IPs. | | Optional |
389+
| node_affinity | The node affinty in the format `"{\"key\": \"compute.googleapis.com/node-group-name\", \"operator\": \"IN\", \"values\": [\"node-group-name\"]}"`. | | Optional |
389390
| node_count | The number of nodes in the nodepool when autoscaling is false. Otherwise defaults to 1. Only valid for non-autoscaling clusters | | Required |
390391
| node_locations | The list of zones in which the cluster's nodes are located. Nodes must be in the region of their regional cluster or in the same region as their cluster's zone for zonal clusters. Defaults to cluster level node locations if nothing is specified | " " | Optional |
391392
| node_metadata | Options to expose the node metadata to the workload running on the node | | Optional |

modules/beta-private-cluster/cluster.tf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,21 @@ resource "google_container_cluster" "primary" {
478478
}
479479
}
480480

481+
dynamic "sole_tenant_config" {
482+
# node_affinity is currently the only member of sole_tenant_config
483+
for_each = lookup(var.node_pools[0], "node_affinity", null) != null ? [true] : []
484+
content {
485+
dynamic "node_affinity" {
486+
for_each = lookup(var.node_pools[0], "node_affinity", null) != null ? [lookup(var.node_pools[0], "node_affinity", null)] : []
487+
content {
488+
key = lookup(jsondecode(node_affinity.value), "key", null)
489+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
490+
values = lookup(jsondecode(node_affinity.value), "values", [])
491+
}
492+
}
493+
}
494+
}
495+
481496
service_account = lookup(var.node_pools[0], "service_account", local.service_account)
482497

483498
tags = concat(
@@ -884,6 +899,21 @@ resource "google_container_node_pool" "pools" {
884899
}
885900
}
886901

902+
dynamic "sole_tenant_config" {
903+
# node_affinity is currently the only member of sole_tenant_config
904+
for_each = lookup(each.value, "node_affinity", null) != null ? [true] : []
905+
content {
906+
dynamic "node_affinity" {
907+
for_each = lookup(each.value, "node_affinity", null) != null ? [lookup(each.value, "node_affinity", null)] : []
908+
content {
909+
key = lookup(jsondecode(node_affinity.value), "key", null)
910+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
911+
values = lookup(jsondecode(node_affinity.value), "values", [])
912+
}
913+
}
914+
}
915+
}
916+
887917
dynamic "sandbox_config" {
888918
for_each = tobool((lookup(each.value, "sandbox_enabled", var.sandbox_enabled))) ? ["gvisor"] : []
889919
content {
@@ -1198,6 +1228,21 @@ resource "google_container_node_pool" "windows_pools" {
11981228
}
11991229
}
12001230

1231+
dynamic "sole_tenant_config" {
1232+
# node_affinity is currently the only member of sole_tenant_config
1233+
for_each = lookup(each.value, "node_affinity", null) != null ? [true] : []
1234+
content {
1235+
dynamic "node_affinity" {
1236+
for_each = lookup(each.value, "node_affinity", null) != null ? [lookup(each.value, "node_affinity", null)] : []
1237+
content {
1238+
key = lookup(jsondecode(node_affinity.value), "key", null)
1239+
operator = lookup(jsondecode(node_affinity.value), "operator", null)
1240+
values = lookup(jsondecode(node_affinity.value), "values", [])
1241+
}
1242+
}
1243+
}
1244+
}
1245+
12011246
dynamic "sandbox_config" {
12021247
for_each = tobool((lookup(each.value, "sandbox_enabled", var.sandbox_enabled))) ? ["gvisor"] : []
12031248
content {

modules/beta-public-cluster-update-variant/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ The node_pools variable takes the following parameters:
396396
| placement_policy | Placement type to set for nodes in a node pool. Can be set as [COMPACT](https://cloud.google.com/kubernetes-engine/docs/how-to/compact-placement#overview) if desired | | Optional |
397397
| pod_range | The name of the secondary range for pod IPs. | | Optional |
398398
| enable_private_nodes | Whether nodes have internal IP addresses only. | | Optional |
399+
| node_affinity | The node affinty in the format `"{\"key\": \"compute.googleapis.com/node-group-name\", \"operator\": \"IN\", \"values\": [\"node-group-name\"]}"`. | | Optional |
399400
| node_count | The number of nodes in the nodepool when autoscaling is false. Otherwise defaults to 1. Only valid for non-autoscaling clusters | | Required |
400401
| node_locations | The list of zones in which the cluster's nodes are located. Nodes must be in the region of their regional cluster or in the same region as their cluster's zone for zonal clusters. Defaults to cluster level node locations if nothing is specified | " " | Optional |
401402
| node_metadata | Options to expose the node metadata to the workload running on the node | | Optional |

0 commit comments

Comments
 (0)