Skip to content

Commit 45cd949

Browse files
author
Aaron Lane
authored
Merge pull request #328 from kopachevsky/issues/93/cluster_autoscaling
Add Support for `cluster_autoscaling`
2 parents d856f2b + 1bd6557 commit 45cd949

File tree

23 files changed

+219
-4
lines changed

23 files changed

+219
-4
lines changed

autogen/cluster.tf.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ resource "google_container_cluster" "primary" {
6262
monitoring_service = var.monitoring_service
6363

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

autogen/main.tf.tmpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ 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+
54+
autoscalling_resource_limits = var.cluster_autoscaling.enabled ? [{
55+
resource_type = "cpu"
56+
minimum = var.cluster_autoscaling.min_cpu_cores
57+
maximum = var.cluster_autoscaling.max_cpu_cores
58+
}, {
59+
resource_type = "memory"
60+
minimum = var.cluster_autoscaling.min_memory_gb
61+
maximum = var.cluster_autoscaling.max_memory_gb
62+
}] : []
63+
5364
{% endif %}
5465

5566

autogen/variables.tf.tmpl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,26 @@ variable "node_pools_metadata" {
174174
default-node-pool = {}
175175
}
176176
}
177-
178177
{% if beta_cluster %}
178+
179+
variable "cluster_autoscaling" {
180+
type = object({
181+
enabled = bool
182+
min_cpu_cores = number
183+
max_cpu_cores = number
184+
min_memory_gb = number
185+
max_memory_gb = number
186+
})
187+
default = {
188+
enabled = false
189+
max_cpu_cores = 0
190+
min_cpu_cores = 0
191+
max_memory_gb = 0
192+
min_memory_gb = 0
193+
}
194+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
195+
}
196+
179197
variable "node_pools_taints" {
180198
type = map(list(object({ key = string, value = string, effect = string })))
181199
description = "Map of lists containing node taints by node-pool name"

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,20 @@ 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+
min_cpu_cores = number
59+
max_cpu_cores = number
60+
min_memory_gb = number
61+
max_memory_gb = number
62+
})
63+
default = {
64+
enabled = false
65+
max_cpu_cores = 0
66+
min_cpu_cores = 0
67+
max_memory_gb = 0
68+
min_memory_gb = 0
69+
}
70+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
71+
}

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ resource "google_container_cluster" "primary" {
5555
logging_service = var.logging_service
5656
monitoring_service = var.monitoring_service
5757

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

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ locals {
4646
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
4747
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
4848

49+
autoscalling_resource_limits = var.cluster_autoscaling.enabled ? [{
50+
resource_type = "cpu"
51+
minimum = var.cluster_autoscaling.min_cpu_cores
52+
maximum = var.cluster_autoscaling.max_cpu_cores
53+
}, {
54+
resource_type = "memory"
55+
minimum = var.cluster_autoscaling.min_memory_gb
56+
maximum = var.cluster_autoscaling.max_memory_gb
57+
}] : []
58+
59+
4960

5061
custom_kube_dns_config = length(keys(var.stub_domains)) > 0
5162
upstream_nameservers_config = length(var.upstream_nameservers) > 0

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ variable "node_pools_metadata" {
175175
}
176176
}
177177

178+
variable "cluster_autoscaling" {
179+
type = object({
180+
enabled = bool
181+
min_cpu_cores = number
182+
max_cpu_cores = number
183+
min_memory_gb = number
184+
max_memory_gb = number
185+
})
186+
default = {
187+
enabled = false
188+
max_cpu_cores = 0
189+
min_cpu_cores = 0
190+
max_memory_gb = 0
191+
min_memory_gb = 0
192+
}
193+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
194+
}
195+
178196
variable "node_pools_taints" {
179197
type = map(list(object({ key = string, value = string, effect = string })))
180198
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ resource "google_container_cluster" "primary" {
5555
logging_service = var.logging_service
5656
monitoring_service = var.monitoring_service
5757

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

modules/beta-private-cluster/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ locals {
4646
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
4747
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
4848

49+
autoscalling_resource_limits = var.cluster_autoscaling.enabled ? [{
50+
resource_type = "cpu"
51+
minimum = var.cluster_autoscaling.min_cpu_cores
52+
maximum = var.cluster_autoscaling.max_cpu_cores
53+
}, {
54+
resource_type = "memory"
55+
minimum = var.cluster_autoscaling.min_memory_gb
56+
maximum = var.cluster_autoscaling.max_memory_gb
57+
}] : []
58+
59+
4960

5061
custom_kube_dns_config = length(keys(var.stub_domains)) > 0
5162
upstream_nameservers_config = length(var.upstream_nameservers) > 0

modules/beta-private-cluster/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ variable "node_pools_metadata" {
175175
}
176176
}
177177

178+
variable "cluster_autoscaling" {
179+
type = object({
180+
enabled = bool
181+
min_cpu_cores = number
182+
max_cpu_cores = number
183+
min_memory_gb = number
184+
max_memory_gb = number
185+
})
186+
default = {
187+
enabled = false
188+
max_cpu_cores = 0
189+
min_cpu_cores = 0
190+
max_memory_gb = 0
191+
min_memory_gb = 0
192+
}
193+
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
194+
}
195+
178196
variable "node_pools_taints" {
179197
type = map(list(object({ key = string, value = string, effect = string })))
180198
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ resource "google_container_cluster" "primary" {
5555
logging_service = var.logging_service
5656
monitoring_service = var.monitoring_service
5757

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

modules/beta-public-cluster/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ locals {
4646
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
4747
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
4848

49+
autoscalling_resource_limits = var.cluster_autoscaling.enabled ? [{
50+
resource_type = "cpu"
51+
minimum = var.cluster_autoscaling.min_cpu_cores
52+
maximum = var.cluster_autoscaling.max_cpu_cores
53+
}, {
54+
resource_type = "memory"
55+
minimum = var.cluster_autoscaling.min_memory_gb
56+
maximum = var.cluster_autoscaling.max_memory_gb
57+
}] : []
58+
59+
4960

5061
custom_kube_dns_config = length(keys(var.stub_domains)) > 0
5162
upstream_nameservers_config = length(var.upstream_nameservers) > 0

modules/beta-public-cluster/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ variable "node_pools_metadata" {
175175
}
176176
}
177177

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ variable "node_pools_metadata" {
174174
default-node-pool = {}
175175
}
176176
}
177-
178177
variable "node_pools_tags" {
179178
type = map(list(string))
180179
description = "Map of lists containing node network tags by node-pool name"

modules/private-cluster/variables.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ variable "node_pools_metadata" {
174174
default-node-pool = {}
175175
}
176176
}
177-
178177
variable "node_pools_tags" {
179178
type = map(list(string))
180179
description = "Map of lists containing node network tags by node-pool name"

test/fixtures/node_pool/example.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,13 @@ 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_accounts[0]
29+
30+
cluster_autoscaling = {
31+
enabled = true
32+
max_cpu_cores = 20
33+
min_cpu_cores = 5
34+
max_memory_gb = 30
35+
min_memory_gb = 10
36+
}
2937
}
3038

test/integration/node_pool/controls/gcloud.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@
3333
end
3434
end
3535

36+
describe "cluster-autoscaling" do
37+
it "has the expected cluster autoscaling settings" do
38+
expect(data['autoscaling']).to eq({
39+
"autoprovisioningNodePoolDefaults" => {
40+
"oauthScopes" => %w(https://www.googleapis.com/auth/logging.write https://www.googleapis.com/auth/monitoring),
41+
"serviceAccount" => "default"
42+
},
43+
"enableNodeAutoprovisioning" => true,
44+
"resourceLimits" => [
45+
{
46+
"maximum" => "20",
47+
"minimum" => "5",
48+
"resourceType" => "cpu"
49+
},
50+
{
51+
"maximum" => "30",
52+
"minimum" => "10",
53+
"resourceType" => "memory"
54+
}
55+
]
56+
})
57+
end
58+
end
59+
3660
describe "node pools" do
3761
let(:node_pools) { data['nodePools'].reject { |p| p['name'] == "default-pool" } }
3862

variables.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ variable "node_pools_metadata" {
174174
default-node-pool = {}
175175
}
176176
}
177-
178177
variable "node_pools_tags" {
179178
type = map(list(string))
180179
description = "Map of lists containing node network tags by node-pool name"

0 commit comments

Comments
 (0)