Skip to content

Commit 275e427

Browse files
committed
* Fix #93
Added cluster auto scaling Updated docs Added tests for cluster auto scaling in node_pool fixture
1 parent 3945205 commit 275e427

File tree

25 files changed

+214
-6
lines changed

25 files changed

+214
-6
lines changed

autogen/cluster.tf

Lines changed: 24 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,29 @@ 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 = concat(
70+
var.cluster_autoscaling.enabled && lookup(var.cluster_autoscaling.resource_limits, "min_cpu_cores", 0) > 0 && lookup(var.cluster_autoscaling.resource_limits, "max_cpu_cores", 0) > 0 ? [{
71+
resource_type = "cpu"
72+
minimum = var.cluster_autoscaling.resource_limits["min_cpu_cores"]
73+
maximum = var.cluster_autoscaling.resource_limits["max_cpu_cores"]
74+
}] : [],
75+
var.cluster_autoscaling.enabled && lookup(var.cluster_autoscaling.resource_limits, "min_memory_gb", 0) > 0 && lookup(var.cluster_autoscaling.resource_limits, "max_memory_gb", 0) > 0 ? [{
76+
resource_type = "memory"
77+
minimum = var.cluster_autoscaling.resource_limits["min_memory_gb"]
78+
maximum = var.cluster_autoscaling.resource_limits["max_memory_gb"]
79+
}] : []
80+
)
81+
content {
82+
resource_type = lookup(resource_limits.value, "resource_type")
83+
minimum = lookup(resource_limits.value, "minimum")
84+
maximum = lookup(resource_limits.value, "maximum")
85+
}
86+
}
87+
}
88+
6589
enable_binary_authorization = var.enable_binary_authorization
6690
enable_intranode_visibility = var.enable_intranode_visibility
6791
default_max_pods_per_node = var.default_max_pods_per_node

