Skip to content

Commit

Permalink
feat: mig labels & most disruptive update policy action update (#381)
Browse files Browse the repository at this point in the history
Co-authored-by: Awais Malik <[email protected]>
  • Loading branch information
rauny-brandao and g-awmalik authored Feb 23, 2024
1 parent f6ebf2c commit 61ba9bf
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 139 deletions.
23 changes: 14 additions & 9 deletions autogen/main.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,23 @@ resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
dynamic "update_policy" {
for_each = var.update_policy
content {
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)
replacement_method = lookup(update_policy.value, "replacement_method", 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)
replacement_method = lookup(update_policy.value, "replacement_method", null)
most_disruptive_allowed_action = lookup(update_policy.value, "most_disruptive_allowed_action", null)
minimal_action = update_policy.value.minimal_action
type = update_policy.value.type
}
}

all_instances_config {
labels = var.labels
}

lifecycle {
create_before_destroy = true
ignore_changes = [distribution_policy_zones]
Expand Down
25 changes: 16 additions & 9 deletions autogen/variables.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ variable "stateful_ips" {
variable "update_policy" {
description = "The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager#rolling_update_policy"
type = list(object({
max_surge_fixed = number
instance_redistribution_type = string
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
replacement_method = string
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
replacement_method = string
minimal_action = string
type = string
most_disruptive_allowed_action = string
}))
default = []
}
Expand Down Expand Up @@ -296,3 +297,9 @@ variable "mig_timeouts" {
delete = "15m"
}
}

variable "labels" {
type = map(string)
description = "Labels, provided as a map"
default = {}
}
19 changes: 10 additions & 9 deletions examples/mig/full/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,16 @@ 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#rolling_update_policy"
type = list(object({
max_surge_fixed = number
instance_redistribution_type = string
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
replacement_method = string
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
replacement_method = string
minimal_action = string
type = string
most_disruptive_allowed_action = string
}))
default = []
}
Expand Down
19 changes: 10 additions & 9 deletions examples/mig_stateful/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ module "mig" {
}]

