Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement the mig_with_percent submodule #19

Merged
merged 5 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ suites:
name: terraform
command_timeout: 1800
root_module_directory: test/fixtures/umig/static_ips
- name: mig_with_percent_simple
driver:
name: terraform
command_timeout: 1800
root_module_directory: test/fixtures/mig_with_percent/simple
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Added

- `preemptible_and_regular_instance_templates` submodule. [#18]
- `mig_with_percent` submodule. [#18]

## [1.0.0] - 2019-07-31

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

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

[^]: (autogen_docs_start)

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| credentials\_path | The path to the GCP credentials JSON file | string | n/a | yes |
| 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 | n/a | yes |
| service\_account | Service account email address and scopes | map | n/a | yes |
| subnetwork | The subnetwork to host the compute instances in | string | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| self\_link | Self-link of the managed instance group |

[^]: (autogen_docs_end)
45 changes: 45 additions & 0 deletions examples/mig_with_percent/simple/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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" {
credentials = file(var.credentials_path)
project = var.project_id
region = var.region
version = "~> 2.11"
}

provider "google-beta" {
credentials = file(var.credentials_path)
project = var.project_id
region = var.region
version = "~> 2.11"
}

module "preemptible_and_regular_instance_templates" {
source = "../../../modules/preemptible_and_regular_instance_templates"
subnetwork = var.subnetwork
service_account = var.service_account
}

module "mig_with_percent" {
source = "../../../modules/mig_with_percent"
region = var.region
target_size = 4
hostname = "mig-with-percent-simple"
instance_template_initial_version = module.preemptible_and_regular_instance_templates.regular_self_link
instance_template_next_version = module.preemptible_and_regular_instance_templates.preemptible_self_link
next_version_percent = 50
}
20 changes: 20 additions & 0 deletions examples/mig_with_percent/simple/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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_with_percent.self_link
}
40 changes: 40 additions & 0 deletions examples/mig_with_percent/simple/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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.
*/

variable "credentials_path" {
description = "The path to the GCP credentials JSON file"
}

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

variable "region" {
description = "The GCP region to create and test resources in"
}

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

variable "service_account" {
default = null
type = object({
email = string
scopes = set(string)
})
description = "Service account email address and scopes"
}
19 changes: 19 additions & 0 deletions examples/mig_with_percent/simple/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.12"
}
52 changes: 52 additions & 0 deletions modules/mig_with_percent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Managed Instance Group (MIG) with percent

This module allows you to set the percentage ratio of second version of instance template on Managed Instance Group.

## Usage

See the [simple example](../../examples/mig_with_percent/simple) for a usage example.

[^]: (autogen_docs_start)
morgante marked this conversation as resolved.
Show resolved Hide resolved

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| autoscaling\_cpu | Autoscaling, cpu utilization policy block as single element array. https://www.terraform.io/docs/providers/google/r/compute\_autoscaler.html#cpu\_utilization | list | `<list>` | no |
| autoscaling\_enabled | Creates an autoscaler for the managed instance group | string | `"false"` | no |
| 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 | `<list>` | no |
| autoscaling\_metric | Autoscaling, metric policy block as single element array. https://www.terraform.io/docs/providers/google/r/compute\_autoscaler.html#metric | list | `<list>` | no |
| cooldown\_period | The number of seconds that the autoscaler should wait before it starts collecting information from a new instance. | string | `"60"` | no |
| distribution\_policy\_zones | The distribution policy, i.e. which zone\(s\) should instances be create in. Default is all zones in given region. | list | `<list>` | no |
| hc\_healthy\_threshold | Health check healthy threshold. | string | `"1"` | no |
| hc\_initial\_delay\_sec | Health check, intial delay in seconds. | string | `"30"` | no |
| hc\_interval\_sec | Health check interval in seconds. | string | `"30"` | no |
| hc\_path | Health check http path to check. | string | `"/"` | no |
| hc\_port | Health check port. | string | `""` | no |
| hc\_timeout\_sec | Health check timeout in seconds. | string | `"10"` | no |
| hc\_unhealthy\_threshold | Health check unhealthy threshold. | string | `"5"` | no |
| hostname | Hostname prefix for instances | string | `"default"` | no |
| http\_healthcheck\_enable | Enable HTTP healthcheck | string | `"false"` | no |
| 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 |
| 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 | list | `<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 |
| region | The GCP region where the managed instance group resides. | string | n/a | yes |
| 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 | `<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 |
| tcp\_healthcheck\_enable | Enable TCP healthcheck | string | `"false"` | no |
| update\_policy | The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute\_region\_instance\_group\_manager.html#rolling\_update\_policy | list | `<list>` | no |