autogen/variables.tf

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ provider "google-beta" {
2626
module "gke" {
2727
source = "../../modules/beta-public-cluster/"
2828
project_id = var.project_id
29-
name = "${local.cluster_type}-cluster${var.cluster_name_suffix}"
29+
name = join("", [local.cluster_type, "-cluster", var.cluster_name_suffix])
3030
region = var.region
3131
zones = var.zones
3232
network = var.network
@@ -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+
}

examples/simple_regional_beta/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This example illustrates how to create a simple cluster with beta features.
88
| Name | Description | Type | Default | Required |
99
|------|-------------|:----:|:-----:|:-----:|
1010
| cloudrun | Boolean to enable / disable CloudRun | string | `"true"` | no |
11+
| 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 |
1112
| cluster\_name\_suffix | A suffix to append to the default cluster name | string | `""` | no |
1213
| compute\_engine\_service\_account | Service account to associate to the nodes in the cluster | string | n/a | yes |
1314
| ip\_range\_pods | The secondary ip range to use for pods | string | n/a | yes |

examples/simple_regional_beta/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ provider "google-beta" {
2626
module "gke" {
2727
source = "../../modules/beta-public-cluster/"
2828
project_id = var.project_id
29-
name = "${local.cluster_type}-cluster${var.cluster_name_suffix}"
29+
name = join("", [local.cluster_type, "-cluster", var.cluster_name_suffix])
3030
regional = true
3131
region = var.region
3232
network = var.network
@@ -41,6 +41,7 @@ module "gke" {
4141
sandbox_enabled = var.sandbox_enabled
4242
remove_default_node_pool = var.remove_default_node_pool
4343
node_pools = var.node_pools
44+
cluster_autoscaling = var.cluster_autoscaling
4445
}
4546

4647
data "google_client_config" "default" {

examples/simple_regional_beta/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,15 @@ variable "node_pools" {
8585
},
8686
]
8787
}
88+
89+
variable "cluster_autoscaling" {
90+
type = object({
91+
enabled = bool
92+
resource_limits = map(number)
93+
})
94+
default = {
95+
enabled = false
96+
resource_limits = {}
97+
}
98+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
99+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
141141
| basic\_auth\_password | The password to be used with Basic Authentication. | string | `""` | no |
142142
| 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 |
143143
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
144+
| 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 |
144145
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
145146
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
146147
| 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: 24 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,29 @@ 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 = concat(
63+
var.cluster_autoscaling.enabled && lookup(var.cluster_autoscaling.resource_limits, "min_cpu_cores", 0) > 0 && lookup(var.cluster_autoscaling.resource_limits, "max_cpu_cores", 0) > 0 ? [{
64+
resource_type = "cpu"
65+
minimum = var.cluster_autoscaling.resource_limits["min_cpu_cores"]
66+
maximum = var.cluster_autoscaling.resource_limits["max_cpu_cores"]
67+
}] : [],
68+
var.cluster_autoscaling.enabled && lookup(var.cluster_autoscaling.resource_limits, "min_memory_gb", 0) > 0 && lookup(var.cluster_autoscaling.resource_limits, "max_memory_gb", 0) > 0 ? [{
69+
resource_type = "memory"
70+
minimum = var.cluster_autoscaling.resource_limits["min_memory_gb"]
71+
maximum = var.cluster_autoscaling.resource_limits["max_memory_gb"]
72+
}] : []
73+
)
74+
content {
75+
resource_type = lookup(resource_limits.value, "resource_type")
76+
minimum = lookup(resource_limits.value, "minimum")
77+
maximum = lookup(resource_limits.value, "maximum")
78+
}
79+
}
80+
}
81+
5882
enable_binary_authorization = var.enable_binary_authorization
5983
enable_intranode_visibility = var.enable_intranode_visibility
6084
default_max_pods_per_node = var.default_max_pods_per_node

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
@@ -141,6 +141,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
141141
| basic\_auth\_password | The password to be used with Basic Authentication. | string | `""` | no |
142142
| 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 |
143143
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
144+
| 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 |
144145
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
145146
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
146147
| 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: 24 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,29 @@ 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 = concat(
63+
var.cluster_autoscaling.enabled && lookup(var.cluster_autoscaling.resource_limits, "min_cpu_cores", 0) > 0 && lookup(var.cluster_autoscaling.resource_limits, "max_cpu_cores", 0) > 0 ? [{
64+
resource_type = "cpu"
65+
minimum = var.cluster_autoscaling.resource_limits["min_cpu_cores"]
66+
maximum = var.cluster_autoscaling.resource_limits["max_cpu_cores"]
67+
}] : [],
68+
var.cluster_autoscaling.enabled && lookup(var.cluster_autoscaling.resource_limits, "min_memory_gb", 0) > 0 && lookup(var.cluster_autoscaling.resource_limits, "max_memory_gb", 0) > 0 ? [{
69+
resource_type = "memory"
70+
minimum = var.cluster_autoscaling.resource_limits["min_memory_gb"]
71+
maximum = var.cluster_autoscaling.resource_limits["max_memory_gb"]
72+
}] : []
73+
)
74+
content {
75+
resource_type = lookup(resource_limits.value, "resource_type")
76+
minimum = lookup(resource_limits.value, "minimum")
77+
maximum = lookup(resource_limits.value, "maximum")
78+
}
79+
}
80+
}
81+
5882
enable_binary_authorization = var.enable_binary_authorization
5983
enable_intranode_visibility = var.enable_intranode_visibility
6084
default_max_pods_per_node = var.default_max_pods_per_node

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
@@ -136,6 +136,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
136136
| basic\_auth\_password | The password to be used with Basic Authentication. | string | `""` | no |
137137
| 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 |
138138
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
139+
| 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 |
139140
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
140141
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
141142
| 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: 24 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,29 @@ 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 = concat(
63+
var.cluster_autoscaling.enabled && lookup(var.cluster_autoscaling.resource_limits, "min_cpu_cores", 0) > 0 && lookup(var.cluster_autoscaling.resource_limits, "max_cpu_cores", 0) > 0 ? [{
64+
resource_type = "cpu"
65+
minimum = var.cluster_autoscaling.resource_limits["min_cpu_cores"]
66+
maximum = var.cluster_autoscaling.resource_limits["max_cpu_cores"]
67+
}] : [],
68+
var.cluster_autoscaling.enabled && lookup(var.cluster_autoscaling.resource_limits, "min_memory_gb", 0) > 0 && lookup(var.cluster_autoscaling.resource_limits, "max_memory_gb", 0) > 0 ? [{
69+
resource_type = "memory"
70+
minimum = var.cluster_autoscaling.resource_limits["min_memory_gb"]
71+
maximum = var.cluster_autoscaling.resource_limits["max_memory_gb"]
72+
}] : []
73+
)
74+
content {
75+
resource_type = lookup(resource_limits.value, "resource_type")
76+
minimum = lookup(resource_limits.value, "minimum")
77+
maximum = lookup(resource_limits.value, "maximum")
78+
}
79+
}
80+
}
81+
5882
enable_binary_authorization = var.enable_binary_authorization
5983
enable_intranode_visibility = var.enable_intranode_visibility
6084
default_max_pods_per_node = var.default_max_pods_per_node

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)