Skip to content

Commit

Permalink
Merge pull request #50 from SKozlovsky/feature/add-modules-autogen
Browse files Browse the repository at this point in the history
Add autogen for `mig` and `mig_with_percent` modules
  • Loading branch information
aaron-lane committed Dec 18, 2019
2 parents 53851f0 + 5c2f0e8 commit cc9ee9c
Show file tree
Hide file tree
Showing 17 changed files with 812 additions and 16 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ docker_generate_docs:
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && generate_docs'

# Generate files from autogen
.PHONY: docker_generate
docker_generate:
docker run --rm -it \
-v "$(CURDIR)":/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && generate'

# Alias for backwards compatibility
.PHONY: generate_docs
generate_docs: docker_generate_docs

.PHONY: generate
generate: docker_generate
19 changes: 19 additions & 0 deletions autogen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% if mig %}
# Managed Instance Group (MIG)

This module is used to create a [google_compute_region_instance_group_manager](https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager.html),
and optionally, an autoscaler and healthchecks.

{% else %}
# 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.

{% endif %}
## Usage

See the [simple example](../../examples/{{ module_name }}/simple) for a usage example.

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

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
173 changes: 173 additions & 0 deletions autogen/main.tf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/**
* 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.
*/

{{ autogeneration_note }}

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" {
{% if mig %}
project = var.project_id
{% endif %}
region = var.region
}

resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
provider = google-beta
base_instance_name = var.hostname
project = var.project_id

{% if mig %}
version {
name = "${var.hostname}-mig-version-0"
instance_template = var.instance_template
}
{% else %}
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
}
}
{% endif %}

name = "${var.hostname}-{{ module_name_hr }}"
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
ignore_changes = [distribution_policy_zones]
}
}

resource "google_compute_region_autoscaler" "autoscaler" {
provider = google
count = var.autoscaling_enabled ? 1 : 0
name = "${var.hostname}-autoscaler"
project = var.project_id
{% if mig %}
region = var.region
{% endif %}
target = google_compute_region_instance_group_manager.{{ module_name }}.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)
}
}
}
{% if mig_with_percent %}

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

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

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
project = var.project_id
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
}
}
27 changes: 27 additions & 0 deletions autogen/outputs.tf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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.
*/

{{ autogeneration_note }}

output "self_link" {
description = "Self-link of managed instance group"
value = google_compute_region_instance_group_manager.{{ module_name }}.self_link
}

output "instance_group" {
description = "Instance-group url of managed instance group"
value = google_compute_region_instance_group_manager.{{ module_name }}.instance_group
}
Loading

0 comments on commit cc9ee9c

Please sign in to comment.