Skip to content

Commit 1e21211

Browse files
committed
Added cluster autoscaling
Updated docs Added tests for cluster autoscaling in node_pool fixture * Fix #93
1 parent 3c7f472 commit 1e21211

File tree

26 files changed

+210
-4
lines changed

26 files changed

+210
-4
lines changed

autogen/cluster.tf.tmpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ resource "google_container_cluster" "primary" {
4545
}
4646
}
4747

48+
4849
{% if beta_cluster %}
4950
dynamic "release_channel" {
5051
for_each = local.release_channel
@@ -62,6 +63,18 @@ resource "google_container_cluster" "primary" {
6263
monitoring_service = var.monitoring_service
6364

6465
{% if beta_cluster %}
66+
cluster_autoscaling {
67+
enabled = var.cluster_autoscaling.enabled
68+
dynamic "resource_limits" {
69+
for_each = local.autoscalling_resource_limits
70+
content {
71+
resource_type = lookup(resource_limits.value, "resource_type")
72+
minimum = lookup(resource_limits.value, "minimum")
73+
maximum = lookup(resource_limits.value, "maximum")
74+
}
75+
}
76+
}
77+
6578
enable_binary_authorization = var.enable_binary_authorization
6679
enable_intranode_visibility = var.enable_intranode_visibility
6780
default_max_pods_per_node = var.default_max_pods_per_node

autogen/main.tf.tmpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ locals {
5050
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
5151
{% if beta_cluster %}
5252
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
53+
limits = var.cluster_autoscaling.resource_limits
54+
55+
autoscalling_resource_limits = concat(
56+
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
57+
resource_type = "cpu"
58+
minimum = local.limits["min_cpu_cores"]
59+
maximum = local.limits["max_cpu_cores"]
60+
}] : [],
61+
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
62+
resource_type = "memory"
63+
minimum = local.limits["min_memory_gb"]
64+
maximum = local.limits["max_memory_gb"]
65+
}] : []
66+
)
5367
{% endif %}
5468

5569

