Skip to content

Commit

Permalink
feat: autoscaler supporting stateful ips
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyz committed Mar 3, 2023
1 parent db2a325 commit 9fba0ff
Show file tree
Hide file tree
Showing 20 changed files with 536 additions and 0 deletions.
16 changes: 16 additions & 0 deletions autogen/main.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
}
}

dynamic "stateful_internal_ip" {
for_each = var.stateful_internal_ip
content {
network_interface_name = stateful_internal_ip.value.network_interface_name
delete_rule = lookup(stateful_internal_ip.value, "delete_rule", null)
}
}

dynamic "stateful_external_ip" {
for_each = var.stateful_external_ip
content {
network_interface_name = stateful_external_ip.value.network_interface_name
delete_rule = lookup(stateful_external_ip.value, "delete_rule", null)
}
}

distribution_policy_zones = local.distribution_policy_zones
dynamic "update_policy" {
for_each = var.update_policy
Expand Down
21 changes: 21 additions & 0 deletions autogen/variables.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ variable "stateful_disks" {
default = []
}

#################
# Stateful IPs
#################
variable "stateful_internal_ips" {
description = "Internal 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"
type = list(object({
network_interface_name = string
delete_rule = string
}))
default = []
}

variable "stateful_external_ips" {
description = "External 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"
type = list(object({
network_interface_name = string
delete_rule = string
}))
default = []
}

#################
# Rolling Update
#################
Expand Down
24 changes: 24 additions & 0 deletions examples/mig/stateful/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# mig-simple

This is a simple, minimal example of how to use the MIG module to create a
managed instance group.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| project\_id | The GCP project to use for integration tests | `string` | n/a | yes |
| region | The GCP region to create and test resources in | `string` | `"us-central1"` | no |
| service\_account | Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template#service_account. | <pre>object({<br> email = string<br> scopes = set(string)<br> })</pre> | `null` | no |
| subnetwork | The subnetwork to host the compute instances in | `any` | n/a | yes |
| 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. | `any` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| region | The GCP region to create and test resources in |
| self\_link | Self-link of the managed instance group |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
79 changes: 79 additions & 0 deletions examples/mig/stateful/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

provider "google" {
project = var.project_id
region = var.region
}

provider "google-beta" {
project = var.project_id
region = var.region
}

resource "random_string" "suffix" {
length = 4
special = "false"
upper = "false"
}

/** Network **/

resource "google_compute_network" "main" {
project = var.project_id
name = "cft-vm-test-${random_string.suffix.result}"
auto_create_subnetworks = "false"
}

resource "google_compute_subnetwork" "main" {
project = var.project_id
region = var.region
name = "cft-vm-test-${random_string.suffix.result}"
ip_cidr_range = "10.128.0.0/20"
network = google_compute_network.main.self_link
}

module "instance_template" {
source = "../../../modules/instance_template"
project_id = var.project_id
subnetwork = var.subnetwork
service_account = var.service_account
subnetwork_project = var.project_id
}

module "mig" {
source = "../../../modules/mig"
project_id = var.project_id
region = var.region
target_size = var.target_size
hostname = "mig-stateful"
instance_template = module.instance_template.self_link
stateful_internal_ips = [{
network_interface_name = "nic0"
delete_rule = "ON_PERMANENT_INSTANCE_DELETION"
}]
}

resource "google_compute_region_disk" "default" {
project_id = var.project_id
type = "pd-ssd"
zone = var.region
image = "debian-11-bullseye-v20220719"
labels = {
environment = "dev"
}
physical_block_size_bytes = 4096
}
25 changes: 25 additions & 0 deletions examples/mig/stateful/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "self_link" {
description = "Self-link of the managed instance group"
value = module.mig.self_link
}

output "region" {
description = "The GCP region to create and test resources in"
value = var.region
}
46 changes: 46 additions & 0 deletions examples/mig/stateful/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/



variable "project_id" {
description = "The GCP project to use for integration tests"
type = string
}

variable "region" {
description = "The GCP region to create and test resources in"
type = string
default = "us-central1"
}

variable "subnetwork" {
description = "The subnetwork to host the compute instances in"
}

variable "target_size" {
description = "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."
}

variable "service_account" {
default = null
type = object({
email = string
scopes = set(string)
})
description = "Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template#service_account."
}

29 changes: 29 additions & 0 deletions examples/mig/stateful/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

terraform {
required_version = ">= 0.13"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.0"
}
google-beta = {
source = "hashicorp/google-beta"
version = "~> 4.0"
}
}
}
2 changes: 2 additions & 0 deletions modules/mig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ The current version is 2.X. The following guides are available to assist with up
| region | The GCP region where the managed instance group resides. | `string` | n/a | yes |
| scaling\_schedules | Autoscaling, scaling schedule block. https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_autoscaler#scaling_schedules | <pre>list(object({<br> disabled = bool<br> duration_sec = number<br> min_required_replicas = number<br> name = string<br> schedule = string<br> time_zone = string<br> }))</pre> | `[]` | no |
| 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 | <pre>list(object({<br> device_name = string<br> delete_rule = string<br> }))</pre> | `[]` | no |
| stateful\_external\_ips | External 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> network_interface_name = string<br> delete_rule = string<br> }))</pre> | `[]` | no |
| stateful\_internal\_ips | Internal 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> network_interface_name = string<br> delete_rule = string<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 |
Expand Down
16 changes: 16 additions & 0 deletions modules/mig/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ resource "google_compute_region_instance_group_manager" "mig" {
}
}

