Skip to content

Commit 10a23b7

Browse files
authored
feat: Add wait_for_instances and configurable timeout support for mig (#96)
BREAKING CHANGE: instance_redistribution_type must now be specified for update policies.
1 parent 645e845 commit 10a23b7

File tree

9 files changed

+150
-38
lines changed

9 files changed

+150
-38
lines changed

autogen/main.tf.tmpl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
7171
target_pools = var.target_pools
7272
target_size = var.autoscaling_enabled ? null : var.target_size
7373

74+
wait_for_instances = var.wait_for_instances
75+
7476
dynamic "auto_healing_policies" {
7577
for_each = local.healthchecks
7678
content {
@@ -91,20 +93,27 @@ resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
9193
dynamic "update_policy" {
9294
for_each = var.update_policy
9395
content {
94-
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
95-
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
96-
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
97-
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
98-
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
99-
minimal_action = update_policy.value.minimal_action
100-
type = update_policy.value.type
96+
instance_redistribution_type = lookup(update_policy.value, "instance_redistribution_type", null)
97+
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
98+
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
99+
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
100+
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
101+
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
102+
minimal_action = update_policy.value.minimal_action
103+
type = update_policy.value.type
101104
}
102105
}
103106

104107
lifecycle {
105108
create_before_destroy = true
106109
ignore_changes = [distribution_policy_zones]
107110
}
111+
112+
timeouts {
113+
create = var.mig_timeouts.create
114+
update = var.mig_timeouts.update
115+
delete = var.mig_timeouts.delete
116+
}
108117
}
109118

110119
resource "google_compute_region_autoscaler" "autoscaler" {
@@ -143,7 +152,7 @@ resource "google_compute_region_autoscaler" "autoscaler" {
143152
}
144153
{% if mig_with_percent %}
145154

146-
depends_on = ["google_compute_region_instance_group_manager.mig_with_percent"]
155+
depends_on = [google_compute_region_instance_group_manager.mig_with_percent]
147156
{% endif %}
148157
}
149158

autogen/variables.tf.tmpl

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ variable "stateful_disks" {
8686
variable "update_policy" {
8787
description = "The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager.html#rolling_update_policy"
8888
type = list(object({
89-
max_surge_fixed = number
90-
max_surge_percent = number
91-
max_unavailable_fixed = number
92-
max_unavailable_percent = number
93-
min_ready_sec = number
94-
minimal_action = string
95-
type = string
89+
max_surge_fixed = number
90+
instance_redistribution_type = string
91+
max_surge_percent = number
92+
max_unavailable_fixed = number
93+
max_unavailable_percent = number
94+
min_ready_sec = number
95+
minimal_action = string
96+
type = string
9697
}))
9798
default = []
9899
}
@@ -203,3 +204,22 @@ variable "named_ports" {
203204
}))
204205
default = []
205206
}
207+
208+
variable "wait_for_instances" {
209+
description = "Whether to wait for all instances to be created/updated before returning. Note that if this is set to true and the operation does not succeed, Terraform will continue trying until it times out."
210+
default = "false"
211+
}
212+
213+
variable "mig_timeouts" {
214+
description = "Times for creation, deleting and updating the MIG resources. Can be helpful when using wait_for_instances to allow a longer VM startup time. "
215+
type = object({
216+
create = string
217+
update = string
218+
delete = string
219+
})
220+
default = {
221+
create = "5m"
222+
update = "5m"
223+
delete = "15m"
224+
}
225+
}

