This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.tf
114 lines (99 loc) · 4.2 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
# Deploy the gcr-cleaner container on Cloud Run
# running as gcr-cleaner service account
resource "google_cloud_run_service" "this" {
name = var.cloud_run_service_name
location = var.cloud_run_service_location
template {
metadata {
annotations = {
"autoscaling.knative.dev/maxScale" = var.cloud_run_service_maximum_instances
"run.googleapis.com/client-name" = "cloud-scheduler"
}
labels = {
"run.googleapis.com/startupProbeType" = "Default"
}
}
spec {
containers {
image = var.gcr_cleaner_image
dynamic "env" {
for_each = local.gcr_cleaner_env
content {
name = env.value.name
value = env.value.value
}
}
}
service_account_name = google_service_account.cleaner.email
timeout_seconds = var.cloud_run_service_timeout_seconds
}
}
autogenerate_revision_name = true
traffic {
percent = 100
latest_revision = true
}
depends_on = [google_project_service.this]
}
# Create a Cloud Scheduler HTTP job to invoke the function
# 1. App Engine app
#
# /!\ Terraform is not able to delete App Engine applications
# so if you have to destroy all resources then apply in same project,
# just import google_app_engine_application resource before applying:
# terraform import google_app_engine_application.this your-project-id
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/app_engine_application
#
# /!\ If you want to create your app engine app using terraform, just set `create_app_engine_app` variable to true
# and provide the app engine app location:
# create_app_engine_app = true
# app_engine_application_location = "us-central1"
resource "google_app_engine_application" "this" {
count = var.create_app_engine_app ? 1 : 0
project = local.google_project_id
location_id = var.app_engine_application_location
depends_on = [google_project_service.this]
}
# 2. Cloud Scheduler job that triggers an action via HTTP
resource "google_cloud_scheduler_job" "this" {
for_each = {
# The key is the `schedule_job_name` if exist otherwise it's a concatenation
# of the filter and repo.
# name must match the RE2 regular expression "[a-zA-Z\d_-]{1,500}"
# and be no more than 500 characters.
# First replace all special characters with '-',
# then replace any at least 2 consecutive `-` characters with one '-'.
for repo in toset(local.fetched_repositories) : replace(replace(repo.scheduler_job_name != null ? repo.scheduler_job_name : "${repo.filter}:${repo.repo}", "/[\\W+:/]/", "-"), "/-{2,}/", "-") => repo
}
name = each.value.scheduler_job_name != null ? each.value.scheduler_job_name : each.key
description = each.value.scheduler_job_description != null ? each.value.scheduler_job_description : "Cleanup ${each.value.repo} using ${each.value.filter} filter"
schedule = var.cloud_scheduler_job_schedule
time_zone = var.cloud_scheduler_job_time_zone
attempt_deadline = "${var.cloud_scheduler_job_attempt_deadline}s"
region = local.cloud_scheduler_job_location
retry_config {
retry_count = var.cloud_scheduler_job_retry_count
min_backoff_duration = "${var.cloud_scheduler_job_min_backoff_duration}s"
max_backoff_duration = "${var.cloud_scheduler_job_max_backoff_duration}s"
max_retry_duration = "${var.cloud_scheduler_job_max_retry_duration}s"
max_doublings = var.cloud_scheduler_job_max_doublings
}
http_target {
http_method = "POST"
uri = "${google_cloud_run_service.this.status[0].url}/http"
body = base64encode(jsonencode({
grace = each.value.grace,
keep = tonumber(each.value.keep),
repos = [each.value.repo],
tag_filter = each.value.tag_filter,
tag_filter_any = each.value.tag_filter_any,
tag_filter_all = each.value.tag_filter_all,
dry_run = tobool(each.value.dry_run),
recursive = tobool(each.value.recursive),
}))
oidc_token {
service_account_email = google_service_account.invoker.email
}
}
depends_on = [google_project_service.this, google_app_engine_application.this]
}