Skip to content

Commit

Permalink
feat: Add wait_for_instances and configurable timeout support for mig (
Browse files Browse the repository at this point in the history
…#96)

BREAKING CHANGE: instance_redistribution_type must now be specified for update policies.
  • Loading branch information
seanson authored Jun 23, 2020
1 parent 645e845 commit 10a23b7
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 38 deletions.
25 changes: 17 additions & 8 deletions autogen/main.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
target_pools = var.target_pools
target_size = var.autoscaling_enabled ? null : var.target_size

wait_for_instances = var.wait_for_instances

dynamic "auto_healing_policies" {
for_each = local.healthchecks
content {
Expand All @@ -91,20 +93,27 @@ resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
dynamic "update_policy" {
for_each = var.update_policy
content {
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
minimal_action = update_policy.value.minimal_action
type = update_policy.value.type
instance_redistribution_type = lookup(update_policy.value, "instance_redistribution_type", null)
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
minimal_action = update_policy.value.minimal_action
type = update_policy.value.type
}
}

lifecycle {
create_before_destroy = true
ignore_changes = [distribution_policy_zones]
}

timeouts {
create = var.mig_timeouts.create
update = var.mig_timeouts.update
delete = var.mig_timeouts.delete
}
}

resource "google_compute_region_autoscaler" "autoscaler" {
Expand Down Expand Up @@ -143,7 +152,7 @@ resource "google_compute_region_autoscaler" "autoscaler" {
}
{% if mig_with_percent %}

depends_on = ["google_compute_region_instance_group_manager.mig_with_percent"]
depends_on = [google_compute_region_instance_group_manager.mig_with_percent]
{% endif %}
}

Expand Down
34 changes: 27 additions & 7 deletions autogen/variables.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ variable "stateful_disks" {
variable "update_policy" {
description = "The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager.html#rolling_update_policy"
type = list(object({
max_surge_fixed = number
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
minimal_action = string
type = string
max_surge_fixed = number
instance_redistribution_type = string
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
minimal_action = string
type = string
}))
default = []
}
Expand Down Expand Up @@ -203,3 +204,22 @@ variable "named_ports" {
}))
default = []
}

variable "wait_for_instances" {
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."
default = "false"
}

variable "mig_timeouts" {
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. "
type = object({
create = string
update = string
delete = string
})
default = {
create = "5m"
update = "5m"
delete = "15m"
}
}
16 changes: 8 additions & 8 deletions examples/mig/full/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,14 @@ variable "distribution_policy_zones" {
variable "update_policy" {
description = "The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager.html#rolling_update_policy"
type = list(object({
max_surge_fixed = number
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
minimal_action = string
type = string
max_surge_fixed = number
instance_redistribution_type = string
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
minimal_action = string
type = string
}))
default = []
}
Expand Down Expand Up @@ -268,4 +269,3 @@ variable "autoscaling_enabled" {
type = bool
default = false
}

3 changes: 3 additions & 0 deletions modules/mig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ The current version is 2.X. The following guides are available to assist with up
| hostname | Hostname prefix for instances | string | `"default"` | no |
| instance\_template | Instance template self_link used to create compute instances | string | n/a | yes |
| 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 |
| 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 |
| min\_replicas | The minimum number of replicas that the autoscaler can scale down to. This cannot be less than 0. | string | `"2"` | no |
| named\_ports | Named name and named port. https://cloud.google.com/load-balancing/docs/backend-service#named_ports | object | `<list>` | no |
| network | Network to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
| project\_id | The GCP project ID | string | `"null"` | no |
| region | The GCP region where the managed instance group resides. | string | n/a | yes |
| 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 |
| subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
| subnetwork\_project | The project that subnetwork belongs to | string | `""` | no |
| target\_pools | The target load balancing pools to assign this group to. | list(string) | `<list>` | no |
| 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 |
| 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 |
| 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 |

## Outputs

Expand Down
8 changes: 8 additions & 0 deletions modules/mig/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ resource "google_compute_region_instance_group_manager" "mig" {
target_pools = var.target_pools
target_size = var.autoscaling_enabled ? null : var.target_size

wait_for_instances = var.wait_for_instances

dynamic "auto_healing_policies" {
for_each = local.healthchecks
content {
Expand Down Expand Up @@ -90,6 +92,12 @@ resource "google_compute_region_instance_group_manager" "mig" {
create_before_destroy = true
ignore_changes = [distribution_policy_zones]
}

timeouts {
create = var.mig_timeouts.create
update = var.mig_timeouts.update
delete = var.mig_timeouts.delete
}
}

resource "google_compute_region_autoscaler" "autoscaler" {
Expand Down
20 changes: 20 additions & 0 deletions modules/mig/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ variable "stateful_disks" {
}))
default = []
}

#################
# Rolling Update
#################
Expand Down Expand Up @@ -188,3 +189,22 @@ variable "named_ports" {
}))
default = []
}

