forked from mirakl/terraform-google-gcr-cleaner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
129 lines (112 loc) · 4.83 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
# 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
}
}
resources {
limits = {
# CPU usage limit
# https://cloud.google.com/run/docs/configuring/cpu
cpu = var.cloud_run_cpu_usage_limit
# Memory usage limit (per container)
# https://cloud.google.com/run/docs/configuring/memory-limits
memory = var.cloud_run_memory_usage_limit
}
}
}
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],
repo_keep_filter = each.value.repo_keep_filter,
repository_match_prefix = each.value.repository_match_prefix,
tag_filter = each.value.tag_filter,
tag_filter_any = each.value.tag_filter_any,
tag_filter_all = each.value.tag_filter_all,
tag_keep_any = each.value.tag_keep_any,
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]
}