dynamic "stateful_internal_ip" {
for_each = var.stateful_internal_ip
content {
network_interface_name = stateful_internal_ip.value.network_interface_name
delete_rule = lookup(stateful_internal_ip.value, "delete_rule", null)
}
}

dynamic "stateful_external_ip" {
for_each = var.stateful_external_ip
content {
network_interface_name = stateful_external_ip.value.network_interface_name
delete_rule = lookup(stateful_external_ip.value, "delete_rule", null)
}
}

distribution_policy_zones = local.distribution_policy_zones
dynamic "update_policy" {
for_each = var.update_policy
Expand Down
21 changes: 21 additions & 0 deletions modules/mig/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ variable "stateful_disks" {
default = []
}

#################
# Stateful IPs
#################
variable "stateful_internal_ips" {
description = "Internal 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"
type = list(object({
network_interface_name = string
delete_rule = string
}))
default = []
}

variable "stateful_external_ips" {
description = "External 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"
type = list(object({
network_interface_name = string
delete_rule = string
}))
default = []
}

#################
# Rolling Update
#################
Expand Down
2 changes: 2 additions & 0 deletions modules/mig_with_percent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ The current version is 2.X. The following guides are available to assist with up
| region | The GCP region where the managed instance group resides. | `string` | n/a | yes |
| scaling\_schedules | Autoscaling, scaling schedule block. https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_autoscaler#scaling_schedules | <pre>list(object({<br> disabled = bool<br> duration_sec = number<br> min_required_replicas = number<br> name = string<br> schedule = string<br> time_zone = string<br> }))</pre> | `[]` | no |
| 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 | <pre>list(object({<br> device_name = string<br> delete_rule = string<br> }))</pre> | `[]` | no |
| stateful\_external\_ips | External 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> network_interface_name = string<br> delete_rule = string<br> }))</pre> | `[]` | no |
| stateful\_internal\_ips | Internal 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> network_interface_name = string<br> delete_rule = string<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 |
Expand Down
16 changes: 16 additions & 0 deletions modules/mig_with_percent/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ resource "google_compute_region_instance_group_manager" "mig_with_percent" {
}
}

dynamic "stateful_internal_ip" {
for_each = var.stateful_internal_ip
content {
network_interface_name = stateful_internal_ip.value.network_interface_name
delete_rule = lookup(stateful_internal_ip.value, "delete_rule", null)
}
}

dynamic "stateful_external_ip" {
for_each = var.stateful_external_ip
content {
network_interface_name = stateful_external_ip.value.network_interface_name
delete_rule = lookup(stateful_external_ip.value, "delete_rule", null)
}
}

distribution_policy_zones = local.distribution_policy_zones
dynamic "update_policy" {
for_each = var.update_policy
Expand Down
21 changes: 21 additions & 0 deletions modules/mig_with_percent/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ variable "stateful_disks" {
default = []
}

#################
# Stateful IPs
#################
variable "stateful_internal_ips" {
description = "Internal 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"
type = list(object({
network_interface_name = string
delete_rule = string
}))
default = []
}

variable "stateful_external_ips" {
description = "External 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"
type = list(object({
network_interface_name = string
delete_rule = string
}))
default = []
}

#################
# Rolling Update
#################
Expand Down
Loading

0 comments on commit 9fba0ff

Please sign in to comment.