Skip to content

Commit 4e5fc40

Browse files
committed
[Part 1/3] Upgrade terraform from 0.11.14 to 1.2.5
Why: - terraform rotated their signing [certificate](https://discuss.hashicorp.com/t/terraform-updates-for-hcsec-2021-12/23570). As a result `terraform init` fails. Instead, a user will need to use `terraform init -verify-plugins=false` if they want to maintain using 0.11.14. This is not safe. Version 0.11.15 has the new certificate. But the other reasons prevented from using that. - The providers versions are not pinned. As a result, it pulls the latest that it can. Not sure if it was the one used initially. - Terraform 0.11.x is end of life [1](https://discuss.hashicorp.com/t/when-specifically-is-terraform-0-11-deprecated/3996/4) [2](https://www.hashicorp.com/blog/deprecating-terraform-0-11-support-in-terraform-providers) - Resources used previously were in the google-beta provider. Now they are in the google stable provider. Changes: - Add .terraform.lock.hcl. This file is a lock file for terraform providers that this repo depends on. Only available in newer versions of terraform - Syntax changes. Version 0.12 and onwards has a different syntax than 0.11. Example: - `ports = "${local.forwarded_ports}` -> `ports = local.forwarded_ports` - These changes occured automatically leveraging [terraform 0.12upgrade](https://www.terraform.io/language/upgrade-guides/0-12#upgrading-terraform-configuration) - various `versions.tf` files. These were generated when running [terraform 0.13upgrade](https://www.terraform.io/language/upgrade-guides/0-13#explicit-provider-source-locations) - compute.tf: This file was added by me. Previously, this repo used two external modules: github.com/dcaba/terraform-google-managed-instance-group and github.com/ecosystem-infra/terraform-google-multi-port-managed-instance-group. However, these modules are not compatible with the new versions of terraform. One repo is archived and the other has not been touched in awhile. Those modules are using beta features which are now in the main google provider now. We can now just use the out of the box Google provider to build the same infrastructure. - Upgrade the terraform-google-modules/container-vm/google module reference to latest. - Removed references to the `google-beta` provider since it does not exist anymore - Added placeholder "cos_image_name" which pins the OS image currently used. Otherwise, it will pick the latest and cause a difference to be detected. This will be removed in Part 3. How were these changes tested: Along with the changes in Part 2/3, running `terraform plan` yielded no changes in the infrastructure after the terraform upgrade Outstanding changes: - terraform.tfstate has changed the format over the versions. In order to make this PR readable, I separated that change into Part 2/3
1 parent eb0bfe0 commit 4e5fc40

File tree

14 files changed

+500
-211
lines changed

14 files changed

+500
-211
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ jobs:
2121
- name: Set up Terraform
2222
uses: hashicorp/setup-terraform@v1
2323
with:
24-
terraform_version: '0.11.14'
24+
terraform_version: '1.2.5'
2525
- name: terraform
2626
run: terraform fmt --check

.terraform.lock.hcl

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Requirements:
139139

140140
- [Python 3](https://python.org)
141141
- [Pipenv](https://pipenv.pypa.io/)
142-
- [Terraform](https://www.terraform.io/) version 0.11.14
142+
- [Terraform](https://www.terraform.io/) version 1.2.5
143143

144144
The following commands will run the lints:
145145

@@ -153,7 +153,7 @@ Requirements:
153153

154154
- [Docker](https://www.docker.com/)
155155
- [GNU Make](https://www.gnu.org/software/make/)
156-
- [Terraform](https://www.terraform.io/) version 0.11.14
156+
- [Terraform](https://www.terraform.io/) version 1.2.5
157157
- [Python 3](https://python.org)
158158
- access credentials to the Google Cloud Platform project, saved to a file named
159159
`google-cloud-platform-credentials.json` in the root pf this repository

infrastructure/docker-image/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
variable "registry" {
22
description = "Host name of the Docker registry from which the image identifier should be retirieved"
3-
type = "string"
3+
type = string
44
}
55

66
variable "image" {
77
description = "Name of the Docker image whose identifier should be retrieved"
8-
type = "string"
8+
type = string
99
}
1010

1111
output "identifier" {
@@ -17,8 +17,8 @@ data "external" "image" {
1717
"python3",
1818
"${path.module}/latest-image.py",
1919
"--registry",
20-
"${var.registry}",
20+
var.registry,
2121
"--image",
22-
"${var.image}",
22+
var.image,
2323
]
2424
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
terraform {
3+
required_version = "~> 1.2.5"
4+
required_providers {
5+
external = {
6+
source = "hashicorp/external"
7+
}
8+
}
9+
}
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
# Contains the configurations for the Compute Engine section of Google Cloud
2+
# These configurations come from modules that are now archived:
3+
# - Cert Renewer used: github.com/dcaba/terraform-google-managed-instance-group
4+
# - WPT Server used: github.com/ecosystem-infra/terraform-google-multi-port-managed-instance-group
5+
# Most hardcoded defaults come from those aforementioned modules.
6+
7+
########################################
8+
# WPT Server
9+
# These configurations come from: github.com/ecosystem-infra/terraform-google-multi-port-managed-instance-group
10+
# More information about how it was used previously: https://github.com/web-platform-tests/wpt.live/blob/67dc5976ccce2e64483f2028a35659d4d6e58891/infrastructure/web-platform-tests/main.tf#L69-L137
11+
########################################
12+
13+
resource "google_compute_health_check" "wpt_health_check" {
14+
name = "${var.name}-wpt-servers"
15+
16+
check_interval_sec = 10
17+
timeout_sec = 10
18+
healthy_threshold = 3
19+
unhealthy_threshold = 6
20+
21+
https_health_check {
22+
port = "443"
23+
# A query parameter is used to distinguish the health check in the server's
24+
# request logs.
25+
request_path = "/?gcp-health-check"
26+
}
27+
}
28+
29+
resource "google_compute_instance_group_manager" "wpt_servers" {
30+
name = "${var.name}-wpt-servers"
31+
zone = var.zone
32+
description = "compute VM Instance Group"
33+
wait_for_instances = false
34+
base_instance_name = "${var.name}-wpt-servers"
35+
version {
36+
name = "${var.name}-wpt-servers-default"
37+
instance_template = google_compute_instance_template.wpt_server.self_link
38+
}
39+
update_policy {
40+
type = local.update_policy.type
41+
minimal_action = local.update_policy.minimal_action
42+
max_unavailable_fixed = local.update_policy.max_unavailable_fixed
43+
}
44+
target_pools = [google_compute_target_pool.default.self_link]
45+
target_size = 2
46+
47+
dynamic "named_port" {
48+
for_each = var.wpt_server_ports
49+
content {
50+
name = named_port.value["name"]
51+
port = named_port.value["port"]
52+
}
53+
}
54+
55+
auto_healing_policies {
56+
health_check = google_compute_health_check.wpt_health_check.self_link
57+
initial_delay_sec = 30
58+
}
59+
}
60+
61+
resource "google_compute_firewall" "wpt-server-mig-health-check" {
62+
name = "${var.name}-wpt-servers-vm-hc"
63+
network = var.network_name
64+
65+
allow {
66+
protocol = "tcp"
67+
# https port
68+
ports = [var.wpt_server_ports[2].port]
69+
}
70+
71+
# This range comes from this module that was used previously:
72+
# https://github.com/Ecosystem-Infra/terraform-google-multi-port-managed-instance-group/blob/master/main.tf#L347
73+
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"]
74+
target_tags = ["${var.name}-allow"]
75+
}
76+
77+
resource "google_compute_firewall" "wpt-servers-default-ssh" {
78+
name = "${var.name}-wpt-servers-vm-ssh"
79+
network = var.network_name
80+
81+
allow {
82+
protocol = "tcp"
83+
ports = ["22"]
84+
}
85+
86+
source_ranges = ["0.0.0.0/0"]
87+
target_tags = ["allow-ssh"]
88+
}
89+
90+
resource "google_compute_instance_template" "wpt_server" {
91+
name_prefix = "default-"
92+
93+
tags = ["allow-ssh", "${var.name}-allow"]
94+
95+
# As of 2020-06-17, we were running into OOM issues with the 1.7 GB
96+
# "g1-small" instance[1]. This was suspected to be due to 'git gc' needing
97+
# more memory, so we upgraded to "e2-medium" (4 GB of RAM).
98+
#
99+
# [1] https://github.com/web-platform-tests/wpt.live/issues/30
100+
machine_type = "e2-medium"
101+
102+
# The "google-logging-enabled" metadata is undocumented, but it is apparently
103+
# necessary to enable the capture of logs from the Docker image.
104+
#
105+
# https://github.com/GoogleCloudPlatform/konlet/issues/56
106+
labels = {
107+
"${module.wpt-server-container.vm_container_label_key}" = module.wpt-server-container.vm_container_label
108+
}
109+
110+
network_interface {
111+
network = var.network_name
112+
subnetwork = var.subnetwork_name
113+
access_config {
114+
network_tier = "PREMIUM"
115+
}
116+
}
117+
118+
can_ip_forward = false
119+
120+
// Create a new boot disk from an image
121+
disk {
122+
auto_delete = true
123+
boot = true
124+
source_image = module.wpt-server-container.source_image
125+
type = "PERSISTENT"
126+
disk_type = "pd-ssd"
127+
disk_size_gb = var.wpt_server_disk_size
128+
mode = "READ_WRITE"
129+
}
130+
131+
service_account {
132+
email = "default"
133+
scopes = ["storage-ro", "logging-write"]
134+
}
135+
136+
scheduling {
137+
automatic_restart = true
138+
on_host_maintenance = "MIGRATE"
139+
}
140+
141+
# startup-script and tf_depends_id comes from the module previously used for wpt-server. (see link at top)
142+
# TODO: evaluate if those two should be removed.
143+
metadata = {
144+
# "${module.wpt-server-container.metadata_key}" = module.wpt-server-container.metadata_value
145+
# The value for ${module.wpt-server-container.metadata_key} is temporary. During the upgrade, the metadata rendering changes.
146+
# More info: https://github.com/terraform-google-modules/terraform-google-container-vm/blob/master/docs/upgrading_to_v2.0.md
147+
# Clarification to the linked docs, metadata changes will destroy the old template and create a new one.
148+
# In order to make this as smooth as possible, we will hardcode this.
149+
# When ready, remove this temporary metadata and the one on cert-renewer. And uncomment the line above.
150+
"${module.wpt-server-container.metadata_key}" = <<-EOT
151+
---
152+
spec:
153+
containers:
154+
- env:
155+
- name: WPT_HOST
156+
value: wpt.live
157+
- name: WPT_ALT_HOST
158+
value: not-wpt.live
159+
- name: WPT_BUCKET
160+
value: wpt-tot-certificates
161+
image: gcr.io/wpt-live/wpt-live-wpt-server-tot@sha256:5d7a3d7a5ca0ba4ca7f6e56ad62aa6342c9ab92d41eea24cc6ce4a9b1e2a6afe
162+
restartPolicy: Always
163+
volumes: []
164+
EOT
165+
"startup-script" = ""
166+
"tf_depends_id" = ""
167+
"google-logging-enabled" = "true"
168+
}
169+
170+
lifecycle {
171+
create_before_destroy = true
172+
}
173+
}
174+
175+
########################################
176+
# Cert Renewers
177+
# These configurations come from: github.com/dcaba/terraform-google-managed-instance-group
178+
# More information about how it was used previously: https://github.com/web-platform-tests/wpt.live/blob/67dc5976ccce2e64483f2028a35659d4d6e58891/infrastructure/web-platform-tests/main.tf#L139-L178
179+
########################################
180+
181+
resource "google_compute_instance_template" "cert_renewers" {
182+
name_prefix = "default-"
183+
184+
machine_type = "f1-micro"
185+
186+
region = var.region
187+
188+
tags = ["allow-ssh", "${var.name}-allow"]
189+
190+
labels = {
191+
"${module.cert-renewer-container.vm_container_label_key}" = module.cert-renewer-container.vm_container_label
192+
}
193+
194+
network_interface {
195+
network = var.network_name
196+
subnetwork = var.subnetwork_name
197+
network_ip = ""
198+
access_config {
199+
network_tier = "PREMIUM"
200+
}
201+
}
202+
203+
can_ip_forward = false
204+
205+
disk {
206+
auto_delete = true
207+
boot = true
208+
source_image = module.cert-renewer-container.source_image
209+
type = "PERSISTENT"
210+
disk_type = "pd-ssd"
211+
mode = "READ_WRITE"
212+
}
213+
214+
service_account {
215+
email = "default"
216+
scopes = ["cloud-platform"]
217+
}
218+
219+
# startup-script and tf_depends_id comes from the module previously used for cert renewer. (see link at top)
220+
# TODO: evaluate if those two should be removed.
221+
metadata = {
222+
# "${module.cert-renewer-container.metadata_key}" = module.cert-renewer-container.metadata_value
223+
# The value for ${module.cert-renewer-container.metadata_key} is temporary. During the upgrade, the metadata rendering changes.
224+
# More info: https://github.com/terraform-google-modules/terraform-google-container-vm/blob/master/docs/upgrading_to_v2.0.md
225+
# Clarification to the linked docs, metadata changes will destroy the old template and create a new one.
226+
# In order to make this as smooth as possible, we will hardcode this.
227+
# When ready, remove this temporary metadata and the one on wpt-server. And uncomment the line above.
228+
"${module.cert-renewer-container.metadata_key}" = <<-EOT
229+
---
230+
spec:
231+
containers:
232+
- env:
233+
- name: WPT_HOST
234+
value: wpt.live
235+
- name: WPT_ALT_HOST
236+
value: not-wpt.live
237+
- name: WPT_BUCKET
238+
value: wpt-tot-certificates
239+
image: gcr.io/wpt-live/wpt-live-cert-renewer@sha256:5b3c0a3a2b0d7e2a0e1c0303874d09bb3214aa93dec55ac245cf1c81e7d117d5
240+
restartPolicy: Always
241+
volumes: []
242+
EOT
243+
"startup-script" = ""
244+
"tf_depends_id" = ""
245+
"google-logging-enabled" = "true"
246+
}
247+
248+
scheduling {
249+
preemptible = false
250+
automatic_restart = true
251+
on_host_maintenance = "MIGRATE"
252+
}
253+
254+
lifecycle {
255+
create_before_destroy = true
256+
}
257+
}
258+
259+
resource "google_compute_instance_group_manager" "cert_renewers" {
260+
name = "${var.name}-cert-renewers"
261+
description = "compute VM Instance Group"
262+
wait_for_instances = false
263+
264+
base_instance_name = "${var.name}-cert-renewers"
265+
266+
version {
267+
instance_template = google_compute_instance_template.cert_renewers.self_link
268+
}
269+
270+
zone = var.zone
271+
272+
update_policy {
273+
# The type is different from wpt servers's update policy.
274+
# TODO: Evaluate why
275+
type = "OPPORTUNISTIC"
276+
minimal_action = local.update_policy.minimal_action
277+
max_unavailable_fixed = local.update_policy.max_unavailable_fixed
278+
}
279+
280+
target_pools = []
281+
282+
target_size = 1
283+
284+
dynamic "named_port" {
285+
for_each = var.cert_renewer_ports
286+
content {
287+
name = named_port.value["name"]
288+
port = named_port.value["port"]
289+
}
290+
}
291+
}

0 commit comments

Comments
 (0)