-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocals_containers.tf
295 lines (286 loc) · 11 KB
/
locals_containers.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
// all the logic to parse the config YAML file into objects we can use in Terraform
// good luck reading and debugging this
locals {
container_namespaces = merge(flatten(
contains(keys(local.config), "container_namespaces") ? [
for ns in local.config.container_namespaces : {
format("%s/%s/%s", ns.region, ns.project_id, ns.name) = {
region = ns.region
project_id = ns.project_id
name = ns.name
description = contains(keys(ns), "description") ? ns.description : ""
environment_variables = contains(keys(ns), "environment_variables") ? { for v in ns.environment_variables : v.key => v.value } : {}
secret_environment_variables = contains(keys(ns), "secret_environment_variables") ? { for v in ns.secret_environment_variables : v.key => v.value } : {}
}
}
] : []
)...)
containers = merge(flatten(
contains(keys(local.config), "container_namespaces") ? [
for ns in local.config.container_namespaces : [
contains(keys(ns), "containers") ? (
[
for c in(ns.containers) : {
format("%s/%s/%s/%s", ns.region, ns.project_id, ns.name, c.name) = {
namespace = {
region = ns.region
project_id = ns.project_id
name = ns.name
}
container = {
name = c.name
cpu_limit = c.cpu_limit
memory_limit = c.memory_limit
registry_image = contains(keys(c), "image") ? c.image : null // null means we will built the image and use it
port = c.port // port is normally optional, but I'd rather put it as mandatory to avoid surprises and common issues
privacy = c.privacy
description = contains(keys(c), "description") ? c.description : ""
environment_variables = contains(keys(c), "environment_variables") ? { for v in c.environment_variables : v.key => v.value } : {}
secret_environment_variables = contains(keys(c), "secret_environment_variables") ? { for v in c.secret_environment_variables : v.key => v.value } : {}
min_scale = contains(keys(c), "min_scale") ? c.min_scale : 0
max_scale = contains(keys(c), "max_scale") ? c.max_scale : 1
timeout = contains(keys(c), "timeout") ? c.timeout : 300
http_option = contains(keys(c), "http_option") ? c.http_option : "redirected"
max_concurrency = contains(keys(c), "max_concurrency") ? c.max_concurrency : 50
}
}
}
]
) : []
]
] : []
)...)
container_crons = merge(flatten(
contains(keys(local.config), "container_namespaces") ? [
for ns in local.config.container_namespaces : [
contains(keys(ns), "containers") ? (
[
for f in(ns.containers) : [
contains(keys(f), "triggers") ? (
contains(keys(f.triggers), "crons") ? (
[
for c in(f.triggers.crons) :
{
format("%s/%s/%s/%s/%s", ns.region, ns.project_id, ns.name, f.name, c.name) = {
namespace = {
region = ns.region
name = ns.name
project_id = ns.project_id
}
container = {
name = f.name
}
cron = {
name = c.name
schedule = c.schedule
args = jsonencode(jsondecode(c.json_args))
}
}
}
]
) : []
) : []
]
]
) : []
]
] : []
)...)
container_sqs_triggers = merge(flatten(
contains(keys(local.config), "container_namespaces") ? [
for ns in local.config.container_namespaces : [
contains(keys(ns), "containers") ? (
[
for f in(ns.containers) : [
contains(keys(f), "triggers") ? (
contains(keys(f.triggers), "sqs") ? (
[
for t in(f.triggers.sqs) :
{
format("%s/%s/%s/%s/%s", ns.region, ns.project_id, ns.name, f.name, t.name) = {
namespace = {
region = ns.region
name = ns.name
project_id = ns.project_id
}
container = {
name = f.name
}
sqs = {
name = t.name
description = contains(keys(t), "description") ? t.description : ""
queue = t.queue
project_id = contains(keys(t), "project_id") ? t.project_id : ns.project_id
region = contains(keys(t), "region") ? t.region : ns.region
}
}
}
]
) : []
) : []
]
]
) : []
]
] : []
)...)
container_nats_triggers = merge(flatten(
contains(keys(local.config), "container_namespaces") ? [
for ns in local.config.container_namespaces : [
contains(keys(ns), "containers") ? (
[
for f in(ns.containers) : [
contains(keys(f), "triggers") ? (
contains(keys(f.triggers), "nats") ? (
[
for t in(f.triggers.nats) :
{
format("%s/%s/%s/%s/%s", ns.region, ns.project_id, ns.name, f.name, t.name) = {
namespace = {
region = ns.region
name = ns.name
project_id = ns.project_id
}
container = {
name = f.name
}
nats = {
name = t.name
description = contains(keys(t), "description") ? t.description : ""
account_id = t.account_id
subject = t.subject
project_id = contains(keys(t), "project_id") ? t.project_id : ns.project_id
region = contains(keys(t), "region") ? t.region : ns.region
}
}
}
]
) : []
) : []
]
]
) : []
]
] : []
)...)
container_domains = merge(flatten(
contains(keys(local.config), "container_namespaces") ? [
for ns in local.config.container_namespaces : [
contains(keys(ns), "containers") ? (
[
for f in(ns.containers) : [
contains(keys(f), "domains") ? (
[
for d in(toset(f.domains)) :
{
format("%s/%s/%s/%s/%s", ns.region, ns.project_id, ns.name, f.name, d) = {
namespace = {
region = ns.region
name = ns.name
project_id = ns.project_id
}
container = {
name = f.name
}
domain = d
}
}
]
) : []
]
]
) : []
]
] : []
)...)
container_namespace_tokens = merge(flatten(
contains(keys(local.config), "container_namespaces") ? [
for ns in local.config.container_namespaces : [
contains(keys(ns), "tokens") ? (
[
for t in ns.tokens : {
// we can't pass a unique name to a token, so we use the description as the unique discriminator of tokens
format("%s/%s/%s/%s", ns.region, ns.project_id, ns.name, t.description) = {
namespace = {
region = ns.region
project_id = ns.project_id
name = ns.name
}
token = {
description = contains(keys(t), "description") ? t.description : ""
expires_at = contains(keys(t), "expires_at") ? t.expires_at : null
}
}
}
]
) : []
]
] : []
)...)
container_tokens = merge(flatten(
contains(keys(local.config), "container_namespaces") ? [
for ns in local.config.container_namespaces : [
contains(keys(ns), "containers") ? (
[
for f in(ns.containers) : [
contains(keys(f), "tokens") ? (
[
for t in f.tokens :
{
// we can't pass a unique name to a token, so we use the description as the unique discriminator of tokens
format("%s/%s/%s/%s/%s", ns.region, ns.project_id, ns.name, f.name, t.description) = {
namespace = {
region = ns.region
name = ns.name
project_id = ns.project_id
}
container = {
name = f.name
}
token = {
description = contains(keys(t), "description") ? t.description : ""
expires_at = contains(keys(t), "expires_at") ? t.expires_at : null
}
}
}
]
) : []
]
]
) : []
]
] : []
)...)
}
locals {
container_images = merge(flatten(
contains(keys(local.config), "container_namespaces") ? [
for ns in local.config.container_namespaces : [
contains(keys(ns), "containers") ? (
[
for f in(ns.containers) : [
contains(keys(f), "build") ? (
{
format("%s/%s/%s/%s", ns.region, ns.project_id, ns.name, f.name) = {
namespace = {
region = ns.region
name = ns.name
project_id = ns.project_id
}
container = {
name = f.name
}
build = {
dockerfile = format("%s/%s", var.context_dir, contains(keys(f.build), "dockerfile") ? f.build.dockerfile : "${f.build.context}/Dockerfile")
context = format("%s/%s", var.context_dir, f.build.context)
}
}
}
) : {}
]
]
) : []
]
] : []
)...)
}