examples/mig/full/variables.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,14 @@ variable "distribution_policy_zones" {
179179
variable "update_policy" {
180180
description = "The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager.html#rolling_update_policy"
181181
type = list(object({
182-
max_surge_fixed = number
183-
max_surge_percent = number
184-
max_unavailable_fixed = number
185-
max_unavailable_percent = number
186-
min_ready_sec = number
187-
minimal_action = string
188-
type = string
182+
max_surge_fixed = number
183+
instance_redistribution_type = string
184+
max_surge_percent = number
185+
max_unavailable_fixed = number
186+
max_unavailable_percent = number
187+
min_ready_sec = number
188+
minimal_action = string
189+
type = string
189190
}))
190191
default = []
191192
}
@@ -268,4 +269,3 @@ variable "autoscaling_enabled" {
268269
type = bool
269270
default = false
270271
}
271-

modules/mig/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ The current version is 2.X. The following guides are available to assist with up
2828
| hostname | Hostname prefix for instances | string | `"default"` | no |
2929
| instance\_template | Instance template self_link used to create compute instances | string | n/a | yes |
3030
| max\_replicas | The maximum number of instances that the autoscaler can scale up to. This is required when creating or updating an autoscaler. The maximum number of replicas should not be lower than minimal number of replicas. | string | `"10"` | no |
31+
| mig\_timeouts | Times for creation, deleting and updating the MIG resources. Can be helpful when using wait_for_instances to allow a longer VM startup time. | object | `<map>` | no |
3132
| min\_replicas | The minimum number of replicas that the autoscaler can scale down to. This cannot be less than 0. | string | `"2"` | no |
3233
| named\_ports | Named name and named port. https://cloud.google.com/load-balancing/docs/backend-service#named_ports | object | `<list>` | no |
3334
| network | Network to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
3435
| project\_id | The GCP project ID | string | `"null"` | no |
3536
| region | The GCP region where the managed instance group resides. | string | n/a | yes |
37+
| stateful\_disks | Disks created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-disks-in-migs | object | `<list>` | no |
3638
| subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
3739
| subnetwork\_project | The project that subnetwork belongs to | string | `""` | no |
3840
| target\_pools | The target load balancing pools to assign this group to. | list(string) | `<list>` | no |
3941
| target\_size | The target number of running instances for this managed instance group. This value should always be explicitly set unless this resource is attached to an autoscaler, in which case it should never be set. | string | `"1"` | no |
4042
| update\_policy | The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager.html#rolling_update_policy | object | `<list>` | no |
43+
| wait\_for\_instances | Whether to wait for all instances to be created/updated before returning. Note that if this is set to true and the operation does not succeed, Terraform will continue trying until it times out. | string | `"false"` | no |
4144

4245
## Outputs
4346

modules/mig/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ resource "google_compute_region_instance_group_manager" "mig" {
5555
target_pools = var.target_pools
5656
target_size = var.autoscaling_enabled ? null : var.target_size
5757

58+
wait_for_instances = var.wait_for_instances
59+
5860
dynamic "auto_healing_policies" {
5961
for_each = local.healthchecks
6062
content {
@@ -90,6 +92,12 @@ resource "google_compute_region_instance_group_manager" "mig" {
9092
create_before_destroy = true
9193
ignore_changes = [distribution_policy_zones]
9294
}
95+
96+
timeouts {
97+
create = var.mig_timeouts.create
98+
update = var.mig_timeouts.update
99+
delete = var.mig_timeouts.delete
100+
}
93101
}
94102

95103
resource "google_compute_region_autoscaler" "autoscaler" {

modules/mig/variables.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ variable "stateful_disks" {
6363
}))
6464
default = []
6565
}
66+
6667
#################
6768
# Rolling Update
6869
#################
@@ -188,3 +189,22 @@ variable "named_ports" {
188189
}))
189190
default = []
190191
}
192+
193+
variable "wait_for_instances" {
194+
description = "Whether to wait for all instances to be created/updated before returning. Note that if this is set to true and the operation does not succeed, Terraform will continue trying until it times out."
195+
default = "false"
196+
}
197+
198+
variable "mig_timeouts" {
199+
description = "Times for creation, deleting and updating the MIG resources. Can be helpful when using wait_for_instances to allow a longer VM startup time. "
200+
type = object({
201+
create = string
202+
update = string
203+
delete = string
204+
})
205+
default = {
206+
create = "5m"
207+
update = "5m"
208+
delete = "15m"
209+
}
210+
}

modules/mig_with_percent/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,20 @@ The current version is 2.X. The following guides are available to assist with up
2828
| instance\_template\_initial\_version | Instance template self_link used to create compute instances for the initial version | string | n/a | yes |
2929
| instance\_template\_next\_version | Instance template self_link used to create compute instances for the second version | string | n/a | yes |
3030
| max\_replicas | The maximum number of instances that the autoscaler can scale up to. This is required when creating or updating an autoscaler. The maximum number of replicas should not be lower than minimal number of replicas. | string | `"10"` | no |
31+
| mig\_timeouts | Times for creation, deleting and updating the MIG resources. Can be helpful when using wait_for_instances to allow a longer VM startup time. | object | `<map>` | no |
3132
| min\_replicas | The minimum number of replicas that the autoscaler can scale down to. This cannot be less than 0. | string | `"2"` | no |
3233
| named\_ports | Named name and named port. https://cloud.google.com/load-balancing/docs/backend-service#named_ports | object | `<list>` | no |
3334
| network | Network to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
3435
| next\_version\_percent | Percentage of instances defined in the second version | string | n/a | yes |
3536
| project\_id | The GCP project ID | string | `"null"` | no |
3637
| region | The GCP region where the managed instance group resides. | string | n/a | yes |
38+
| stateful\_disks | Disks created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-disks-in-migs | object | `<list>` | no |
3739
| subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
3840
| subnetwork\_project | The project that subnetwork belongs to | string | `""` | no |
3941
| target\_pools | The target load balancing pools to assign this group to. | list(string) | `<list>` | no |
4042
| target\_size | The target number of running instances for this managed instance group. This value should always be explicitly set unless this resource is attached to an autoscaler, in which case it should never be set. | string | `"1"` | no |
4143
| update\_policy | The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager.html#rolling_update_policy | object | `<list>` | no |
44+
| wait\_for\_instances | Whether to wait for all instances to be created/updated before returning. Note that if this is set to true and the operation does not succeed, Terraform will continue trying until it times out. | string | `"false"` | no |
4245