update_policy = [{
max_surge_fixed = 0
instance_redistribution_type = "NONE"
max_surge_percent = null
max_unavailable_fixed = 4
max_unavailable_percent = null
min_ready_sec = 180
replacement_method = "RECREATE"
minimal_action = "REFRESH"
type = "OPPORTUNISTIC"
max_surge_fixed = 0
instance_redistribution_type = "NONE"
max_surge_percent = null
max_unavailable_fixed = 4
max_unavailable_percent = null
min_ready_sec = 180
replacement_method = "RECREATE"
minimal_action = "REFRESH"
type = "OPPORTUNISTIC"
most_disruptive_allowed_action = "REFRESH"
}]
}
2 changes: 1 addition & 1 deletion metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Google LLC
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
8 changes: 1 addition & 7 deletions modules/compute_disk_snapshot/metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Google LLC
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,17 +85,14 @@ spec:
- name: name
description: Name of the resource policy to create
varType: string
defaultValue: null
required: true
- name: project
description: The project ID where the resources will be created
varType: string
defaultValue: null
required: true
- name: region
description: Region where resource policy resides
varType: string
defaultValue: null
required: true
- name: snapshot_properties
description: The properties of the schedule policy. For more details see https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_resource_policy#snapshot_properties
Expand All @@ -107,7 +104,6 @@ spec:
storage_locations = list(string)
}
)
defaultValue: null
- name: snapshot_retention_policy
description: The retention policy to be applied to the schedule policy. For more details see https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_resource_policy#retention_policy
varType: |-
Expand All @@ -117,7 +113,6 @@ spec:
on_source_disk_delete = string
}
)
defaultValue: null
required: true
- name: snapshot_schedule
description: The scheduled to be used by the snapshot policy. For more details see https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_resource_policy#schedule
Expand Down Expand Up @@ -148,7 +143,6 @@ spec:
)
}
)
defaultValue: null
required: true
outputs:
- name: attachments
Expand Down
7 changes: 1 addition & 6 deletions modules/compute_instance/metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Google LLC
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -109,7 +109,6 @@ spec:
- name: instance_template
description: Instance template self_link used to create compute instances
varType: string
defaultValue: null
required: true
- name: ipv6_access_config
description: IPv6 access configurations. Currently a max of 1 IPv6 access configuration is supported. If not specified, the instance will have no external IPv6 Internet access.
Expand All @@ -121,7 +120,6 @@ spec:
- name: labels
description: (Optional) Labels to override those from the template, provided as a map
varType: map(string)
defaultValue: null
- name: network
description: Network to deploy to. Only one of network or subnetwork should be specified.
varType: string
Expand All @@ -133,11 +131,9 @@ spec:
- name: region
description: Region where the instances should be created.
varType: string
defaultValue: null
- name: resource_manager_tags
description: (Optional) A tag is a key-value pair that can be attached to a Google Cloud resource. You can use tags to conditionally allow or deny policies based on whether a resource has a specific tag. This value is not returned by the API. In Terraform, this value cannot be updated and changing it will recreate the resource.
varType: map(string)
defaultValue: null
- name: resource_policies
description: (Optional) A list of short names or self_links of resource policies to attach to the instance. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported.
varType: list(string)
Expand All @@ -157,7 +153,6 @@ spec:
- name: zone
description: Zone where the instances should be created. If not specified, instances will be spread across available zones in the region.
varType: string
defaultValue: null
outputs:
- name: available_zones
description: List of available zones in region
Expand Down
13 changes: 1 addition & 12 deletions modules/instance_template/metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Google LLC
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -130,7 +130,6 @@ spec:
ip_cidr_range = string
subnetwork_range_name = string
})
defaultValue: null
- name: auto_delete
description: Whether or not the boot disk should be auto-deleted
varType: string
Expand All @@ -146,7 +145,6 @@ spec:
- name: disk_encryption_key
description: The id of the encryption key that is stored in Google Cloud KMS to use to encrypt all the disks on this instance
varType: string
defaultValue: null
- name: disk_labels
description: Labels to be assigned to boot disk, provided as a map
varType: map(string)
Expand Down Expand Up @@ -178,7 +176,6 @@ spec:
type = string
count = number
})
defaultValue: null
- name: ipv6_access_config
description: IPv6 access configurations. Currently a max of 1 IPv6 access configuration is supported. If not specified, the instance will have no external IPv6 Internet access.
varType: |-
Expand All @@ -197,15 +194,13 @@ spec:
- name: maintenance_interval
description: Specifies the frequency of planned maintenance events
varType: string
defaultValue: null
- name: metadata
description: Metadata, provided as a map
varType: map(string)
defaultValue: {}
- name: min_cpu_platform
description: "Specifies a minimum CPU platform. Applicable values are the friendly names of CPU platforms, such as Intel Haswell or Intel Skylake. See the complete list: https://cloud.google.com/compute/docs/instances/specify-min-cpu-platform"
varType: string
defaultValue: null
- name: name_prefix
description: Name prefix for the instance template
varType: string
Expand All @@ -221,7 +216,6 @@ spec:
- name: nic_type
description: Valid values are "VIRTIO_NET", "GVNIC" or set to null to accept API default behavior.
varType: string
defaultValue: null
- name: on_host_maintenance
description: Instance availability Policy
varType: string
Expand All @@ -233,11 +227,9 @@ spec:
- name: project_id
description: The GCP project ID
varType: string
defaultValue: null
- name: region
description: Region where the instance template should be created.
varType: string
defaultValue: null
- name: resource_policies
description: A list of self_links of resource policies to attach to the instance. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported.
varType: list(string)
Expand All @@ -249,7 +241,6 @@ spec:
email = string
scopes = set(string)
})
defaultValue: null
required: true
- name: shielded_instance_config
description: Not used unless enable_shielded_vm is true. Shielded VM configuration for the instance.
Expand Down Expand Up @@ -286,7 +277,6 @@ spec:
- name: stack_type
description: The stack type for this network interface to identify whether the IPv6 feature is enabled or not. Values are `IPV4_IPV6` or `IPV4_ONLY`. Default behavior is equivalent to IPV4_ONLY.
varType: string
defaultValue: null
- name: startup_script
description: User startup script to run when instances spin up
varType: string
Expand All @@ -306,7 +296,6 @@ spec:
- name: threads_per_core
description: The number of threads per physical core. To disable simultaneous multithreading (SMT) set this to 1.
varType: number
defaultValue: null
- name: total_egress_bandwidth_tier
description: Egress bandwidth tier setting for supported VM families
varType: string
Expand Down
3 changes: 2 additions & 1 deletion modules/mig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The current version is 2.X. The following guides are available to assist with up
| health\_check\_name | Health check name. When variable is empty, name will be derived from var.hostname. | `string` | `""` | no |
| hostname | Hostname prefix for instances | `string` | `"default"` | no |
| instance\_template | Instance template self\_link used to create compute instances | `string` | n/a | yes |
| labels | Labels, provided as a map | `map(string)` | `{}` | no |
| 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. | `number` | `10` | no |
| mig\_name | Managed instance group name. When variable is empty, name will be derived from var.hostname. | `string` | `""` | 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. | <pre>object({<br> create = string<br> update = string<br> delete = string<br> })</pre> | <pre>{<br> "create": "5m",<br> "delete": "15m",<br> "update": "5m"<br>}</pre> | no |
Expand All @@ -44,7 +45,7 @@ The current version is 2.X. The following guides are available to assist with up
| stateful\_ips | Statful IPs created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-ip-addresses-in-migs | <pre>list(object({<br> interface_name = string<br> delete_rule = string<br> is_external = bool<br> }))</pre> | `[]` | no |
| target\_pools | The target load balancing pools to assign this group to. | `list(string)` | `[]` | 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. | `number` | `1` | no |
| update\_policy | The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager#rolling_update_policy | <pre>list(object({<br> max_surge_fixed = number<br> instance_redistribution_type = string<br> max_surge_percent = number<br> max_unavailable_fixed = number<br> max_unavailable_percent = number<br> min_ready_sec = number<br> replacement_method = string<br> minimal_action = string<br> type = string<br> }))</pre> | `[]` | no |
| update\_policy | The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager#rolling_update_policy | <pre>list(object({<br> max_surge_fixed = number<br> instance_redistribution_type = string<br> max_surge_percent = number<br> max_unavailable_fixed = number<br> max_unavailable_percent = number<br> min_ready_sec = number<br> replacement_method = string<br> minimal_action = string<br> type = string<br> most_disruptive_allowed_action = string<br> }))</pre> | `[]` | 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
23 changes: 14 additions & 9 deletions modules/mig/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,23 @@ resource "google_compute_region_instance_group_manager" "mig" {
dynamic "update_policy" {
for_each = var.update_policy
content {
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)
replacement_method = lookup(update_policy.value, "replacement_method", 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)
replacement_method = lookup(update_policy.value, "replacement_method", null)
most_disruptive_allowed_action = lookup(update_policy.value, "most_disruptive_allowed_action", null)
minimal_action = update_policy.value.minimal_action
type = update_policy.value.type
}
}

all_instances_config {
labels = var.labels
}

lifecycle {
create_before_destroy = true
ignore_changes = [distribution_policy_zones]
Expand Down
Loading

0 comments on commit 61ba9bf

Please sign in to comment.