## Outputs

| Name | Description |
|------|-------------|
| instance\_group | Instance-group url of managed instance group |
| self\_link | Self-link of managed instance group |

[^]: (autogen_docs_end)
151 changes: 151 additions & 0 deletions modules/mig_with_percent/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* 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.
*/

locals {
healthchecks = concat(
google_compute_health_check.http_healthcheck.*.self_link,
google_compute_health_check.tcp_healthcheck.*.self_link,
)
distribution_policy_zones_base = {
default = data.google_compute_zones.available.names
user = var.distribution_policy_zones
}
distribution_policy_zones = local.distribution_policy_zones_base[length(var.distribution_policy_zones) == 0 ? "default" : "user"]
}

data "google_compute_zones" "available" {
region = var.region
}

resource "google_compute_region_instance_group_manager" "mig_with_percent" {
provider = google-beta
base_instance_name = var.hostname

version {
name = "${var.hostname}-mig-version-0"
instance_template = var.instance_template_initial_version
}

version {
name = "${var.hostname}-mig-version-1"
instance_template = var.instance_template_next_version

target_size {
percent = var.next_version_percent
}
}

name = "${var.hostname}-mig-with-percent"
region = var.region
dynamic "named_port" {
for_each = var.named_ports
content {
name = lookup(named_port.value, "name", null)
port = lookup(named_port.value, "port", null)
}
}
target_pools = var.target_pools
target_size = var.autoscaling_enabled ? var.min_replicas : var.target_size

auto_healing_policies {
health_check = length(local.healthchecks) > 0 ? local.healthchecks[0] : ""
initial_delay_sec = length(local.healthchecks) > 0 ? var.hc_initial_delay_sec : 0
}

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
}
}

lifecycle {
create_before_destroy = "true"
}
}

resource "google_compute_region_autoscaler" "autoscaler" {
provider = google
count = var.autoscaling_enabled ? 1 : 0
name = "${var.hostname}-autoscaler"
target = google_compute_region_instance_group_manager.mig_with_percent.self_link

autoscaling_policy {
max_replicas = var.max_replicas
min_replicas = var.min_replicas
cooldown_period = var.cooldown_period
dynamic "cpu_utilization" {
for_each = var.autoscaling_cpu
content {
target = lookup(cpu_utilization.value, "target", null)
}
}
dynamic "metric" {
for_each = var.autoscaling_metric
content {
name = lookup(metric.value, "name", null)
target = lookup(metric.value, "target", null)
type = lookup(metric.value, "type", null)
}
}
dynamic "load_balancing_utilization" {
for_each = var.autoscaling_lb
content {
target = lookup(load_balancing_utilization.value, "target", null)
}
}
}

depends_on = ["google_compute_region_instance_group_manager.mig_with_percent"]
}

resource "google_compute_health_check" "http_healthcheck" {
provider = google
count = var.http_healthcheck_enable ? 1 : 0
name = "${var.hostname}-http-healthcheck"

check_interval_sec = var.hc_interval_sec
timeout_sec = var.hc_timeout_sec
healthy_threshold = var.hc_healthy_threshold
unhealthy_threshold = var.hc_unhealthy_threshold

http_health_check {
request_path = var.hc_path
port = var.hc_port
}
}

resource "google_compute_health_check" "tcp_healthcheck" {
provider = google
count = var.tcp_healthcheck_enable ? 1 : 0
name = "${var.hostname}-tcp-healthcheck"

check_interval_sec = var.hc_interval_sec
timeout_sec = var.hc_timeout_sec
healthy_threshold = var.hc_healthy_threshold
unhealthy_threshold = var.hc_unhealthy_threshold

tcp_health_check {
port = var.hc_port
}
}
Loading