Skip to content

Commit 5ca905b

Browse files
authored
feat: Add HTTPS health check support and use empty variables to indicate default names (#170)
1 parent 9e930ce commit 5ca905b

File tree

20 files changed

+587
-20
lines changed

20 files changed

+587
-20
lines changed

.kitchen.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ suites:
7474
name: terraform
7575
command_timeout: 1800
7676
root_module_directory: test/fixtures/mig/autoscaler
77+
- name: mig_healthcheck
78+
lifecycle:
79+
pre_verify:
80+
# Wait for instances to be created
81+
- local: sleep 300
82+
driver:
83+
name: terraform
84+
command_timeout: 1800
85+
root_module_directory: test/fixtures/mig/healthcheck
7786
- name: umig_simple
7887
driver:
7988
name: terraform

autogen/main.tf.tmpl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
locals {
2020
healthchecks = concat(
21+
google_compute_health_check.https.*.self_link,
2122
google_compute_health_check.http.*.self_link,
2223
google_compute_health_check.tcp.*.self_link,
2324
)
@@ -60,7 +61,7 @@ resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
6061
}
6162
{% endif %}
6263

63-
name = var.mig_name == "default" ? "${var.hostname}-{{ module_name_hr }}" : var.mig_name
64+
name = var.mig_name == "" ? "${var.hostname}-{{ module_name_hr }}" : var.mig_name
6465
region = var.region
6566
dynamic "named_port" {
6667
for_each = var.named_ports
@@ -120,7 +121,7 @@ resource "google_compute_region_instance_group_manager" "{{ module_name }}" {
120121
resource "google_compute_region_autoscaler" "autoscaler" {
121122
provider = google
122123
count = var.autoscaling_enabled ? 1 : 0
123-
name = "${var.hostname}-autoscaler"
124+
name = var.autoscaler_name == "" ? "${var.hostname}-autoscaler" : var.autoscaler_name
124125
project = var.project_id
125126
region = var.region
126127

@@ -167,10 +168,29 @@ resource "google_compute_region_autoscaler" "autoscaler" {
167168
{% endif %}
168169
}
169170

171+
resource "google_compute_health_check" "https" {
172+
count = var.health_check["type"] == "https" ? 1 : 0
173+
project = var.project_id
174+
name = var.health_check_name == "" ? "${var.hostname}-https-healthcheck" : var.health_check_name
175+
176+
check_interval_sec = var.health_check["check_interval_sec"]
177+
healthy_threshold = var.health_check["healthy_threshold"]
178+
timeout_sec = var.health_check["timeout_sec"]
179+
unhealthy_threshold = var.health_check["unhealthy_threshold"]
180+
181+
https_health_check {
182+
port = var.health_check["port"]
183+
request_path = var.health_check["request_path"]
184+
host = var.health_check["host"]
185+
response = var.health_check["response"]
186+
proxy_header = var.health_check["proxy_header"]
187+
}
188+
}
189+
170190
resource "google_compute_health_check" "http" {
171191
count = var.health_check["type"] == "http" ? 1 : 0
172192
project = var.project_id
173-
name = "${var.hostname}-http-healthcheck"
193+
name = var.health_check_name == "" ? "${var.hostname}-http-healthcheck" : var.health_check_name
174194

175195
check_interval_sec = var.health_check["check_interval_sec"]
176196
healthy_threshold = var.health_check["healthy_threshold"]
@@ -189,7 +209,7 @@ resource "google_compute_health_check" "http" {
189209
resource "google_compute_health_check" "tcp" {
190210
count = var.health_check["type"] == "tcp" ? 1 : 0
191211
project = var.project_id
192-
name = "${var.hostname}-tcp-healthcheck"
212+
name = var.health_check_name == "" ? "${var.hostname}-tcp-healthcheck" : var.health_check_name
193213

194214
timeout_sec = var.health_check["timeout_sec"]
195215
check_interval_sec = var.health_check["check_interval_sec"]

autogen/variables.tf.tmpl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ variable "hostname" {
2828
}
2929

3030
variable "mig_name" {
31-
description = "Managed instance group name. When set to `default`, name will be derived from var.hostname."
32-
default = "default"
31+
type = string
32+
description = "Managed instance group name. When variable is empty, name will be derived from var.hostname."
33+
default = ""
3334
}
3435

3536
variable "region" {
@@ -107,6 +108,12 @@ variable "update_policy" {
107108
# Healthcheck
108109
##############
109110

111+
variable "health_check_name" {
112+
type = string
113+
description = "Health check name. When variable is empty, name will be derived from var.hostname."
114+
default = ""
115+
}
116+
110117
variable "health_check" {
111118
description = "Health check to determine whether instances are responsive and able to do work"
112119
type = object({
@@ -142,6 +149,12 @@ variable "health_check" {
142149
#############
143150
# Autoscaler
144151
#############
152+
variable "autoscaler_name" {
153+
type = string
154+
description = "Autoscaler name. When variable is empty, name will be derived from var.hostname."
155+
default = ""
156+
}
157+
145158
variable "autoscaling_enabled" {
146159
description = "Creates an autoscaler for the managed instance group"
147160
default = "false"

examples/mig/healthcheck/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# vm-autoscaler
2+
3+
This is an example of how to use the MIG module to create a managed instance
4+
group with an autoscaler.
5+
6+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
7+
## Inputs
8+
9+
| Name | Description | Type | Default | Required |
10+
|------|-------------|------|---------|:--------:|
11+
| project\_id | The GCP project to use for integration tests | `string` | n/a | yes |
12+
| region | The GCP region to create and test resources in | `string` | `"us-central1"` | no |
13+
14+
## Outputs
15+
16+
| Name | Description |
17+
|------|-------------|
18+
| instance\_template\_self\_link | Self-link of instance template |
19+
| mig\_self\_link | Self-link for managed instance group |
20+
| region | The GCP region to create and test resources in |
21+
22+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/mig/healthcheck/main.tf

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
/** Providers **/
18+
19+
provider "google" {
20+
project = var.project_id
21+
region = var.region
22+
version = "~>3.0"
23+
}
24+
25+
provider "google-beta" {
26+
project = var.project_id
27+
region = var.region
28+
version = "~> 3.0"
29+
}
30+
31+
/** Random String (optional) **/
32+
33+
provider "random" {
34+
version = "~> 2.2"
35+
}
36+
37+
resource "random_string" "suffix" {
38+
length = 4
39+
special = "false"
40+
upper = "false"
41+
}
42+
43+
/** Network **/
44+
45+
resource "google_compute_network" "main" {
46+
project = var.project_id
47+
name = "cft-vm-test-${random_string.suffix.result}"
48+
auto_create_subnetworks = "false"
49+
}
50+
51+
resource "google_compute_subnetwork" "main" {
52+
project = var.project_id
53+
region = var.region
54+
name = "cft-vm-test-${random_string.suffix.result}"
55+
ip_cidr_range = "10.128.0.0/20"
56+
network = google_compute_network.main.self_link
57+
}
58+
59+
/** Instance Template **/
60+
61+
module "instance_template" {
62+
source = "../../../modules/instance_template"
63+
project_id = var.project_id
64+
subnetwork = google_compute_subnetwork.main.name
65+
service_account = null
66+
}
67+
68+
/** Instance Group within autoscale and health check **/
69+
70+
module "mig" {
71+
source = "../../../modules/mig"
72+
project_id = var.project_id
73+
subnetwork = google_compute_subnetwork.main.name
74+
instance_template = module.instance_template.self_link
75+
region = var.region
76+
autoscaling_enabled = "true"
77+
min_replicas = 2
78+
autoscaler_name = "mig-as"
79+
80+
autoscaling_cpu = [
81+
{
82+
target = 0.4
83+
},
84+
]
85+
86+
health_check_name = "mig-https-hc"
87+
health_check = {
88+
type = "https"
89+
initial_delay_sec = 120
90+
check_interval_sec = 5
91+
healthy_threshold = 2
92+
timeout_sec = 5
93+
unhealthy_threshold = 2
94+
response = ""
95+
proxy_header = "NONE"
96+
port = 443
97+
request = ""
98+
request_path = "/"
99+
host = "localhost"
100+
}
101+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
output "instance_template_self_link" {
18+
description = "Self-link of instance template"
19+
value = module.instance_template.self_link
20+
}
21+
22+
output "mig_self_link" {
23+
description = "Self-link for managed instance group"
24+
value = module.mig.self_link
25+
}
26+
27+
output "region" {
28+
description = "The GCP region to create and test resources in"
29+
value = var.region
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2019 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+
variable "project_id" {
18+
description = "The GCP project to use for integration tests"
19+
type = string
20+
}
21+
22+
variable "region" {
23+
description = "The GCP region to create and test resources in"
24+
type = string
25+
default = "us-central1"
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
terraform {
18+
required_version = ">=0.12.6"
19+
}

modules/mig/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The current version is 2.X. The following guides are available to assist with up
1818

1919
| Name | Description | Type | Default | Required |
2020
|------|-------------|------|---------|:--------:|
21+
| autoscaler\_name | Autoscaler name. When variable is empty, name will be derived from var.hostname. | `string` | `""` | no |
2122
| 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(map(number))` | `[]` | no |
2223
| autoscaling\_enabled | Creates an autoscaler for the managed instance group | `string` | `"false"` | no |
2324
| 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(map(number))` | `[]` | no |
@@ -26,10 +27,11 @@ The current version is 2.X. The following guides are available to assist with up
2627
| cooldown\_period | The number of seconds that the autoscaler should wait before it starts collecting information from a new instance. | `number` | `60` | no |
2728
| distribution\_policy\_zones | The distribution policy, i.e. which zone(s) should instances be create in. Default is all zones in given region. | `list(string)` | `[]` | no |
2829
| health\_check | Health check to determine whether instances are responsive and able to do work | <pre>object({<br> type = string<br> initial_delay_sec = number<br> check_interval_sec = number<br> healthy_threshold = number<br> timeout_sec = number<br> unhealthy_threshold = number<br> response = string<br> proxy_header = string<br> port = number<br> request = string<br> request_path = string<br> host = string<br> })</pre> | <pre>{<br> "check_interval_sec": 30,<br> "healthy_threshold": 1,<br> "host": "",<br> "initial_delay_sec": 30,<br> "port": 80,<br> "proxy_header": "NONE",<br> "request": "",<br> "request_path": "/",<br> "response": "",<br> "timeout_sec": 10,<br> "type": "",<br> "unhealthy_threshold": 5<br>}</pre> | no |
30+
| health\_check\_name | Health check name. When variable is empty, name will be derived from var.hostname. | `string` | `""` | no |
2931
| hostname | Hostname prefix for instances | `string` | `"default"` | no |
3032
| instance\_template | Instance template self\_link used to create compute instances | `any` | n/a | yes |
3133
| 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 |
32-
| mig\_name | Managed instance group name. When set to `default`, name will be derived from var.hostname. | `string` | `"default"` | no |
34+
| mig\_name | Managed instance group name. When variable is empty, name will be derived from var.hostname. | `string` | `""` | no |
3335
| 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 |
3436
| min\_replicas | The minimum number of replicas that the autoscaler can scale down to. This cannot be less than 0. | `number` | `2` | no |
3537
| named\_ports | Named name and named port. https://cloud.google.com/load-balancing/docs/backend-service#named_ports | <pre>list(object({<br> name = string<br> port = number<br> }))</pre> | `[]` | no |

0 commit comments

Comments
 (0)