forked from garbetjie/terraform-google-cloud-run
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
242 lines (212 loc) · 7.75 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
resource "google_cloud_run_service" "default" {
provider = google-beta
name = var.name
location = var.location
autogenerate_revision_name = true
project = local.project_id
metadata {
namespace = local.project_id
labels = var.labels
annotations = {
"run.googleapis.com/launch-stage" = local.launch_stage
"run.googleapis.com/ingress" = var.ingress
"run.googleapis.com/client-name" = "terraform"
}
}
lifecycle {
ignore_changes = [
template[0].metadata[0].annotations["client.knative.dev/user-image"],
template[0].metadata[0].annotations["run.googleapis.com/client-name"],
template[0].metadata[0].annotations["run.googleapis.com/client-version"],
template[0].metadata[0].annotations["run.googleapis.com/sandbox"],
template[0].spec[0].containers[0].image,
metadata[0].annotations["serving.knative.dev/creator"],
metadata[0].annotations["serving.knative.dev/lastModifier"],
metadata[0].annotations["run.googleapis.com/ingress-status"],
metadata[0].annotations["run.googleapis.com/launch-stage"],
metadata[0].annotations["client.knative.dev/user-image"],
metadata[0].labels["cloud.googleapis.com/location"],
]
}
template {
spec {
container_concurrency = var.concurrency
timeout_seconds = var.timeout
service_account_name = var.service_account_email
containers {
image = var.image
command = var.entrypoint
args = var.args
ports {
name = var.http2 ? "h2c" : "http1"
container_port = var.port
}
resources {
limits = {
cpu = "${var.cpus * 1000}m"
memory = "${var.memory}Mi"
}
}
# Populate straight environment variables.
dynamic "env" {
for_each = [for e in local.env : e if e.value != null]
content {
name = env.value.key
value = env.value.value
}
}
# Populate environment variables from secrets.
dynamic "env" {
for_each = [for e in local.env : e if e.secret.name != null]
content {
name = env.value.key
value_from {
secret_key_ref {
name = coalesce(env.value.secret.alias, env.value.secret.name)
key = env.value.secret.version
}
}
}
}
dynamic "volume_mounts" {
for_each = local.volumes
content {
name = volume_mounts.value.name
mount_path = volume_mounts.value.path
}
}
dynamic "startup_probe" {
for_each = var.startup_probe != null ? [1] : []
content {
failure_threshold = var.startup_probe.failure_threshold
initial_delay_seconds = var.startup_probe.initial_delay_seconds
timeout_seconds = var.startup_probe.timeout_seconds
period_seconds = var.startup_probe.period_seconds
dynamic "http_get" {
for_each = var.startup_probe.http_get != null ? [1] : []
content {
path = var.startup_probe.http_get.path
dynamic "http_headers" {
for_each = var.startup_probe.http_get.http_headers != null ? var.startup_probe.http_get.http_headers : []
content {
name = http_headers.value["name"]
value = http_headers.value["value"]
}
}
}
}
dynamic "tcp_socket" {
for_each = var.startup_probe.tcp_socket != null ? [1] : []
content {
port = var.startup_probe.tcp_socket.port
}
}
dynamic "grpc" {
for_each = var.startup_probe.grpc != null ? [1] : []
content {
port = var.startup_probe.grpc.port
service = var.startup_probe.grpc.service
}
}
}
}
dynamic "liveness_probe" {
for_each = var.liveness_probe != null ? [1] : []
content {
failure_threshold = var.liveness_probe.failure_threshold
initial_delay_seconds = var.liveness_probe.initial_delay_seconds
timeout_seconds = var.liveness_probe.timeout_seconds
period_seconds = var.liveness_probe.period_seconds
dynamic "http_get" {
for_each = var.liveness_probe.http_get != null ? [1] : []
content {
path = var.liveness_probe.http_get.path
dynamic "http_headers" {
for_each = var.liveness_probe.http_get.http_headers != null ? var.liveness_probe.http_get.http_headers : []
content {
name = http_headers.value["name"]
value = http_headers.value["value"]
}
}
}
}
dynamic "grpc" {
for_each = var.liveness_probe.grpc != null ? [1] : []
content {
port = var.liveness_probe.grpc.port
service = var.liveness_probe.grpc.service
}
}
}
}
}
dynamic "volumes" {
for_each = local.volumes
content {
name = volumes.value.name
secret {
secret_name = coalesce(volumes.value.secret.alias, volumes.value.secret.name)
dynamic "items" {
for_each = volumes.value.items
content {
key = items.value.version
path = items.value.filename
}
}
}
}
}
}
metadata {
labels = var.labels
annotations = merge(
{
"run.googleapis.com/cpu-throttling" = var.cpu_throttling
"run.googleapis.com/cloudsql-instances" = join(",", var.cloudsql_connections)
"autoscaling.knative.dev/maxScale" = var.max_instances
"autoscaling.knative.dev/minScale" = var.min_instances
"run.googleapis.com/execution-environment" = var.execution_environment
"run.googleapis.com/startup-cpu-boost" = var.startup_cpu_boost
},
local.vpc_access.connector == null ? {} : {
"run.googleapis.com/vpc-access-connector" = local.vpc_access.connector
"run.googleapis.com/vpc-access-egress" = local.vpc_access.egress
},
length(local.secrets_to_aliases) < 1 ? {} : {
"run.googleapis.com/secrets" = join(",", [for secret, alias in local.secrets_to_aliases : "${alias}:${secret}"])
},
)
}
}
traffic {
percent = 100
latest_revision = var.revision == null
revision_name = var.revision != null ? "${var.name}-${var.revision}" : null
}
}
resource "google_cloud_run_service_iam_member" "public_access" {
count = var.allow_public_access ? 1 : 0
service = google_cloud_run_service.default.name
location = google_cloud_run_service.default.location
project = google_cloud_run_service.default.project
role = "roles/run.invoker"
member = "allUsers"
}
resource "google_cloud_run_domain_mapping" "domains" {
for_each = var.map_domains
location = google_cloud_run_service.default.location
project = google_cloud_run_service.default.project
name = each.value
metadata {
namespace = google_cloud_run_service.default.project
annotations = {
"run.googleapis.com/launch-stage" = local.launch_stage
}
}
spec {
route_name = google_cloud_run_service.default.name
}
lifecycle {
ignore_changes = [metadata[0]]
}
}