-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.tf
179 lines (151 loc) · 5 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
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 = merge(
{
"run.googleapis.com/launch-stage" = local.launch_stage
"run.googleapis.com/ingress" = var.ingress
"run.googleapis.com/client-name" = "terraform"
"client.knative.dev/user-image" = var.image
},
length(local.secrets_to_aliases) < 1 ? {} : {
"run.googleapis.com/secrets" = join(",", [for secret, alias in local.secrets_to_aliases: "${alias}:${secret}"])
}
)
}
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"],
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].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 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
},
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]]
}
}