4346
## Outputs
4447

modules/mig_with_percent/main.tf

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ resource "google_compute_region_instance_group_manager" "mig_with_percent" {
6464
target_pools = var.target_pools
6565
target_size = var.autoscaling_enabled ? null : var.target_size
6666

67+
wait_for_instances = var.wait_for_instances
68+
6769
dynamic "auto_healing_policies" {
6870
for_each = local.healthchecks
6971
content {
@@ -72,24 +74,39 @@ resource "google_compute_region_instance_group_manager" "mig_with_percent" {
7274
}
7375
}
7476

77+
dynamic "stateful_disk" {
78+
for_each = var.stateful_disks
79+
content {
80+
device_name = stateful_disk.value.device_name
81+
delete_rule = lookup(stateful_disk.value, "delete_rule", null)
82+
}
83+
}
84+
7585
distribution_policy_zones = local.distribution_policy_zones
7686
dynamic "update_policy" {
7787
for_each = var.update_policy
7888
content {
79-
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
80-
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
81-
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
82-
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
83-
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
84-
minimal_action = update_policy.value.minimal_action
85-
type = update_policy.value.type
89+
instance_redistribution_type = lookup(update_policy.value, "instance_redistribution_type", null)
90+
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
91+
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
92+
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
93+
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
94+
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
95+
minimal_action = update_policy.value.minimal_action
96+
type = update_policy.value.type
8697
}
8798
}
8899

89100
lifecycle {
90101
create_before_destroy = true
91102
ignore_changes = [distribution_policy_zones]
92103
}
104+
105+
timeouts {
106+
create = var.mig_timeouts.create
107+
update = var.mig_timeouts.update
108+
delete = var.mig_timeouts.delete
109+
}
93110
}
94111

95112
resource "google_compute_region_autoscaler" "autoscaler" {
@@ -127,7 +144,7 @@ resource "google_compute_region_autoscaler" "autoscaler" {
127144
}
128145
}
129146

130-
depends_on = ["google_compute_region_instance_group_manager.mig_with_percent"]
147+
depends_on = [google_compute_region_instance_group_manager.mig_with_percent]
131148
}
132149

133150
resource "google_compute_health_check" "http" {

modules/mig_with_percent/variables.tf

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,33 @@ variable "distribution_policy_zones" {
6060
default = []
6161
}
6262

63+
#################
64+
# Stateful disks
65+
#################
66+
variable "stateful_disks" {
67+
description = "Disks created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-disks-in-migs"
68+
type = list(object({
69+
device_name = string
70+
delete_rule = string
71+
}))
72+
default = []
73+
}
74+
6375
#################
6476
# Rolling Update
6577
#################
6678

6779
variable "update_policy" {
6880
description = "The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager.html#rolling_update_policy"
6981
type = list(object({
70-
max_surge_fixed = number
71-
max_surge_percent = number
72-
max_unavailable_fixed = number
73-
max_unavailable_percent = number
74-
min_ready_sec = number
75-
minimal_action = string
76-
type = string
82+
max_surge_fixed = number
83+
instance_redistribution_type = string
84+
max_surge_percent = number
85+
max_unavailable_fixed = number
86+
max_unavailable_percent = number
87+
min_ready_sec = number
88+
minimal_action = string
89+
type = string
7790
}))
7891
default = []
7992
}
@@ -184,3 +197,22 @@ variable "named_ports" {
184197
}))
185198
default = []
186199
}
200+
201+
variable "wait_for_instances" {
202+
description = "Whether to wait for all instances to be created/updated before returning. Note that if this is set to true and the operation does not succeed, Terraform will continue trying until it times out."
203+
default = "false"
204+
}
205+
206+
variable "mig_timeouts" {
207+
description = "Times for creation, deleting and updating the MIG resources. Can be helpful when using wait_for_instances to allow a longer VM startup time. "
208+
type = object({
209+
create = string
210+
update = string
211+
delete = string
212+
})
213+
default = {
214+
create = "5m"
215+
update = "5m"
216+
delete = "15m"
217+
}
218+
}

0 commit comments

Comments
 (0)