Skip to content

Commit 5c0f061

Browse files
authored
feat: Add support for configuring autoscaler scale_in on MIGs (#166)
1 parent 5837759 commit 5c0f061

File tree

10 files changed

+102
-10
lines changed

10 files changed

+102
-10
lines changed

autogen/main.tf.tmpl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ locals {
2525
default = data.google_compute_zones.available.names
2626
user = var.distribution_policy_zones
2727
}
28-
distribution_policy_zones = local.distribution_policy_zones_base[length(var.distribution_policy_zones) == 0 ? "default" : "user"]
28+
distribution_policy_zones = local.distribution_policy_zones_base[length(var.distribution_policy_zones) == 0 ? "default" : "user"]
29+
autoscaling_scale_in_enabled = var.autoscaling_scale_in_control.fixed_replicas != null || var.autoscaling_scale_in_control.percent_replicas != null
2930
}
3031

3132
data "google_compute_zones" "available" {
@@ -129,6 +130,16 @@ resource "google_compute_region_autoscaler" "autoscaler" {
129130
max_replicas = var.max_replicas
130131
min_replicas = var.min_replicas
131132
cooldown_period = var.cooldown_period
133+
dynamic "scale_in_control" {
134+
for_each = local.autoscaling_scale_in_enabled ? [var.autoscaling_scale_in_control] : []
135+
content {
136+
max_scaled_in_replicas {
137+
fixed = lookup(scale_in_control.value, "fixed_replicas", null)
138+
percent = lookup(scale_in_control.value, "percent_replicas", null)
139+
}
140+
time_window_sec = lookup(scale_in_control.value, "time_window_sec", null)
141+
}
142+
}
132143
dynamic "cpu_utilization" {
133144
for_each = var.autoscaling_cpu
134145
content {

autogen/variables.tf.tmpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ variable "autoscaling_lb" {
184184
default = []
185185
}
186186

187+
variable "autoscaling_scale_in_control" {
188+
description = "Autoscaling, scale-in control block. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#scale_in_control"
189+
type = object({
190+
fixed_replicas = number
191+
percent_replicas = number
192+
time_window_sec = number
193+
})
194+
default = {
195+
fixed_replicas = null
196+
percent_replicas = null
197+
time_window_sec = null
198+
}
199+
}
200+
187201
##########################
188202

189203
variable "network" {

examples/mig/full/main.tf

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ module "mig" {
7676
health_check = var.health_check
7777

7878
/* autoscaler */
79-
autoscaling_enabled = var.autoscaling_enabled
80-
max_replicas = var.max_replicas
81-
min_replicas = var.min_replicas
82-
cooldown_period = var.cooldown_period
83-
autoscaling_cpu = var.autoscaling_cpu
84-
autoscaling_metric = var.autoscaling_metric
85-
autoscaling_lb = var.autoscaling_lb
79+
autoscaling_enabled = var.autoscaling_enabled
80+
max_replicas = var.max_replicas
81+
min_replicas = var.min_replicas
82+
cooldown_period = var.cooldown_period
83+
autoscaling_cpu = var.autoscaling_cpu
84+
autoscaling_metric = var.autoscaling_metric
85+
autoscaling_lb = var.autoscaling_lb
86+
autoscaling_scale_in_control = var.autoscaling_scale_in_control
8687
}
8788

examples/mig/full/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,20 @@ variable "autoscaling_lb" {
266266
default = []
267267
}
268268

269+
variable "autoscaling_scale_in_control" {
270+
description = "Autoscaling, scale-in control block. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#scale_in_control"
271+
type = object({
272+
fixed_replicas = number
273+
percent_replicas = number
274+
time_window_sec = number
275+
})
276+
default = {
277+
fixed_replicas = 0
278+
percent_replicas = 30
279+
time_window_sec = 600
280+
}
281+
}
282+
269283
variable "autoscaling_enabled" {
270284
description = "Creates an autoscaler for the managed instance group"
271285
type = bool

modules/mig/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The current version is 2.X. The following guides are available to assist with up
2222
| autoscaling\_enabled | Creates an autoscaler for the managed instance group | `string` | `"false"` | no |
2323
| autoscaling\_lb | Autoscaling, load balancing utilization policy block as single element array. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#load_balancing_utilization | `list(map(number))` | `[]` | no |
2424
| autoscaling\_metric | Autoscaling, metric policy block as single element array. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#metric | <pre>list(object({<br> name = string<br> target = number<br> type = string<br> }))</pre> | `[]` | no |
25+
| autoscaling\_scale\_in\_control | Autoscaling, scale-in control block. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#scale_in_control | <pre>object({<br> fixed_replicas = number<br> percent_replicas = number<br> time_window_sec = number<br> })</pre> | <pre>{<br> "fixed_replicas": null,<br> "percent_replicas": null,<br> "time_window_sec": null<br>}</pre> | no |
2526
| cooldown\_period | The number of seconds that the autoscaler should wait before it starts collecting information from a new instance. | `number` | `60` | no |
2627
| distribution\_policy\_zones | The distribution policy, i.e. which zone(s) should instances be create in. Default is all zones in given region. | `list(string)` | `[]` | no |
2728
| health\_check | Health check to determine whether instances are responsive and able to do work | <pre>object({<br> type = string<br> initial_delay_sec = number<br> check_interval_sec = number<br> healthy_threshold = number<br> timeout_sec = number<br> unhealthy_threshold = number<br> response = string<br> proxy_header = string<br> port = number<br> request = string<br> request_path = string<br> host = string<br> })</pre> | <pre>{<br> "check_interval_sec": 30,<br> "healthy_threshold": 1,<br> "host": "",<br> "initial_delay_sec": 30,<br> "port": 80,<br> "proxy_header": "NONE",<br> "request": "",<br> "request_path": "/",<br> "response": "",<br> "timeout_sec": 10,<br> "type": "",<br> "unhealthy_threshold": 5<br>}</pre> | no |

modules/mig/main.tf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ locals {
2525
default = data.google_compute_zones.available.names
2626
user = var.distribution_policy_zones
2727
}
28-
distribution_policy_zones = local.distribution_policy_zones_base[length(var.distribution_policy_zones) == 0 ? "default" : "user"]
28+
distribution_policy_zones = local.distribution_policy_zones_base[length(var.distribution_policy_zones) == 0 ? "default" : "user"]
29+
autoscaling_scale_in_enabled = var.autoscaling_scale_in_control.fixed_replicas != null || var.autoscaling_scale_in_control.percent_replicas != null
2930
}
3031

3132
data "google_compute_zones" "available" {
@@ -113,6 +114,16 @@ resource "google_compute_region_autoscaler" "autoscaler" {
113114
max_replicas = var.max_replicas
114115
min_replicas = var.min_replicas
115116
cooldown_period = var.cooldown_period
117+
dynamic "scale_in_control" {
118+
for_each = local.autoscaling_scale_in_enabled ? [var.autoscaling_scale_in_control] : []
119+
content {
120+
max_scaled_in_replicas {
121+
fixed = lookup(scale_in_control.value, "fixed_replicas", null)
122+
percent = lookup(scale_in_control.value, "percent_replicas", null)
123+
}
124+
time_window_sec = lookup(scale_in_control.value, "time_window_sec", null)
125+
}
126+
}
116127
dynamic "cpu_utilization" {
117128
for_each = var.autoscaling_cpu
118129
content {

modules/mig/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ variable "autoscaling_lb" {
169169
default = []
170170
}
171171

172+
variable "autoscaling_scale_in_control" {
173+
description = "Autoscaling, scale-in control block. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#scale_in_control"
174+
type = object({
175+
fixed_replicas = number
176+
percent_replicas = number
177+
time_window_sec = number
178+
})
179+
default = {
180+
fixed_replicas = null
181+
percent_replicas = null
182+
time_window_sec = null
183+
}
184+
}
185+
172186
##########################
173187

174188
variable "network" {

modules/mig_with_percent/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The current version is 2.X. The following guides are available to assist with up
2121
| autoscaling\_enabled | Creates an autoscaler for the managed instance group | `string` | `"false"` | no |
2222
| autoscaling\_lb | Autoscaling, load balancing utilization policy block as single element array. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#load_balancing_utilization | `list(map(number))` | `[]` | no |
2323
| autoscaling\_metric | Autoscaling, metric policy block as single element array. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#metric | <pre>list(object({<br> name = string<br> target = number<br> type = string<br> }))</pre> | `[]` | no |
24+
| autoscaling\_scale\_in\_control | Autoscaling, scale-in control block. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#scale_in_control | <pre>object({<br> fixed_replicas = number<br> percent_replicas = number<br> time_window_sec = number<br> })</pre> | <pre>{<br> "fixed_replicas": null,<br> "percent_replicas": null,<br> "time_window_sec": null<br>}</pre> | no |
2425
| cooldown\_period | The number of seconds that the autoscaler should wait before it starts collecting information from a new instance. | `number` | `60` | no |
2526
| distribution\_policy\_zones | The distribution policy, i.e. which zone(s) should instances be create in. Default is all zones in given region. | `list(string)` | `[]` | no |
2627
| health\_check | Health check to determine whether instances are responsive and able to do work | <pre>object({<br> type = string<br> initial_delay_sec = number<br> check_interval_sec = number<br> healthy_threshold = number<br> timeout_sec = number<br> unhealthy_threshold = number<br> response = string<br> proxy_header = string<br> port = number<br> request = string<br> request_path = string<br> host = string<br> })</pre> | <pre>{<br> "check_interval_sec": 30,<br> "healthy_threshold": 1,<br> "host": "",<br> "initial_delay_sec": 30,<br> "port": 80,<br> "proxy_header": "NONE",<br> "request": "",<br> "request_path": "/",<br> "response": "",<br> "timeout_sec": 10,<br> "type": "",<br> "unhealthy_threshold": 5<br>}</pre> | no |

modules/mig_with_percent/main.tf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ locals {
2525
default = data.google_compute_zones.available.names
2626
user = var.distribution_policy_zones
2727
}
28-
distribution_policy_zones = local.distribution_policy_zones_base[length(var.distribution_policy_zones) == 0 ? "default" : "user"]
28+
distribution_policy_zones = local.distribution_policy_zones_base[length(var.distribution_policy_zones) == 0 ? "default" : "user"]
29+
autoscaling_scale_in_enabled = var.autoscaling_scale_in_control.fixed_replicas != null || var.autoscaling_scale_in_control.percent_replicas != null
2930
}
3031

3132
data "google_compute_zones" "available" {
@@ -122,6 +123,16 @@ resource "google_compute_region_autoscaler" "autoscaler" {
122123
max_replicas = var.max_replicas
123124
min_replicas = var.min_replicas
124125
cooldown_period = var.cooldown_period
126+
dynamic "scale_in_control" {
127+
for_each = local.autoscaling_scale_in_enabled ? [var.autoscaling_scale_in_control] : []
128+
content {
129+
max_scaled_in_replicas {
130+
fixed = lookup(scale_in_control.value, "fixed_replicas", null)
131+
percent = lookup(scale_in_control.value, "percent_replicas", null)
132+
}
133+
time_window_sec = lookup(scale_in_control.value, "time_window_sec", null)
134+
}
135+
}
125136
dynamic "cpu_utilization" {
126137
for_each = var.autoscaling_cpu
127138
content {

modules/mig_with_percent/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ variable "autoscaling_lb" {
177177
default = []
178178
}
179179

180+
variable "autoscaling_scale_in_control" {
181+
description = "Autoscaling, scale-in control block. https://www.terraform.io/docs/providers/google/r/compute_autoscaler.html#scale_in_control"
182+
type = object({
183+
fixed_replicas = number
184+
percent_replicas = number
185+
time_window_sec = number
186+
})
187+
default = {
188+
fixed_replicas = null
189+
percent_replicas = null
190+
time_window_sec = null
191+
}
192+
}
193+
180194
##########################
181195

182196
variable "network" {

0 commit comments

Comments
 (0)