autogen/variables.tf.tmpl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,20 @@ variable "node_pools_metadata" {
178178
default-node-pool = {}
179179
}
180180
}
181-
182181
{% if beta_cluster %}
182+
183+
variable "cluster_autoscaling" {
184+
type = object({
185+
enabled = bool
186+
resource_limits = map(number)
187+
})
188+
default = {
189+
enabled = false
190+
resource_limits = {}
191+
}
192+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
193+
}
194+
183195
variable "node_pools_taints" {
184196
type = map(list(object({key=string,value=string,effect=string})))
185197
description = "Map of lists containing node taints by node-pool name"

cluster.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ resource "google_container_cluster" "primary" {
4242
}
4343

4444

45+
4546
subnetwork = data.google_compute_subnetwork.gke_subnetwork.self_link
4647
min_master_version = local.master_version
4748

examples/node_pool/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This example illustrates how to create a cluster with multiple custom node-pool
77

88
| Name | Description | Type | Default | Required |
99
|------|-------------|:----:|:-----:|:-----:|
10+
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | object | `<map>` | no |
1011
| cluster\_name\_suffix | A suffix to append to the default cluster name | string | `""` | no |
1112
| compute\_engine\_service\_account | Service account to associate to the nodes in the cluster | string | n/a | yes |
1213
| ip\_range\_pods | The secondary ip range to use for pods | string | n/a | yes |

examples/node_pool/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module "gke" {
3636
create_service_account = false
3737
remove_default_node_pool = true
3838
disable_legacy_metadata_endpoints = false
39+
cluster_autoscaling = var.cluster_autoscaling
3940

4041
node_pools = [
4142
{

examples/node_pool/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,14 @@ variable "compute_engine_service_account" {
5252
description = "Service account to associate to the nodes in the cluster"
5353
}
5454

55+
variable "cluster_autoscaling" {
56+
type = object({
57+
enabled = bool
58+
resource_limits = map(number)
59+
})
60+
default = {
61+
enabled = false
62+
resource_limits = {}
63+
}
64+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
65+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
142142
| basic\_auth\_password | The password to be used with Basic Authentication. | string | `""` | no |
143143
| basic\_auth\_username | The username to be used with Basic Authentication. An empty value will disable Basic Authentication, which is the recommended configuration. | string | `""` | no |
144144
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
145+
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | object | `<map>` | no |
145146
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
146147
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
147148
| configure\_ip\_masq | Enables the installation of ip masquerading, which is usually no longer required when using aliasied IP addresses. IP masquerading uses a kubectl call, so when you have a private cluster, you will need access to the API server. | string | `"false"` | no |

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ resource "google_container_cluster" "primary" {
4141
}
4242
}
4343

44+
4445
dynamic "release_channel" {
4546
for_each = local.release_channel
4647

@@ -55,6 +56,18 @@ resource "google_container_cluster" "primary" {
5556
logging_service = var.logging_service
5657
monitoring_service = var.monitoring_service
5758

59+
cluster_autoscaling {
60+
enabled = var.cluster_autoscaling.enabled
61+
dynamic "resource_limits" {
62+
for_each = local.autoscalling_resource_limits
63+
content {
64+
resource_type = lookup(resource_limits.value, "resource_type")
65+
minimum = lookup(resource_limits.value, "minimum")
66+
maximum = lookup(resource_limits.value, "maximum")
67+
}
68+
}
69+
}
70+
5871
enable_binary_authorization = var.enable_binary_authorization
5972
enable_intranode_visibility = var.enable_intranode_visibility
6073
default_max_pods_per_node = var.default_max_pods_per_node

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ 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
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
48+
limits = var.cluster_autoscaling.resource_limits
49+
50+
autoscalling_resource_limits = concat(
51+
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
52+
resource_type = "cpu"
53+
minimum = local.limits["min_cpu_cores"]
54+
maximum = local.limits["max_cpu_cores"]
55+
}] : [],
56+
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
57+
resource_type = "memory"
58+
minimum = local.limits["min_memory_gb"]
59+
maximum = local.limits["max_memory_gb"]
60+
}] : []
61+
)
4862

4963

5064
custom_kube_dns_config = length(keys(var.stub_domains)) > 0

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ variable "node_pools_metadata" {
179179
}
180180
}
181181

182+
variable "cluster_autoscaling" {
183+
type = object({
184+
enabled = bool
185+
resource_limits = map(number)
186+
})
187+
default = {
188+
enabled = false
189+
resource_limits = {}
190+
}
191+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
192+
}
193+
182194
variable "node_pools_taints" {
183195
type = map(list(object({ key = string, value = string, effect = string })))
184196
description = "Map of lists containing node taints by node-pool name"

modules/beta-private-cluster/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
142142
| basic\_auth\_password | The password to be used with Basic Authentication. | string | `""` | no |
143143
| basic\_auth\_username | The username to be used with Basic Authentication. An empty value will disable Basic Authentication, which is the recommended configuration. | string | `""` | no |
144144
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
145+
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | object | `<map>` | no |
145146
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
146147
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
147148
| configure\_ip\_masq | Enables the installation of ip masquerading, which is usually no longer required when using aliasied IP addresses. IP masquerading uses a kubectl call, so when you have a private cluster, you will need access to the API server. | string | `"false"` | no |

modules/beta-private-cluster/cluster.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ resource "google_container_cluster" "primary" {
4141
}
4242
}
4343

44+
4445
dynamic "release_channel" {
4546
for_each = local.release_channel
4647

@@ -55,6 +56,18 @@ resource "google_container_cluster" "primary" {
5556
logging_service = var.logging_service
5657
monitoring_service = var.monitoring_service
5758

59+
cluster_autoscaling {
60+
enabled = var.cluster_autoscaling.enabled
61+
dynamic "resource_limits" {
62+
for_each = local.autoscalling_resource_limits
63+
content {
64+
resource_type = lookup(resource_limits.value, "resource_type")
65+
minimum = lookup(resource_limits.value, "minimum")
66+
maximum = lookup(resource_limits.value, "maximum")
67+
}
68+
}
69+
}
70+
5871
enable_binary_authorization = var.enable_binary_authorization
5972
enable_intranode_visibility = var.enable_intranode_visibility
6073
default_max_pods_per_node = var.default_max_pods_per_node

modules/beta-private-cluster/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ 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
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
48+
limits = var.cluster_autoscaling.resource_limits
49+
50+
autoscalling_resource_limits = concat(
51+
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
52+
resource_type = "cpu"
53+
minimum = local.limits["min_cpu_cores"]
54+
maximum = local.limits["max_cpu_cores"]
55+
}] : [],
56+
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
57+
resource_type = "memory"
58+
minimum = local.limits["min_memory_gb"]
59+
maximum = local.limits["max_memory_gb"]
60+
}] : []
61+
)
4862

4963

5064
custom_kube_dns_config = length(keys(var.stub_domains)) > 0

modules/beta-private-cluster/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ variable "node_pools_metadata" {
179179
}
180180
}
181181

