Skip to content

Commit 155f9b3

Browse files
committed
fix: add target tags to node_pool_auto_config for standard clusters
While terraform-google-modules#1817 added autopilot support for adding tags to `node_pool_auto_config` when `add_cluster_firewall_rules` is set to `true`, the same change did not apply for standard (non-autopilot) clusters with cluster level autoscaling (nodepool autoprovisioning) in place, Fixes terraform-google-modules#2104 Signed-off-by: William Yardley <[email protected]>
1 parent a4b130d commit 155f9b3

File tree

20 files changed

+909
-16
lines changed

20 files changed

+909
-16
lines changed

autogen/main/cluster.tf.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ resource "google_container_cluster" "primary" {
281281

282282
{% if autopilot_cluster != true %}
283283
dynamic "node_pool_auto_config" {
284-
for_each = var.cluster_autoscaling.enabled && length(var.network_tags) > 0 ? [1] : []
284+
for_each = var.cluster_autoscaling.enabled && (length(var.network_tags) > 0 || var.add_cluster_firewall_rules) ? [1] : []
285285
content {
286286
network_tags {
287-
tags = var.network_tags
287+
tags = var.add_cluster_firewall_rules ? (concat(var.network_tags, [local.cluster_network_tag])) : var.network_tags
288288
}
289289
}
290290
}

cluster.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ resource "google_container_cluster" "primary" {
203203
}
204204

205205
dynamic "node_pool_auto_config" {
206-
for_each = var.cluster_autoscaling.enabled && length(var.network_tags) > 0 ? [1] : []
206+
for_each = var.cluster_autoscaling.enabled && (length(var.network_tags) > 0 || var.add_cluster_firewall_rules) ? [1] : []
207207
content {
208208
network_tags {
209-
tags = var.network_tags
209+
tags = var.add_cluster_firewall_rules ? (concat(var.network_tags, [local.cluster_network_tag])) : var.network_tags
210210
}
211211
}
212212
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Simple Regional Cluster with Node Autoscaling
2+
3+
This example illustrates how to create a simple private cluster with cluster
4+
level node autoprovisioning.
5+
6+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
7+
## Inputs
8+
9+
| Name | Description | Type | Default | Required |
10+
|------|-------------|------|---------|:--------:|
11+
| cluster\_name\_suffix | A suffix to append to the default cluster name | `string` | `""` | no |
12+
| compute\_engine\_service\_account | Service account to associate to the nodes in the cluster | `any` | n/a | yes |
13+
| ip\_range\_pods | The secondary ip range to use for pods | `any` | n/a | yes |
14+
| ip\_range\_services | The secondary ip range to use for services | `any` | n/a | yes |
15+
| network | The VPC network to host the cluster in | `any` | n/a | yes |
16+
| project\_id | The project ID to host the cluster in | `any` | n/a | yes |
17+
| region | The region to host the cluster in | `any` | n/a | yes |
18+
| subnetwork | The subnetwork to host the cluster in | `any` | n/a | yes |
19+
20+
## Outputs
21+
22+
| Name | Description |
23+
|------|-------------|
24+
| ca\_certificate | n/a |
25+
| client\_token | n/a |
26+
| cluster\_name | Cluster name |
27+
| ip\_range\_pods | The secondary IP range used for pods |
28+
| ip\_range\_services | The secondary IP range used for services |
29+
| kubernetes\_endpoint | n/a |
30+
| location | n/a |
31+
| master\_kubernetes\_version | The master Kubernetes version |
32+
| network | n/a |
33+
| project\_id | n/a |
34+
| region | n/a |
35+
| service\_account | The default service account used for running nodes. |
36+
| subnetwork | n/a |
37+
| zones | List of zones in which the cluster resides |
38+
39+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2018-2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
locals {
18+
cluster_type = "simple-rgnl-cluster-autosc"
19+
}
20+
21+
data "google_client_config" "default" {}
22+
23+
provider "kubernetes" {
24+
host = "https://${module.gke.endpoint}"
25+
token = data.google_client_config.default.access_token
26+
cluster_ca_certificate = base64decode(module.gke.ca_certificate)
27+
}
28+
29+
module "gke" {
30+
source = "terraform-google-modules/kubernetes-engine/google"
31+
version = "~> 33.0"
32+
33+
project_id = var.project_id
34+
name = "${local.cluster_type}-cluster${var.cluster_name_suffix}"
35+
regional = true
36+
region = var.region
37+
network = var.network
38+
subnetwork = var.subnetwork
39+
ip_range_pods = var.ip_range_pods
40+
ip_range_services = var.ip_range_services
41+
create_service_account = false
42+
service_account = var.compute_engine_service_account
43+
default_max_pods_per_node = 20
44+
remove_default_node_pool = true
45+
deletion_protection = false
46+
47+
add_cluster_firewall_rules = true
48+
firewall_inbound_ports = ["8443", "9443", "15017"]
49+
50+
# Just an example
51+
network_tags = ["egress-internet"]
52+
53+
cluster_autoscaling = {
54+
enabled = true
55+
autoscaling_profile = "OPTIMIZE_UTILIZATION"
56+
min_cpu_cores = 4
57+
max_cpu_cores = 86
58+
min_memory_gb = 16
59+
max_memory_gb = 256
60+
disk_size = 100
61+
disk_type = "pd-standard"
62+
image_type = "COS_CONTAINERD"
63+
gpu_resources = []
64+
auto_repair = true
65+
auto_upgrade = true
66+
strategy = "SURGE"
67+
max_surge = 1
68+
max_unavailable = 0
69+
}
70+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2018-2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "kubernetes_endpoint" {
18+
sensitive = true
19+
value = module.gke.endpoint
20+
}
21+
22+
output "client_token" {
23+
sensitive = true
24+
value = base64encode(data.google_client_config.default.access_token)
25+
}
26+
27+
output "ca_certificate" {
28+
sensitive = true
29+
value = module.gke.ca_certificate
30+
}
31+
32+
output "service_account" {
33+
description = "The default service account used for running nodes."
34+
value = module.gke.service_account
35+
}
36+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2018-2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// These outputs are used to test the module with kitchen-terraform
18+
// They do not need to be included in real-world uses of this module
19+
20+
output "project_id" {
21+
value = var.project_id
22+
}
23+
24+
output "region" {
25+
value = module.gke.region
26+
}
27+
28+
output "cluster_name" {
29+
description = "Cluster name"
30+
value = module.gke.name
31+
}
32+
33+
output "network" {
34+
value = var.network
35+
}
36+
37+
output "subnetwork" {
38+
value = var.subnetwork
39+
}
40+
41+
output "location" {
42+
value = module.gke.location
43+
}
44+
45+
output "ip_range_pods" {
46+
description = "The secondary IP range used for pods"
47+
value = var.ip_range_pods
48+
}
49+
50+
output "ip_range_services" {
51+
description = "The secondary IP range used for services"
52+
value = var.ip_range_services
53+
}
54+
55+
output "zones" {
56+
description = "List of zones in which the cluster resides"
57+
value = module.gke.zones
58+
}
59+
60+
output "master_kubernetes_version" {
61+
description = "The master Kubernetes version"
62+
value = module.gke.master_version
63+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2018-2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
description = "The project ID to host the cluster in"
19+
}
20+
21+
variable "cluster_name_suffix" {
22+
description = "A suffix to append to the default cluster name"
23+
default = ""
24+
}
25+
26+
variable "region" {
27+
description = "The region to host the cluster in"
28+
}
29+
30+
variable "network" {
31+
description = "The VPC network to host the cluster in"
32+
}
33+
34+
variable "subnetwork" {
35+
description = "The subnetwork to host the cluster in"
36+
}
37+
38+
variable "ip_range_pods" {
39+
description = "The secondary ip range to use for pods"
40+
}
41+
42+
variable "ip_range_services" {
43+
description = "The secondary ip range to use for services"
44+
}
45+
46+
variable "compute_engine_service_account" {
47+
description = "Service account to associate to the nodes in the cluster"
48+
}
49+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2021-2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
terraform {
18+
required_providers {
19+
google = {
20+
source = "hashicorp/google"
21+
}
22+
kubernetes = {
23+
source = "hashicorp/kubernetes"
24+
}
25+
}
26+
required_version = ">= 0.13"
27+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ resource "google_container_cluster" "primary" {
224224
}
225225

226226
dynamic "node_pool_auto_config" {
227-
for_each = var.cluster_autoscaling.enabled && length(var.network_tags) > 0 ? [1] : []
227+
for_each = var.cluster_autoscaling.enabled && (length(var.network_tags) > 0 || var.add_cluster_firewall_rules) ? [1] : []
228228
content {
229229
network_tags {
230-
tags = var.network_tags
230+
tags = var.add_cluster_firewall_rules ? (concat(var.network_tags, [local.cluster_network_tag])) : var.network_tags
231231
}
232232
}
233233
}

modules/beta-private-cluster/cluster.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ resource "google_container_cluster" "primary" {
224224
}
225225

226226
dynamic "node_pool_auto_config" {
227-
for_each = var.cluster_autoscaling.enabled && length(var.network_tags) > 0 ? [1] : []
227+
for_each = var.cluster_autoscaling.enabled && (length(var.network_tags) > 0 || var.add_cluster_firewall_rules) ? [1] : []
228228
content {
229229
network_tags {
230-
tags = var.network_tags
230+
tags = var.add_cluster_firewall_rules ? (concat(var.network_tags, [local.cluster_network_tag])) : var.network_tags
231231
}
232232
}
233233
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ resource "google_container_cluster" "primary" {
224224
}
225225

226226
dynamic "node_pool_auto_config" {
227-
for_each = var.cluster_autoscaling.enabled && length(var.network_tags) > 0 ? [1] : []
227+
for_each = var.cluster_autoscaling.enabled && (length(var.network_tags) > 0 || var.add_cluster_firewall_rules) ? [1] : []
228228
content {
229229
network_tags {
230-
tags = var.network_tags
230+
tags = var.add_cluster_firewall_rules ? (concat(var.network_tags, [local.cluster_network_tag])) : var.network_tags
231231
}
232232
}
233233
}

modules/beta-public-cluster/cluster.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ resource "google_container_cluster" "primary" {
224224
}
225225

226226
dynamic "node_pool_auto_config" {
227-
for_each = var.cluster_autoscaling.enabled && length(var.network_tags) > 0 ? [1] : []
227+
for_each = var.cluster_autoscaling.enabled && (length(var.network_tags) > 0 || var.add_cluster_firewall_rules) ? [1] : []
228228
content {
229229
network_tags {
230-
tags = var.network_tags
230+
tags = var.add_cluster_firewall_rules ? (concat(var.network_tags, [local.cluster_network_tag])) : var.network_tags
231231
}
232232
}
233233
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ resource "google_container_cluster" "primary" {
203203
}
204204

205205
dynamic "node_pool_auto_config" {
206-
for_each = var.cluster_autoscaling.enabled && length(var.network_tags) > 0 ? [1] : []
206+
for_each = var.cluster_autoscaling.enabled && (length(var.network_tags) > 0 || var.add_cluster_firewall_rules) ? [1] : []
207207
content {
208208
network_tags {
209-
tags = var.network_tags
209+
tags = var.add_cluster_firewall_rules ? (concat(var.network_tags, [local.cluster_network_tag])) : var.network_tags
210210
}
211211
}
212212
}

modules/private-cluster/cluster.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ resource "google_container_cluster" "primary" {
203203
}
204204

205205
dynamic "node_pool_auto_config" {
206-
for_each = var.cluster_autoscaling.enabled && length(var.network_tags) > 0 ? [1] : []
206+
for_each = var.cluster_autoscaling.enabled && (length(var.network_tags) > 0 || var.add_cluster_firewall_rules) ? [1] : []
207207
content {
208208
network_tags {
209-
tags = var.network_tags
209+
tags = var.add_cluster_firewall_rules ? (concat(var.network_tags, [local.cluster_network_tag])) : var.network_tags
210210
}
211211
}
212212
}

0 commit comments

Comments
 (0)