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