variable "wait_for_instances" {
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."
default = "false"
}

variable "mig_timeouts" {
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. "
type = object({
create = string
update = string
delete = string
})
default = {
create = "5m"
update = "5m"
delete = "15m"
}
}
3 changes: 3 additions & 0 deletions modules/mig_with_percent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ The current version is 2.X. The following guides are available to assist with up
| instance\_template\_initial\_version | Instance template self_link used to create compute instances for the initial version | string | n/a | yes |
| instance\_template\_next\_version | Instance template self_link used to create compute instances for the second version | string | n/a | yes |
| 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 |
| 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 |
| min\_replicas | The minimum number of replicas that the autoscaler can scale down to. This cannot be less than 0. | string | `"2"` | no |
| named\_ports | Named name and named port. https://cloud.google.com/load-balancing/docs/backend-service#named_ports | object | `<list>` | no |
| network | Network to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
| next\_version\_percent | Percentage of instances defined in the second version | string | n/a | yes |
| project\_id | The GCP project ID | string | `"null"` | no |
| region | The GCP region where the managed instance group resides. | string | n/a | yes |
| 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 |
| subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
| subnetwork\_project | The project that subnetwork belongs to | string | `""` | no |
| target\_pools | The target load balancing pools to assign this group to. | list(string) | `<list>` | no |
| 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 |
| 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 |
| 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 |

## Outputs

Expand Down
33 changes: 25 additions & 8 deletions modules/mig_with_percent/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ resource "google_compute_region_instance_group_manager" "mig_with_percent" {
target_pools = var.target_pools
target_size = var.autoscaling_enabled ? null : var.target_size

wait_for_instances = var.wait_for_instances

dynamic "auto_healing_policies" {
for_each = local.healthchecks
content {
Expand All @@ -72,24 +74,39 @@ resource "google_compute_region_instance_group_manager" "mig_with_percent" {
}
}

dynamic "stateful_disk" {
for_each = var.stateful_disks
content {
device_name = stateful_disk.value.device_name
delete_rule = lookup(stateful_disk.value, "delete_rule", null)
}
}

distribution_policy_zones = local.distribution_policy_zones
dynamic "update_policy" {
for_each = var.update_policy
content {
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
minimal_action = update_policy.value.minimal_action
type = update_policy.value.type
instance_redistribution_type = lookup(update_policy.value, "instance_redistribution_type", null)
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
minimal_action = update_policy.value.minimal_action
type = update_policy.value.type
}
}

lifecycle {
create_before_destroy = true
ignore_changes = [distribution_policy_zones]
}

timeouts {
create = var.mig_timeouts.create
update = var.mig_timeouts.update
delete = var.mig_timeouts.delete
}
}

resource "google_compute_region_autoscaler" "autoscaler" {
Expand Down Expand Up @@ -127,7 +144,7 @@ resource "google_compute_region_autoscaler" "autoscaler" {
}
}

depends_on = ["google_compute_region_instance_group_manager.mig_with_percent"]
depends_on = [google_compute_region_instance_group_manager.mig_with_percent]
}

resource "google_compute_health_check" "http" {
Expand Down
46 changes: 39 additions & 7 deletions modules/mig_with_percent/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,33 @@ variable "distribution_policy_zones" {
default = []
}

#################
# Stateful disks
#################
variable "stateful_disks" {
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"
type = list(object({
device_name = string
delete_rule = string
}))
default = []
}

#################
# Rolling Update
#################

variable "update_policy" {
description = "The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager.html#rolling_update_policy"
type = list(object({
max_surge_fixed = number
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
minimal_action = string
type = string
max_surge_fixed = number
instance_redistribution_type = string
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
minimal_action = string
type = string
}))
default = []
}
Expand Down Expand Up @@ -184,3 +197,22 @@ variable "named_ports" {
}))
default = []
}

variable "wait_for_instances" {
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."
default = "false"
}

variable "mig_timeouts" {
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. "
type = object({
create = string
update = string
delete = string
})
default = {
create = "5m"
update = "5m"
delete = "15m"
}
}

0 comments on commit 10a23b7

Please sign in to comment.