Skip to content

Commit 132c5ec

Browse files
committed
Added cluster autoscaling - optimized input variable
* Fix #93
1 parent 1e21211 commit 132c5ec

File tree

21 files changed

+42
-74
lines changed

21 files changed

+42
-74
lines changed

autogen/cluster.tf.tmpl

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

48-
4948
{% if beta_cluster %}
5049
dynamic "release_channel" {
5150
for_each = local.release_channel
@@ -64,7 +63,7 @@ resource "google_container_cluster" "primary" {
6463

6564
{% if beta_cluster %}
6665
cluster_autoscaling {
67-
enabled = var.cluster_autoscaling.enabled
66+
enabled = local.autoscalling_enabled
6867
dynamic "resource_limits" {
6968
for_each = local.autoscalling_resource_limits
7069
content {

autogen/main.tf.tmpl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,22 @@ 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
53+
limits = var.cluster_autoscaling
5454

5555
autoscalling_resource_limits = concat(
56-
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
56+
lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
5757
resource_type = "cpu"
5858
minimum = local.limits["min_cpu_cores"]
5959
maximum = local.limits["max_cpu_cores"]
6060
}] : [],
61-
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
61+
lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
6262
resource_type = "memory"
6363
minimum = local.limits["min_memory_gb"]
6464
maximum = local.limits["max_memory_gb"]
6565
}] : []
6666
)
67+
68+
autoscalling_enabled = length(local.autoscalling_resource_limits) > 0
6769
{% endif %}
6870

6971

autogen/variables.tf.tmpl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,8 @@ variable "node_pools_metadata" {
181181
{% if beta_cluster %}
182182

183183
variable "cluster_autoscaling" {
184-
type = object({
185-
enabled = bool
186-
resource_limits = map(number)
187-
})
188-
default = {
189-
enabled = false
190-
resource_limits = {}
191-
}
184+
type = map(number)
185+
default = {}
192186
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
193187
}
194188

cluster.tf

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

4444

45-
4645
subnetwork = data.google_compute_subnetwork.gke_subnetwork.self_link
4746
min_master_version = local.master_version
4847

examples/node_pool/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +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 |
10+
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | map(number) | `<map>` | no |
1111
| cluster\_name\_suffix | A suffix to append to the default cluster name | string | `""` | no |
1212
| compute\_engine\_service\_account | Service account to associate to the nodes in the cluster | string | n/a | yes |
1313
| ip\_range\_pods | The secondary ip range to use for pods | string | n/a | yes |

examples/node_pool/variables.tf

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,7 @@ variable "compute_engine_service_account" {
5353
}
5454

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +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 |
145+
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | map(number) | `<map>` | no |
146146
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
147147
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
148148
| 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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ resource "google_container_cluster" "primary" {
4141
}
4242
}
4343

44-
4544
dynamic "release_channel" {
4645
for_each = local.release_channel
4746

@@ -57,7 +56,7 @@ resource "google_container_cluster" "primary" {
5756
monitoring_service = var.monitoring_service
5857

5958
cluster_autoscaling {
60-
enabled = var.cluster_autoscaling.enabled
59+
enabled = local.autoscalling_enabled
6160
dynamic "resource_limits" {
6261
for_each = local.autoscalling_resource_limits
6362
content {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,23 @@ 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
48+
limits = var.cluster_autoscaling
4949

5050
autoscalling_resource_limits = concat(
51-
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
51+
lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
5252
resource_type = "cpu"
5353
minimum = local.limits["min_cpu_cores"]
5454
maximum = local.limits["max_cpu_cores"]
5555
}] : [],
56-
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
56+
lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
5757
resource_type = "memory"
5858
minimum = local.limits["min_memory_gb"]
5959
maximum = local.limits["max_memory_gb"]
6060
}] : []
6161
)
6262

63+
autoscalling_enabled = length(local.autoscalling_resource_limits) > 0
64+
6365

6466
custom_kube_dns_config = length(keys(var.stub_domains)) > 0
6567
upstream_nameservers_config = length(var.upstream_nameservers) > 0

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,8 @@ variable "node_pools_metadata" {
180180
}
181181

182182
variable "cluster_autoscaling" {
183-
type = object({
184-
enabled = bool
185-
resource_limits = map(number)
186-
})
187-
default = {
188-
enabled = false
189-
resource_limits = {}
190-
}
183+
type = map(number)
184+
default = {}
191185
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
192186
}
193187

modules/beta-private-cluster/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +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 |
145+
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | map(number) | `<map>` | no |
146146
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
147147
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
148148
| 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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ resource "google_container_cluster" "primary" {
4141
}
4242
}
4343

44-
4544
dynamic "release_channel" {
4645
for_each = local.release_channel
4746

@@ -57,7 +56,7 @@ resource "google_container_cluster" "primary" {
5756
monitoring_service = var.monitoring_service
5857

5958
cluster_autoscaling {
60-
enabled = var.cluster_autoscaling.enabled
59+
enabled = local.autoscalling_enabled
6160
dynamic "resource_limits" {
6261
for_each = local.autoscalling_resource_limits
6362
content {

modules/beta-private-cluster/main.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,23 @@ 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
48+
limits = var.cluster_autoscaling
4949

5050
autoscalling_resource_limits = concat(
51-
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
51+
lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
5252
resource_type = "cpu"
5353
minimum = local.limits["min_cpu_cores"]
5454
maximum = local.limits["max_cpu_cores"]
5555
}] : [],
56-
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
56+
lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
5757
resource_type = "memory"
5858
minimum = local.limits["min_memory_gb"]
5959
maximum = local.limits["max_memory_gb"]
6060
}] : []
6161
)
6262

63+
autoscalling_enabled = length(local.autoscalling_resource_limits) > 0
64+
6365

6466
custom_kube_dns_config = length(keys(var.stub_domains)) > 0
6567
upstream_nameservers_config = length(var.upstream_nameservers) > 0

modules/beta-private-cluster/variables.tf

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,8 @@ variable "node_pools_metadata" {
180180
}
181181

182182
variable "cluster_autoscaling" {
183-
type = object({
184-
enabled = bool
185-
resource_limits = map(number)
186-
})
187-
default = {
188-
enabled = false
189-
resource_limits = {}
190-
}
183+
type = map(number)
184+
default = {}
191185
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
192186
}
193187

modules/beta-public-cluster/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +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 |
140+
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | map(number) | `<map>` | no |
141141
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
142142
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
143143
| 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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ resource "google_container_cluster" "primary" {
4141
}
4242
}
4343

44-
4544
dynamic "release_channel" {
4645
for_each = local.release_channel
4746

@@ -57,7 +56,7 @@ resource "google_container_cluster" "primary" {
5756
monitoring_service = var.monitoring_service
5857

5958
cluster_autoscaling {
60-
enabled = var.cluster_autoscaling.enabled
59+
enabled = local.autoscalling_enabled
6160
dynamic "resource_limits" {
6261
for_each = local.autoscalling_resource_limits
6362
content {

modules/beta-public-cluster/main.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,23 @@ 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
48+
limits = var.cluster_autoscaling
4949

5050
autoscalling_resource_limits = concat(
51-
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
51+
lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
5252
resource_type = "cpu"
5353
minimum = local.limits["min_cpu_cores"]
5454
maximum = local.limits["max_cpu_cores"]
5555
}] : [],
56-
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
56+
lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
5757
resource_type = "memory"
5858
minimum = local.limits["min_memory_gb"]
5959
maximum = local.limits["max_memory_gb"]
6060
}] : []
6161
)
6262

63+
autoscalling_enabled = length(local.autoscalling_resource_limits) > 0
64+
6365

6466
custom_kube_dns_config = length(keys(var.stub_domains)) > 0
6567
upstream_nameservers_config = length(var.upstream_nameservers) > 0

modules/beta-public-cluster/variables.tf

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,8 @@ variable "node_pools_metadata" {
180180
}
181181

182182
variable "cluster_autoscaling" {
183-
type = object({
184-
enabled = bool
185-
resource_limits = map(number)
186-
})
187-
default = {
188-
enabled = false
189-
resource_limits = {}
190-
}
183+
type = map(number)
184+
default = {}
191185
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
192186
}
193187

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

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

4444

45-
4645
subnetwork = data.google_compute_subnetwork.gke_subnetwork.self_link
4746
min_master_version = local.master_version
4847

modules/private-cluster/cluster.tf

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

4444

45-
4645
subnetwork = data.google_compute_subnetwork.gke_subnetwork.self_link
4746
min_master_version = local.master_version
4847

test/fixtures/node_pool/example.tf

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ module "example" {
2828
compute_engine_service_account = var.compute_engine_service_account
2929

3030
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-
}
31+
max_cpu_cores = 20
32+
min_cpu_cores = 5
33+
max_memory_gb = 30
34+
min_memory_gb = 10
3835
}
3936
}
4037

0 commit comments

Comments
 (0)