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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.37.1"
}
}
}
#
# Module inputs
#
variable "annotations" {
type = map(string)
default = {}
description = <<-EOF
A map of strings representing the annotations to set on all resources
created by the module. In Kubernetes manifests, this is equivalent to
`.metadata.annotations`.
EOF
}
variable "labels" {
type = map(string)
default = {}
description = <<-EOF
A map of strings representing the labels to add to all resources created by
the module. In Kubernetes manifests, this is equivalent to
`.metadata.annotations`. Note that the module applies several default
labels, which can be overwritten with this map.
EOF
}
variable "name" {
type = string
description = <<-EOF
The name used for the deployed resources. In Kubernetes manifests, this is
equivalent to `.metadata.name`.
EOF
}
variable "namespace" {
type = string
default = "default"
description = <<-EOF
The namespace used to deploy resources in. This must be created outside of
the module before the module itself will deploy. In Kubernetes manifests,
this is equivalent to `.metadata.namespace`.
EOF
}
# Deployment
variable "allow_privilege_escalation" {
type = bool
default = null
description = <<-EOF
Allow privilege escalation of the container. Be wary of security risks when
setting this to true. In Kubernetes manifests, this is equivalent to
`.spec.template.spec.container[0].securityContext.allowPrivilegeEscalation`.
EOF
}
variable "args" {
type = list(string)
default = null
description = <<-EOF
A list of strings to use as the arguments passed to the command to run
inside the container. In Kubernetes manifests, this is equivalent to
`.spec.template.spec.container[0].args`.
EOF
}
variable "command" {
type = list(string)
default = null
description = <<-EOF
A list of strings representing the command to run inside the container. In
Kubernetes manifests, this is equivalent to
`.spec.template.spec.container[0].command`.
EOF
}
variable "deployment_annotations" {
type = map(string)
default = {}
description = <<-EOF
A map of strings representing the annotations to set on the Deployment
object. In Kubernetes manifests, this is equivalent to
`.metadata.annotations`.
EOF
}
variable "deployment_labels" {
type = map(string)
default = {}
description = <<-EOF
A map of strings representing the labels to add to the Deployment object.
In Kubernetes manifests, this is equivalent to `.metadata.annotations`.
Note that the module applies several default labels, which can be
overwritten with this map.
EOF
}
variable "env" {
type = map(string)
default = {}
description = <<-EOF
A map of strings, where each key represents an environment variable name,
and each value represents the environment variable's value.
EOF
}
variable "env_configmaps" {
type = list(string)
default = []
description = <<-EOF
A list of names of ConfigMap resources, which will be used as environment
variables.
EOF
}
variable "env_secrets" {
type = list(string)
default = []
description = <<-EOF
A list of names of Secret resources, which will be used as environment
variables.
EOF
}
variable "fs_group" {
type = number
default = null
description = <<-EOF
The gid to assign to the container filesystem. If not set, but `gid` is
set, the `fs_group` will be equivalent to `gid`. In Kubernetes manifests,
this is equivalent to `.spec.template.spec.securityContext.fsGroup`.
EOF
}
variable "fs_group_change_policy" {
type = string
default = null
description = <<-EOF
The policy to apply for filesystem group mismatches. In Kubernetes
manifests, this is equivalent to
`.spec.template.spec.securityContext.fsGroupChangePolicy`.
EOF
}
variable "gid" {
type = number
default = null
description = <<-EOF
The gid to assign to the pod through the container security context. In
Kubernetes manifests, this is equivalent to
`.spec.template.spec.securityContext.runAsGroup`.
EOF
}
variable "host_network" {
type = bool
default = false
description = <<-EOF
Allow the container to use the host's own networking namespace. In
Kubernetes manifests, this is equivalent to
`.spec.template.spec.container[0].hostNetwork`.
EOF
}
variable "image" {
type = string
description = <<-EOF
The container image to run. In Kubernetes manifests, this is equivalent to
`.spec.template.spec.container[0].image`.
EOF
}
variable "priority_class" {
type = string
default = null
description = <<-EOF
The priority class name to schedule the containers with. In Kubernetes
manifests, this is equivalent to
`.spec.template.spec.container[0].priorityClassName`.
EOF
}
variable "privileged" {
type = bool
default = false
description = <<-EOF
Run the container with full privileges. Be wary of security risks when
setting this to true. In Kubernetes manifests, this is equivalent to
`.spec.template.spec.container[0].securityContext.privileged`.
EOF
}
variable "pull_policy" {
type = string
default = null
description = <<-EOF
A string to set the container image's pull policy. If not set explicitly,
it will be set to `Always` if the container image tag is `latest`,
`main`, or `master`. Otherwise, it will use the `IfNotPresent` policy. In
Kubernetes manifests, this is equivalent to
`.spec.template.spec.container[0].imagePullPolicy`.
EOF
}
variable "pull_secrets" {
type = list(string)
default = []
description = <<-EOF
A list of names of Secret resources representing image pull secrets. In
Kubernetes manifests, this is equivalent to
`.spec.template.spec.imagePullSecrets`.
EOF
}
variable "replicas" {
type = number
default = 1
description = <<-EOF
The number of replicas to run. In Kubernetes manifests, this is equivalent
to `.spec.replicas`.
EOF
}
variable "resources" {
type = map(map(string))
default = {}
description = <<-EOF
A map to define resource requests and limits for the container. If the map
contains either a `requests` or a `limits` key, the map inside of it will
be mapped directly to the Kubernetes manifest attribute
`.spec.template.spec.container[0].resources.requests` or
`.spec.template.spec.container[0].resources.limits` respectively.
EOF
}
variable "ro_rootfs" {
type = bool
default = false
description = <<-EOF
Mark the container's root filesystem as read-only. It is set to true by
default, which can break some containers. In Kubernetes manifests, this is
equivalent to
`.spec.template.spec.container[0].securityContext.readOnlyRootFilesystem`.
EOF
}
variable "selector" {
type = map(string)
default = {}
description = <<-EOF
A map of strings to set node selector labels. In Kubernetes manifests, this
is equivalent to `.spec.template.spec.nodeSelector`.
EOF
}
variable "strategy" {
type = string
default = null
description = <<-EOF
The strategy used for updating the image. In Kubernetes manifests, this is
equivalent to `.spec.strategy`. If unset, the strategy "Recreate" will be
used if there's only a single replica. If replicas is set to a number
larger than 1, the strategy will default to "RollingUpdate".
EOF
}
variable "uid" {
type = number
default = null
description = <<-EOF
The uid to assign to the pod through the container security context. If this
is set, and not 0, the `run_as_non_root` flag will be set for this container
as well. In Kubernetes manifests, this is equivalent to
`.spec.template.spec.securityContext.runAsUser`.
EOF
}
variable "volumes" {
type = map(map(string))
default = {}
description = <<-EOF
A map of volumes, each containing a map to describe said volumes. Each
volume requires a `path` key to denote where inside the container it must
be mounted. Each volume requires _either_ a `pvc`, `secret` or `configmap`
key, using the name of a PersistentVolumeClaim, Secret, or ConfigMap
respectively to use as the volume source. When using a `secret` or
`configmap`, an optional `key` can be set as well to indicate which key
from the Secret or ConfigMap to use for the data, allowing you to use an
element from a Secret or ConfigMap as a configuration file.
EOF
}
# Service
variable "ports" {
type = list(number)
default = []
description = <<-EOF
A list of port numbers to open up through a Service resource. If unset, or
set to an empty list, no Service object will be created.
EOF
}
variable "service_annotations" {
type = map(string)
default = {}
description = <<-EOF
A map of strings representing the annotations to set on the Service object.
In Kubernetes manifests, this is equivalent to `.metadata.annotations`.
EOF
}
variable "service_labels" {
type = map(string)
default = {}
description = <<-EOF
A map of strings representing the labels to add to the Service object.
In Kubernetes manifests, this is equivalent to `.metadata.annotations`.
Note that the module applies several default labels, which can be
overwritten with this map.
EOF
}
variable "service_type" {
type = string
default = "ClusterIP"
description = <<-EOF
The type of Service to deploy. In Kubernetes manifests, this is equivalent
to `.spec.type`.
EOF
}
# Ingress
variable "hosts" {
type = list(string)
default = []
description = <<-EOF
A list of hostnames to include in the Ingress object.
EOF
}
variable "ingress_annotations" {
type = map(string)
default = {}
description = <<-EOF
A map of strings representing the annotations to set on the Ingress object.
In Kubernetes manifests, this is equivalent to `.metadata.annotations`.
EOF
}
variable "ingress_class" {
type = string
default = ""
description = <<-EOF
The name of the IngressClass to use for this Ingress object. If unset, or
set to an empty string, no Ingress object will be made. In Kubernetes
manifests, this is equivalent to `.spec.ingressClassName`.
EOF
}
variable "ingress_labels" {
type = map(string)
default = {}
description = <<-EOF
A map of strings representing the labels to add to the Ingress object.
In Kubernetes manifests, this is equivalent to `.metadata.annotations`.
Note that the module applies several default labels, which can be
overwritten with this map.
EOF
}
variable "ingress_port" {
type = number
default = 0
description = <<-EOF
The port to use for the Ingress object. If unset, it will use the first
port defined in the `ports` list.
EOF
}
#
# IngressRoute
#
variable "ingress_certresolver" {
type = string
default = null
description = <<-EOF
The name of the certificate resolver provider.
EOF
}
variable "ingress_entrypoints" {
type = list(string)
default = ["web", "websecure"]
description = <<-EOF
A list of entrypoints to use with the IngressRoute object. Defaults to
`["web", "websecure"]`.
EOF
}
variable "ingress_middlewares" {
type = list(string)
default = []
description = <<-EOF
A list of names of Middleware resources to apply to the IngressRoute object.
EOF
}
variable "ingress_route" {
type = bool
default = false
description = <<-EOF
Create an IngressRoute resource, rather than an Ingress resource. Defaults
to `false`.
EOF
}
#
# Module outputs
#
output "labels" {
value = local.labels
}
output "resources" {
value = {
deployment = kubernetes_deployment_v1.this
service = try(kubernetes_service_v1.this[0], null)
ingress = try(kubernetes_ingress_v1.this[0], null)
}
}
output "service_fqdn" {
value = length(kubernetes_service_v1.this) < 1 ? null : "${kubernetes_service_v1.this[0].metadata[0].name}.${kubernetes_service_v1.this[0].metadata[0].namespace}"
}
#
# Module local variables
#
locals {
container_tag = reverse(split(var.image, ":"))[0]
container_tags_pull_always = [
"latest",
"main",
"master",
]
labels = {
"app.kubernetes.io/created-by" = "terraform-kubernetes-container"
"app.kubernetes.io/managed-by" = "opentofu"
"app.kubernetes.io/name" = var.name
"app.kubernetes.io/part-of" = var.name
}
}
|