-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes_deployment.tf
230 lines (197 loc) · 7.04 KB
/
kubernetes_deployment.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
locals {
# Set the default values for the health check
# We set the defaults as a local so that we can merge them with the user
# provided values. This allows uses to only overide the some values on
# the resource object. It also allows us to use the container_port, to avoid
# setting redundant values in our code.
liveness_probe_defaults = {
path = "/livez"
port = var.container_port
initial_delay_seconds = 10
period_seconds = 10
timeout_seconds = 2
failure_threshold = 3
success_threshold = 1
}
# Merge Local values with user provided values
liveness_probe = merge(local.liveness_probe_defaults, var.liveness_probe)
readiness_probe_defaults = {
path = "/readyz"
port = var.container_port
initial_delay_seconds = 10
period_seconds = 10
timeout_seconds = 2
failure_threshold = 3
success_threshold = 1
}
# Merge Local values with user provided values
readiness_probe = merge(local.readiness_probe_defaults, var.readiness_probe)
topology_spread_defaults = {
max_skew = 1
topology_key = "kubernetes.io/hostname"
when_unsatisfiable = "ScheduleAnyway"
}
# Merge Local values with user provided values
topology_spread = merge(local.topology_spread_defaults, var.topology_spread)
}
resource "kubernetes_deployment" "platform_deployment" {
metadata {
name = "${var.application_name}-deployment"
namespace = data.kubernetes_namespace.deployment_namespace.id
labels = var.labels
}
wait_for_rollout = var.wait_for_rollout
spec {
replicas = var.min_replicas
# Change the strategy to RollingUpdate to avoid downtime by ensuring max_surge or min_relicas are available before killing the old ones. The defaults are 25%
strategy {
type = "RollingUpdate"
rolling_update {
max_surge = var.max_surge
max_unavailable = var.max_unavailable
}
}
selector {
match_labels = {
app = var.application_name
}
}
template {
metadata {
labels = local.labels
}
spec {
service_account_name = module.deployment_workload_identity.k8s_service_account_name
host_aliases {
hostnames = var.host_alias.hostnames
ip = var.host_alias.ip
}
affinity {
node_affinity {
required_during_scheduling_ignored_during_execution {
node_selector_term {
match_expressions {
key = "pool"
operator = "In"
values = [var.node_pool]
}
}
}
}
}
topology_spread_constraint {
max_skew = local.topology_spread.max_skew
topology_key = local.topology_spread.topology_key
when_unsatisfiable = local.topology_spread.when_unsatisfiable
label_selector {
match_labels = {
"app.kubernetes.io/instance" = "var.application_name"
}
}
}
container {
name = var.application_name
image = var.container_image
##########################################
# Dynamic environment variables
##########################################
dynamic "env" {
for_each = var.env_vars
content {
name = env.key
value = env.value
}
}
##########################################
# Dynamic secret environment variables
##########################################
dynamic "env" {
for_each = var.secret_env_vars
content {
name = env.key
value_from {
secret_key_ref {
name = kubernetes_secret.kubernetes_secrets[0].metadata[0].name
key = env.key
}
}
}
}
##########################################
# Static environment variables
##########################################
# Set the DD_AGENT_HOST environment variable to the host IP address.
env {
name = "DD_AGENT_HOST"
value_from {
field_ref {
field_path = "status.hostIP"
}
}
}
# Nodejs uses the DD_TRACE_AGENT_HOSTNAME environment variable to set
# the agent instead of DD_AGENT_HOST. We can set both without any negative effects.
env {
name = "DD_TRACE_AGENT_HOSTNAME"
value_from {
field_ref {
field_path = "status.hostIP"
}
}
}
env {
name = "DD_SERVICE"
value = "ddm-platform-${var.application_name}"
}
env {
name = "DD_VERSION"
value = var.application_version
}
port {
container_port = var.container_port
protocol = "TCP"
}
# Resource limits and requests for the container. This is used by
# Kubernetes to schedule the container on a node. It is also used by
# the Horizontal Pod Autoscaler to determine when to scale the workload.
resources {
limits = {
cpu = var.resources.limits.cpu
memory = var.resources.limits.memory
}
requests = {
cpu = var.resources.requests.cpu
memory = var.resources.requests.memory
}
}
# Health Checks probes for the container. Currently limited to http
# health checks only and expects the port to be the same as the
# container port. This is intentional to simplify the configuration.
liveness_probe {
http_get {
path = local.liveness_probe.path
port = local.liveness_probe.port
}
initial_delay_seconds = local.liveness_probe.initial_delay_seconds
period_seconds = local.liveness_probe.period_seconds
timeout_seconds = local.liveness_probe.timeout_seconds
failure_threshold = local.liveness_probe.failure_threshold
success_threshold = local.liveness_probe.success_threshold
}
readiness_probe {
http_get {
path = local.readiness_probe.path
port = local.readiness_probe.port
}
initial_delay_seconds = local.readiness_probe.initial_delay_seconds
period_seconds = local.readiness_probe.period_seconds
timeout_seconds = local.readiness_probe.timeout_seconds
failure_threshold = local.readiness_probe.failure_threshold
success_threshold = local.readiness_probe.success_threshold
}
}
}
}
min_ready_seconds = 60
}
}