Skip to content

Commit cc9ee9c

Browse files
authored
Merge pull request #50 from SKozlovsky/feature/add-modules-autogen
Add autogen for `mig` and `mig_with_percent` modules
2 parents 53851f0 + 5c2f0e8 commit cc9ee9c

File tree

17 files changed

+812
-16
lines changed

17 files changed

+812
-16
lines changed

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ docker_generate_docs:
8080
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
8181
/bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && generate_docs'
8282

83+
# Generate files from autogen
84+
.PHONY: docker_generate
85+
docker_generate:
86+
docker run --rm -it \
87+
-v "$(CURDIR)":/workspace \
88+
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
89+
/bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && generate'
90+
8391
# Alias for backwards compatibility
8492
.PHONY: generate_docs
8593
generate_docs: docker_generate_docs
94+
95+
.PHONY: generate
96+
generate: docker_generate

autogen/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% if mig %}
2+
# Managed Instance Group (MIG)
3+
4+
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),
5+
and optionally, an autoscaler and healthchecks.
6+
7+
{% else %}
8+
# Managed Instance Group (MIG) with percent
9+
10+
This module allows you to set the percentage ratio of second version of instance template on Managed Instance Group.
11+
12+
{% endif %}
13+
## Usage
14+
15+
See the [simple example](../../examples/{{ module_name }}/simple) for a usage example.
16+
17+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
19+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

autogen/main.tf.tmpl

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
{{ autogeneration_note }}
18+
19+
locals {
20+
healthchecks = concat(
21+
google_compute_health_check.http_healthcheck.*.self_link,
22+
google_compute_health_check.tcp_healthcheck.*.self_link,
23+
)
24+
distribution_policy_zones_base = {
25+
default = data.google_compute_zones.available.names
26+
user = var.distribution_policy_zones
27+
}
28+
distribution_policy_zones = local.distribution_policy_zones_base[length(var.distribution_policy_zones) == 0 ? "default" : "user"]
29+
}
30+
31+
data "google_compute_zones" "available" {
32+
{% if mig %}
33+
project = var.project_id
34+
{% endif %}
35+
region = var.region
36+
}
37+
38+
resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
39+
provider = google-beta
40+
base_instance_name = var.hostname
41+
project = var.project_id
42+
43+
{% if mig %}
44+
version {
45+
name = "${var.hostname}-mig-version-0"
46+
instance_template = var.instance_template
47+
}
48+
{% else %}
49+
version {
50+
name = "${var.hostname}-mig-version-0"
51+
instance_template = var.instance_template_initial_version
52+
}
53+
54+
version {
55+
name = "${var.hostname}-mig-version-1"
56+
instance_template = var.instance_template_next_version
57+
58+
target_size {
59+
percent = var.next_version_percent
60+
}
61+
}
62+
{% endif %}
63+
64+
name = "${var.hostname}-{{ module_name_hr }}"
65+
region = var.region
66+
dynamic "named_port" {
67+
for_each = var.named_ports
68+
content {
69+
name = lookup(named_port.value, "name", null)
70+
port = lookup(named_port.value, "port", null)
71+
}
72+
}
73+
target_pools = var.target_pools
74+
target_size = var.autoscaling_enabled ? var.min_replicas : var.target_size
75+
76+
auto_healing_policies {
77+
health_check = length(local.healthchecks) > 0 ? local.healthchecks[0] : ""
78+
initial_delay_sec = length(local.healthchecks) > 0 ? var.hc_initial_delay_sec : 0
79+
}
80+
81+
distribution_policy_zones = local.distribution_policy_zones
82+
dynamic "update_policy" {
83+
for_each = var.update_policy
84+
content {
85+
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
86+
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
87+
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
88+
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
89+
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
90+
minimal_action = update_policy.value.minimal_action
91+
type = update_policy.value.type
92+
}
93+
}
94+
95+
lifecycle {
96+
create_before_destroy = true
97+
ignore_changes = [distribution_policy_zones]
98+
}
99+
}
100+
101+
resource "google_compute_region_autoscaler" "autoscaler" {
102+
provider = google
103+
count = var.autoscaling_enabled ? 1 : 0
104+
name = "${var.hostname}-autoscaler"
105+
project = var.project_id
106+
{% if mig %}
107+
region = var.region
108+
{% endif %}
109+
target = google_compute_region_instance_group_manager.{{ module_name }}.self_link
110+
111+
autoscaling_policy {
112+
max_replicas = var.max_replicas
113+
min_replicas = var.min_replicas
114+
cooldown_period = var.cooldown_period
115+
dynamic "cpu_utilization" {
116+
for_each = var.autoscaling_cpu
117+
content {
118+
target = lookup(cpu_utilization.value, "target", null)
119+
}
120+
}
121+
dynamic "metric" {
122+
for_each = var.autoscaling_metric
123+
content {
124+
name = lookup(metric.value, "name", null)
125+
target = lookup(metric.value, "target", null)
126+
type = lookup(metric.value, "type", null)
127+
}
128+
}
129+
dynamic "load_balancing_utilization" {
130+
for_each = var.autoscaling_lb
131+
content {
132+
target = lookup(load_balancing_utilization.value, "target", null)
133+
}
134+
}
135+
}
136+
{% if mig_with_percent %}
137+
138+
depends_on = ["google_compute_region_instance_group_manager.mig_with_percent"]
139+
{% endif %}
140+
}
141+
142+
resource "google_compute_health_check" "http_healthcheck" {
143+
provider = google
144+
count = var.http_healthcheck_enable ? 1 : 0
145+
name = "${var.hostname}-http-healthcheck"
146+
project = var.project_id
147+
148+
check_interval_sec = var.hc_interval_sec
149+
timeout_sec = var.hc_timeout_sec
150+
healthy_threshold = var.hc_healthy_threshold
151+
unhealthy_threshold = var.hc_unhealthy_threshold
152+
153+
http_health_check {
154+
request_path = var.hc_path
155+
port = var.hc_port
156+
}
157+
}
158+
159+
resource "google_compute_health_check" "tcp_healthcheck" {
160+
provider = google
161+
count = var.tcp_healthcheck_enable ? 1 : 0
162+
project = var.project_id
163+
name = "${var.hostname}-tcp-healthcheck"
164+
165+
check_interval_sec = var.hc_interval_sec
166+
timeout_sec = var.hc_timeout_sec
167+
healthy_threshold = var.hc_healthy_threshold
168+
unhealthy_threshold = var.hc_unhealthy_threshold
169+
170+
tcp_health_check {
171+
port = var.hc_port
172+
}
173+
}

autogen/outputs.tf.tmpl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
{{ autogeneration_note }}
18+
19+
output "self_link" {
20+
description = "Self-link of managed instance group"
21+
value = google_compute_region_instance_group_manager.{{ module_name }}.self_link
22+
}
23+
24+
output "instance_group" {
25+
description = "Instance-group url of managed instance group"
26+
value = google_compute_region_instance_group_manager.{{ module_name }}.instance_group
27+
}

0 commit comments

Comments
 (0)