This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
generated from xoap-io/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
executable file
·250 lines (237 loc) · 6.73 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
243
244
245
246
247
248
249
250
locals {
labels = merge(var.context.tags, {
"managed-by" = "terraform"
})
}
resource "kubernetes_service_account" "this" {
metadata {
name = var.name
namespace = var.namespace
labels = merge(local.labels, {
k8s-app = var.name
})
annotations = var.service_account_annotations
}
automount_service_account_token = true
}
resource "kubernetes_deployment" "this" {
metadata {
name = var.name
namespace = var.namespace
labels = merge(local.labels, {
k8s-app = var.name
})
}
spec {
replicas = 2
strategy {
type = "RollingUpdate"
rolling_update {
max_surge = "25%"
max_unavailable = "25%"
}
}
selector {
match_labels = {
k8s-app = var.name
}
}
template {
metadata {
labels = {
k8s-app = var.name
}
}
spec {
service_account_name = join("", kubernetes_service_account.this.metadata[*].name)
dynamic "volume" {
for_each = var.paths
content {
name = "${var.name}-data-${replace(volume.key, "/", "-")}"
host_path {
path = volume.key
}
}
}
container {
image = var.image
name = var.name
security_context {
capabilities {
drop = ["NET_RAW"]
}
}
dynamic "env" {
for_each = var.environment_variables
content {
name = env.key
value = env.value
}
}
dynamic "port" {
for_each = var.service != null ? toset(["1"]) : toset([])
content {
container_port = var.service.container_port
name = var.service.https_enabled ? "https" : "http"
protocol = "TCP"
}
}
dynamic "liveness_probe" {
for_each = var.service != null ? toset(["1"]) : toset([])
content {
http_get {
path = var.service.healthcheck.path
port = var.service.container_port
scheme = var.service.https_enabled ? "HTTPS" : "HTTP"
}
initial_delay_seconds = var.service.healthcheck.initial_delay_seconds
timeout_seconds = var.service.healthcheck.timeout_seconds
success_threshold = var.service.healthcheck.success_threshold
failure_threshold = var.service.healthcheck.failure_threshold
period_seconds = var.service.healthcheck.period_seconds
}
}
dynamic "readiness_probe" {
for_each = var.service != null ? toset(["1"]) : toset([])
content {
http_get {
path = var.service.healthcheck.path
port = var.service.container_port
scheme = var.service.https_enabled ? "HTTPS" : "HTTP"
}
failure_threshold = 12
success_threshold = 1
period_seconds = 10
}
}
resources {
limits = var.resource_config.limits
requests = var.resource_config.requests
}
dynamic "volume_mount" {
for_each = var.paths
content {
name = "${var.name}-data-${replace(volume_mount.key, "/", "-")}"
mount_path = volume_mount.value
}
}
}
}
}
}
lifecycle {
ignore_changes = [
spec[0].replicas,
metadata[0].annotations["field.cattle.io/publicEndpoints"],
metadata[0].annotations["cattle.io/status"],
metadata[0].annotations["lifecycle.cattle.io/create.namespace-auth"]
]
}
}
resource "kubernetes_service" "this" {
count = var.service != null ? 1 : 0
metadata {
name = var.name
namespace = var.namespace
labels = local.labels
annotations = var.service.annotations
}
spec {
selector = {
k8s-app = kubernetes_deployment.this.metadata[0].labels.k8s-app
}
port {
name = var.service.https_enabled ? "https" : "http"
port = var.service.target_port
target_port = var.service.https_enabled ? "https" : "http"
}
type = var.service.type
}
}
resource "kubernetes_ingress_v1" "this" {
count = var.ingress != null ? 1 : 0
wait_for_load_balancer = true
metadata {
name = var.name
namespace = var.namespace
annotations = var.ingress.annotations
labels = local.labels
}
spec {
ingress_class_name = var.ingress.ingress_class
rule {
host = var.ingress.host
http {
path {
path = "/"
backend {
service {
name = kubernetes_service.this[0].metadata[0].name
port {
number = var.service.target_port
}
}
}
}
}
}
dynamic "rule" {
for_each = var.additional_hosts
content {
host = rule.key
http {
path {
path = rule.value
backend {
service {
name = kubernetes_service.this[0].metadata[0].name
port {
number = var.service.target_port
}
}
}
}
}
}
}
}
lifecycle {
ignore_changes = [
metadata[0].annotations["field.cattle.io/publicEndpoints"],
metadata[0].annotations["cattle.io/status"],
metadata[0].annotations["lifecycle.cattle.io/create.namespace-auth"],
]
}
}
resource "kubernetes_horizontal_pod_autoscaler" "this" {
metadata {
name = "hpa-${var.name}"
namespace = var.namespace
labels = local.labels
}
spec {
max_replicas = var.hpa.max_replicas
min_replicas = var.hpa.min_replicas
target_cpu_utilization_percentage = var.hpa.target_cpu_utilization_percentage
scale_target_ref {
api_version = "apps/v1"
kind = "Deployment"
name = kubernetes_deployment.this.metadata[0].name
}
}
}
resource "kubernetes_pod_disruption_budget" "this" {
metadata {
name = "pdb-${var.name}"
namespace = var.namespace
labels = local.labels
}
spec {
min_available = "50%"
selector {
match_labels = {
k8s-app = kubernetes_deployment.this.metadata[0].labels.k8s-app
}
}
}
}