-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.tf
405 lines (355 loc) · 11.7 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
variable "region" {
type = string
}
variable "zone" {
type = string
}
variable "project_id" {
type = string
}
variable "github_app_id" {
type = number
}
variable "github_org" {
type = string
}
locals {
cf_env_vars = {
GCP_ZONE = var.zone
GCP_REGION = var.region
GCP_PROJECT = var.project_id
GITHUB_APP_ID = var.github_app_id
GITHUB_ORG = var.github_org
}
}
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "4.77.0"
}
}
backend "gcs" {
prefix = "terraform/state"
}
}
//////////////// Service accounts
resource "google_service_account" "vm_maintenance_scheduler" {
account_id = "vm-maintenance-scheduler"
}
resource "google_service_account" "vm_creator" {
account_id = "vm-creator"
}
// Delete the cruft
resource "google_project_default_service_accounts" "delete_default_service_accounts" {
project = var.project_id
action = "DELETE"
}
provider "google" {
project = var.project_id
}
resource "google_project_service" "enabled_apis" {
for_each = toset([
"cloudbuild.googleapis.com",
"cloudfunctions.googleapis.com",
"cloudscheduler.googleapis.com",
"compute.googleapis.com",
"run.googleapis.com",
"secretmanager.googleapis.com",
])
project = var.project_id
service = each.key
}
//////////////// Secrets
// To avoid leaking the secret contents in tfstate, the secret "version"
// must be manually deployed with "gcloud secrets add"
resource "google_secret_manager_secret" "github_webhook" {
secret_id = "caliptra-gce-ci-github-webhook-secret-txt"
replication {
automatic = true
}
depends_on = [google_project_service.enabled_apis]
}
resource "google_secret_manager_secret" "github_private_key" {
secret_id = "caliptra-gce-ci-github-private-key-pem"
replication {
automatic = true
}
depends_on = [google_project_service.enabled_apis]
}
// To avoid leaking the secret contents in tfstate, the secret "version"
// must be manually deployed
//////////////// Cloud Functions Source Code
resource "random_uuid" "cf_upload_bucket_suffix" {
}
resource "google_storage_bucket" "cf_upload_bucket" {
project = var.project_id
name = "cf-uploads-${random_uuid.cf_upload_bucket_suffix.result}"
location = var.region
force_destroy = true
uniform_bucket_level_access = true
lifecycle_rule {
condition {
age = 1
}
action {
type = "Delete"
}
}
}
data "archive_file" "cf_source_archive" {
type = "zip"
output_path = "artifacts/cf_source.zip"
source_dir = "../"
excludes = [
"deployments",
"cmd",
]
}
resource "google_storage_bucket_object" "cf_upload_object" {
name = "cf_source-${data.archive_file.cf_source_archive.output_sha256}.zip"
bucket = google_storage_bucket.cf_upload_bucket.name
source = data.archive_file.cf_source_archive.output_path
}
//////////////// Cloud Functions Deployment
// Builds a VM image for runners to use.
resource "google_cloudfunctions2_function" "runner_build_image" {
name = "runner-build-image"
location = var.region
build_config {
runtime = "go120"
entry_point = "RunnerBuildImage"
source {
storage_source {
bucket = google_storage_bucket.cf_upload_bucket.name
object = google_storage_bucket_object.cf_upload_object.name
}
}
}
service_config {
available_memory = "256M"
service_account_email = google_service_account.vm_creator.email
environment_variables = local.cf_env_vars
timeout_seconds = 1200
}
depends_on = [
google_project_service.enabled_apis,
google_project_iam_binding.project_artifactregister_reader
]
}
// Called by Github App webhook, launches runner if necessary.
resource "google_cloudfunctions2_function" "runner_launch" {
name = "runner-launch"
location = var.region
build_config {
runtime = "go120"
entry_point = "RunnerLaunch"
source {
storage_source {
bucket = google_storage_bucket.cf_upload_bucket.name
object = google_storage_bucket_object.cf_upload_object.name
}
}
}
service_config {
available_memory = "256M"
service_account_email = google_service_account.vm_creator.email
environment_variables = local.cf_env_vars
timeout_seconds = 300
secret_volumes {
mount_path = "/etc/secrets/${google_secret_manager_secret.github_webhook.secret_id}"
project_id = var.project_id
secret = google_secret_manager_secret.github_webhook.secret_id
versions {
version = "latest"
path = "latest"
}
}
secret_volumes {
mount_path = "/etc/secrets/${google_secret_manager_secret.github_private_key.secret_id}"
project_id = var.project_id
secret = google_secret_manager_secret.github_private_key.secret_id
versions {
version = "latest"
path = "latest"
}
}
}
depends_on = [
google_project_service.enabled_apis,
google_project_iam_binding.project_artifactregister_reader
]
}
// Deletes any stopped or stuck vms.
resource "google_cloudfunctions2_function" "runner_cleanup" {
name = "runner-cleanup"
location = var.region
build_config {
runtime = "go120"
entry_point = "RunnerCleanup"
source {
storage_source {
bucket = google_storage_bucket.cf_upload_bucket.name
object = google_storage_bucket_object.cf_upload_object.name
}
}
}
service_config {
available_memory = "256M"
service_account_email = google_service_account.vm_creator.email
environment_variables = local.cf_env_vars
timeout_seconds = 300
}
depends_on = [
google_project_service.enabled_apis,
google_project_iam_binding.project_artifactregister_reader
]
}
//////////////// Scheduler
resource "google_cloud_scheduler_job" "runner_build_image" {
name = "runner-build-image"
description = "Schedule the HTTPS trigger for runner-build-image cloud function"
schedule = "44 1 * * 1" # Monday at 01:44
project = google_cloudfunctions2_function.runner_build_image.project
region = google_cloudfunctions2_function.runner_build_image.location
attempt_deadline = format("%ss", google_cloudfunctions2_function.runner_build_image.service_config[0].timeout_seconds + 30)
http_target {
uri = google_cloudfunctions2_function.runner_build_image.service_config[0].uri
http_method = "POST"
oidc_token {
audience = "${google_cloudfunctions2_function.runner_build_image.service_config[0].uri}/"
service_account_email = google_service_account.vm_maintenance_scheduler.email
}
}
}
resource "google_cloud_scheduler_job" "runner_cleanup" {
name = "runner-cleanup"
description = "Schedule the HTTPS trigger for runner-cleanup cloud function"
schedule = "*/10 * * * *" # every 10 minutes
project = google_cloudfunctions2_function.runner_cleanup.project
region = google_cloudfunctions2_function.runner_cleanup.location
attempt_deadline = format("%ss", google_cloudfunctions2_function.runner_cleanup.service_config[0].timeout_seconds + 30)
http_target {
uri = google_cloudfunctions2_function.runner_cleanup.service_config[0].uri
http_method = "POST"
oidc_token {
audience = "${google_cloudfunctions2_function.runner_cleanup.service_config[0].uri}/"
service_account_email = google_service_account.vm_maintenance_scheduler.email
}
}
}
//////////////// Hardware runners
locals {
hw_runners = toset([
"kor0",
"kor1",
])
}
resource "google_pubsub_topic" "hw_runner_requests" {
name = "hw-runner-requests"
message_retention_duration = "3600s"
}
resource "google_pubsub_subscription" "hw_runner_requests" {
name = google_pubsub_topic.hw_runner_requests.name
topic = google_pubsub_topic.hw_runner_requests.name
ack_deadline_seconds = 20
retain_acked_messages = false
message_retention_duration = "3600s"
expiration_policy {
ttl = ""
}
enable_exactly_once_delivery = true
}
resource "google_service_account" "hw_runners" {
for_each = local.hw_runners
account_id = "hw-runner-${each.key}"
}
//////////////// IAM Bindings
resource "google_pubsub_topic_iam_binding" "hw_runner_requests" {
role = "roles/pubsub.publisher"
project = var.project_id
topic = google_pubsub_topic.hw_runner_requests.name
members = [
"serviceAccount:${google_service_account.vm_creator.email}",
]
}
resource "google_pubsub_subscription_iam_binding" "hw_runner_requests" {
role = "roles/pubsub.subscriber"
project = var.project_id
subscription = google_pubsub_subscription.hw_runner_requests.name
members = [for r in google_service_account.hw_runners : "serviceAccount:${r.email}"]
}
resource "google_project_iam_binding" "project_artifactregister_reader" {
project = var.project_id
role = "roles/artifactregistry.reader"
members = [
"serviceAccount:${google_service_account.vm_creator.email}",
"serviceAccount:${google_service_account.vm_maintenance_scheduler.email}",
]
}
resource "google_project_iam_binding" "project_compute_instanceadmin" {
project = var.project_id
role = "roles/compute.instanceAdmin.v1"
members = [
"serviceAccount:${google_service_account.vm_creator.email}",
]
}
resource "google_secret_manager_secret_iam_binding" "github_webhook" {
secret_id = google_secret_manager_secret.github_webhook.secret_id
role = "roles/secretmanager.secretAccessor"
members = [
"serviceAccount:${google_service_account.vm_creator.email}",
]
}
resource "google_secret_manager_secret_iam_binding" "github_private_key" {
secret_id = google_secret_manager_secret.github_private_key.secret_id
role = "roles/secretmanager.secretAccessor"
members = [
"serviceAccount:${google_service_account.vm_creator.email}",
]
}
resource "google_cloudfunctions2_function_iam_binding" "runner_cleanup_invoker" {
location = google_cloudfunctions2_function.runner_cleanup.location
cloud_function = google_cloudfunctions2_function.runner_cleanup.name
role = "roles/cloudfunctions.invoker"
members = [
"serviceAccount:${google_service_account.vm_maintenance_scheduler.email}",
]
}
resource "google_cloudfunctions2_function_iam_binding" "runner_build_image_invoker" {
location = google_cloudfunctions2_function.runner_build_image.location
cloud_function = google_cloudfunctions2_function.runner_build_image.name
role = "roles/cloudfunctions.invoker"
members = [
"serviceAccount:${google_service_account.vm_maintenance_scheduler.email}",
]
}
resource "google_cloud_run_service_iam_binding" "runner_cleanup_invoker" {
location = google_cloudfunctions2_function.runner_cleanup.location
service = google_cloudfunctions2_function.runner_cleanup.name
role = "roles/run.invoker"
members = [
"serviceAccount:${google_service_account.vm_maintenance_scheduler.email}",
]
}
resource "google_cloud_run_service_iam_binding" "runner_build_image_invoker" {
location = google_cloudfunctions2_function.runner_build_image.location
service = google_cloudfunctions2_function.runner_build_image.name
role = "roles/run.invoker"
members = [
"serviceAccount:${google_service_account.vm_maintenance_scheduler.email}",
]
}
// Github doesn't call with any IAM creds; function is responsible for verifying
// identity using the webhook secret instead
resource "google_cloud_run_v2_service_iam_binding" "runner_launch" {
location = google_cloudfunctions2_function.runner_launch.location
name = google_cloudfunctions2_function.runner_launch.name
role = "roles/run.invoker"
members = [
"allUsers"
]
}
output "runner_launch_uri" {
value = google_cloudfunctions2_function.runner_launch.service_config[0].uri
}