182+
variable "cluster_autoscaling" {
183+
type = object({
184+
enabled = bool
185+
resource_limits = map(number)
186+
})
187+
default = {
188+
enabled = false
189+
resource_limits = {}
190+
}
191+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
192+
}
193+
182194
variable "node_pools_taints" {
183195
type = map(list(object({ key = string, value = string, effect = string })))
184196
description = "Map of lists containing node taints by node-pool name"

modules/beta-public-cluster/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
137137
| basic\_auth\_password | The password to be used with Basic Authentication. | string | `""` | no |
138138
| basic\_auth\_username | The username to be used with Basic Authentication. An empty value will disable Basic Authentication, which is the recommended configuration. | string | `""` | no |
139139
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
140+
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | object | `<map>` | no |
140141
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
141142
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
142143
| configure\_ip\_masq | Enables the installation of ip masquerading, which is usually no longer required when using aliasied IP addresses. IP masquerading uses a kubectl call, so when you have a private cluster, you will need access to the API server. | string | `"false"` | no |

modules/beta-public-cluster/cluster.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ resource "google_container_cluster" "primary" {
4141
}
4242
}
4343

44+
4445
dynamic "release_channel" {
4546
for_each = local.release_channel
4647

@@ -55,6 +56,18 @@ resource "google_container_cluster" "primary" {
5556
logging_service = var.logging_service
5657
monitoring_service = var.monitoring_service
5758

59+
cluster_autoscaling {
60+
enabled = var.cluster_autoscaling.enabled
61+
dynamic "resource_limits" {
62+
for_each = local.autoscalling_resource_limits
63+
content {
64+
resource_type = lookup(resource_limits.value, "resource_type")
65+
minimum = lookup(resource_limits.value, "minimum")
66+
maximum = lookup(resource_limits.value, "maximum")
67+
}
68+
}
69+
}
70+
5871
enable_binary_authorization = var.enable_binary_authorization
5972
enable_intranode_visibility = var.enable_intranode_visibility
6073
default_max_pods_per_node = var.default_max_pods_per_node

modules/beta-public-cluster/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ 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
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
48+
limits = var.cluster_autoscaling.resource_limits
49+
50+
autoscalling_resource_limits = concat(
51+
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
52+
resource_type = "cpu"
53+
minimum = local.limits["min_cpu_cores"]
54+
maximum = local.limits["max_cpu_cores"]
55+
}] : [],
56+
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
57+
resource_type = "memory"
58+
minimum = local.limits["min_memory_gb"]
59+
maximum = local.limits["max_memory_gb"]
60+
}] : []
61+
)
4862

4963

5064
custom_kube_dns_config = length(keys(var.stub_domains)) > 0

modules/beta-public-cluster/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ variable "node_pools_metadata" {
179179
}
180180
}
181181

182+
variable "cluster_autoscaling" {
183+
type = object({
184+
enabled = bool
185+
resource_limits = map(number)
186+
})
187+
default = {
188+
enabled = false
189+
resource_limits = {}
190+
}
191+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
192+
}
193+
182194
variable "node_pools_taints" {
183195
type = map(list(object({ key = string, value = string, effect = string })))
184196
description = "Map of lists containing node taints by node-pool name"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ resource "google_container_cluster" "primary" {
4242
}
4343

4444

45+
4546
subnetwork = data.google_compute_subnetwork.gke_subnetwork.self_link
4647
min_master_version = local.master_version
4748

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ variable "node_pools_metadata" {
178178
default-node-pool = {}
179179
}
180180
}
181-
182181
variable "node_pools_tags" {
183182
type = map(list(string))
184183
description = "Map of lists containing node network tags by node-pool name"

modules/private-cluster/cluster.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ resource "google_container_cluster" "primary" {
4242
}
4343

4444

45+
4546
subnetwork = data.google_compute_subnetwork.gke_subnetwork.self_link
4647
min_master_version = local.master_version
4748

modules/private-cluster/variables.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ variable "node_pools_metadata" {
178178
default-node-pool = {}
179179
}
180180
}
181-
182181
variable "node_pools_tags" {
183182
type = map(list(string))
184183
description = "Map of lists containing node network tags by node-pool name"

test/fixtures/node_pool/example.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,15 @@ module "example" {
2626
ip_range_pods = google_compute_subnetwork.main.secondary_ip_range[0].range_name
2727
ip_range_services = google_compute_subnetwork.main.secondary_ip_range[1].range_name
2828
compute_engine_service_account = var.compute_engine_service_account
29+
30+
cluster_autoscaling = {
31+
enabled = true
32+
resource_limits = {
33+
max_cpu_cores = 20
34+
min_cpu_cores = 5
35+
max_memory_gb = 30
36+
min_memory_gb = 10
37+
}
38+
}
2939
}
3040

0 commit comments

Comments
 (0)