-
Notifications
You must be signed in to change notification settings - Fork 50
/
server_nodes.tf
454 lines (384 loc) · 22.6 KB
/
server_nodes.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
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
locals {
// Some vars use to easily access to the first k3s server values
root_server_name = keys(var.servers)[0]
// Get the first address from the IP array using comma's as the delimiter
root_advertise_ip = split(",", values(var.servers)[0].ip)[0]
// If root_advertise_ip is IPv6 wrap it in square brackets for IPv6 K3S URLs otherwise leave it raw
root_advertise_ip_k3s = can(regex("::", local.root_advertise_ip)) ? "[${local.root_advertise_ip}]" : local.root_advertise_ip
// string representation of all specified extra k3s installation env vars
install_env_vars = join(" ", [for k, v in var.k3s_install_env_vars : "${k}=${v}"])
root_server_connection = {
type = try(var.servers[local.root_server_name].connection.type, "ssh")
host = try(var.servers[local.root_server_name].connection.host, var.servers[local.root_server_name].ip)
user = try(var.servers[local.root_server_name].connection.user, null)
password = try(var.servers[local.root_server_name].connection.password, null)
port = try(var.servers[local.root_server_name].connection.port, null)
timeout = try(var.servers[local.root_server_name].connection.timeout, null)
script_path = try(var.servers[local.root_server_name].connection.script_path, null)
private_key = try(var.servers[local.root_server_name].connection.private_key, null)
certificate = try(var.servers[local.root_server_name].connection.certificate, null)
agent = try(var.servers[local.root_server_name].connection.agent, null)
agent_identity = try(var.servers[local.root_server_name].connection.agent_identity, null)
host_key = try(var.servers[local.root_server_name].connection.host_key, null)
https = try(var.servers[local.root_server_name].connection.https, null)
insecure = try(var.servers[local.root_server_name].connection.insecure, null)
use_ntlm = try(var.servers[local.root_server_name].connection.use_ntlm, null)
cacert = try(var.servers[local.root_server_name].connection.cacert, null)
bastion_host = try(var.servers[local.root_server_name].connection.bastion_host, null)
bastion_host_key = try(var.servers[local.root_server_name].connection.bastion_host_key, null)
bastion_port = try(var.servers[local.root_server_name].connection.bastion_port, null)
bastion_user = try(var.servers[local.root_server_name].connection.bastion_user, null)
bastion_password = try(var.servers[local.root_server_name].connection.bastion_password, null)
bastion_private_key = try(var.servers[local.root_server_name].connection.bastion_private_key, null)
bastion_certificate = try(var.servers[local.root_server_name].connection.bastion_certificate, null)
}
// Generate a map of all servers annotations in order to manage them through this module. This
// generation is made in two steps:
// - generate a list of objects representing all annotations, following this
// 'template' {key = node_name|annotation_name, value = annotation_value}
// - generate a map based on the generated list (using the field key as map key)
server_annotations_list = flatten([
for nk, nv in var.servers : [
// Because we need node name and annotation name when we remove the annotation resource, we need
// to share them through the annotation key (each.value are not avaible on destruction).
for ak, av in try(nv.annotations, {}) : av == null ? { key : "" } : { key : "${nk}${var.separator}${ak}", value : av }
]
])
server_annotations = local.managed_annotation_enabled ? { for o in local.server_annotations_list : o.key => o.value if o.key != "" } : {}
// Generate a map of all servers labels in order to manage them through this module. This
// generation is made in two steps, following the same process than annotation's map.
server_labels_list = flatten([
for nk, nv in var.servers : [
// Because we need node name and label name when we remove the label resource, we need
// to share them through the label key (each.value are not avaible on destruction).
for lk, lv in try(nv.labels, {}) : lv == null ? { key : "" } : { key : "${nk}${var.separator}${lk}", value : lv }
]
])
server_labels = local.managed_label_enabled ? { for o in local.server_labels_list : o.key => o.value if o.key != "" } : {}
// Generate a map of all servers taints in order to manage them through this module. This
// generation is made in two steps, following the same process than annotation's map.
server_taints_list = flatten([
for nk, nv in var.servers : [
// Because we need node name and taint name when we remove the taint resource, we need
// to share them through the taint key (each.value are not avaible on destruction).
for tk, tv in try(nv.taints, {}) : tv == null ? { key : "" } : { key : "${nk}${var.separator}${tk}", value : tv }
]
])
server_taints = local.managed_taint_enabled ? { for o in local.server_taints_list : o.key => o.value if o.key != "" } : {}
// Generate a map of all calculated server fields, used during k3s installation.
servers_metadata = {
for key, server in var.servers :
key => {
name = try(server.name, key)
ip = server.ip
flags = join(" ", compact(concat(
key == local.root_server_name ?
// For the first server node, add all configuration flags
[
"--advertise-address ${local.root_advertise_ip}",
"--node-ip ${server.ip}",
"--node-name '${try(server.name, key)}'",
"--cluster-domain '${var.cluster_domain}'",
"--cluster-cidr ${var.cidr.pods}",
"--service-cidr ${var.cidr.services}",
"--token ${nonsensitive(random_password.k3s_cluster_secret.result)}", # NOTE: nonsensitive is used to show logs during provisioning
length(var.servers) > 1 ? "--cluster-init" : "",
] :
// For other server nodes, use agent flags (because the first node manage the cluster configuration)
[
"--node-ip ${server.ip}",
"--node-name '${try(server.name, key)}'",
"--server https://${local.root_advertise_ip_k3s}:6443",
"--cluster-domain '${var.cluster_domain}'",
"--cluster-cidr ${var.cidr.pods}",
"--service-cidr ${var.cidr.services}",
"--token ${nonsensitive(random_password.k3s_cluster_secret.result)}", # NOTE: nonsensitive is used to show logs during provisioning
],
var.global_flags,
try(server.flags, []),
[for key, value in try(server.taints, {}) : "--node-taint '${key}=${value}'" if value != null]
)))
immutable_fields_hash = sha1(join("", concat(
[var.cluster_domain, var.cidr.pods, var.cidr.services],
var.global_flags,
try(server.flags, []),
)))
}
}
}
// Install k3s server
resource "null_resource" "k8s_ca_certificates_install" {
count = length(local.certificates_files)
depends_on = [var.depends_on_]
connection {
type = try(local.root_server_connection.type, "ssh")
host = try(local.root_server_connection.host, local.root_server_connection.ip)
user = try(local.root_server_connection.user, null)
password = try(local.root_server_connection.password, null)
port = try(local.root_server_connection.port, null)
timeout = try(local.root_server_connection.timeout, null)
script_path = try(local.root_server_connection.script_path, null)
private_key = try(local.root_server_connection.private_key, null)
certificate = try(local.root_server_connection.certificate, null)
agent = try(local.root_server_connection.agent, null)
agent_identity = try(local.root_server_connection.agent_identity, null)
host_key = try(local.root_server_connection.host_key, null)
https = try(local.root_server_connection.https, null)
insecure = try(local.root_server_connection.insecure, null)
use_ntlm = try(local.root_server_connection.use_ntlm, null)
cacert = try(local.root_server_connection.cacert, null)
bastion_host = try(local.root_server_connection.bastion_host, null)
bastion_host_key = try(local.root_server_connection.bastion_host_key, null)
bastion_port = try(local.root_server_connection.bastion_port, null)
bastion_user = try(local.root_server_connection.bastion_user, null)
bastion_password = try(local.root_server_connection.bastion_password, null)
bastion_private_key = try(local.root_server_connection.bastion_private_key, null)
bastion_certificate = try(local.root_server_connection.bastion_certificate, null)
}
provisioner "remote-exec" {
inline = [
<<-EOT
# --- use sudo if we are not already root ---
[ $(id -u) -eq 0 ] || exec sudo -n $0 $@
mkdir -p /var/lib/rancher/k3s/server/tls/
echo '${local.certificates_files[count.index].file_content}' > /var/lib/rancher/k3s/server/tls/${local.certificates_files[count.index].file_name}
EOT
]
}
}
resource "null_resource" "servers_install" {
for_each = var.servers
depends_on = [var.depends_on_, null_resource.k8s_ca_certificates_install]
triggers = {
on_immutable_changes = local.servers_metadata[each.key].immutable_fields_hash
on_new_version = local.k3s_version
}
connection {
type = try(each.value.connection.type, "ssh")
host = try(each.value.connection.host, each.value.ip)
user = try(each.value.connection.user, null)
password = try(each.value.connection.password, null)
port = try(each.value.connection.port, null)
timeout = try(each.value.connection.timeout, null)
script_path = try(each.value.connection.script_path, null)
private_key = try(each.value.connection.private_key, null)
certificate = try(each.value.connection.certificate, null)
agent = try(each.value.connection.agent, null)
agent_identity = try(each.value.connection.agent_identity, null)
host_key = try(each.value.connection.host_key, null)
https = try(each.value.connection.https, null)
insecure = try(each.value.connection.insecure, null)
use_ntlm = try(each.value.connection.use_ntlm, null)
cacert = try(each.value.connection.cacert, null)
bastion_host = try(each.value.connection.bastion_host, null)
bastion_host_key = try(each.value.connection.bastion_host_key, null)
bastion_port = try(each.value.connection.bastion_port, null)
bastion_user = try(each.value.connection.bastion_user, null)
bastion_password = try(each.value.connection.bastion_password, null)
bastion_private_key = try(each.value.connection.bastion_private_key, null)
bastion_certificate = try(each.value.connection.bastion_certificate, null)
}
// Upload k3s file
provisioner "file" {
content = data.http.k3s_installer.response_body
destination = "/tmp/k3s-installer"
}
// Install k3s server
provisioner "remote-exec" {
inline = [
"${local.install_env_vars} INSTALL_K3S_VERSION=${local.k3s_version} sh /tmp/k3s-installer server ${local.servers_metadata[each.key].flags}",
"until ${local.kubectl_cmd} get node ${local.servers_metadata[each.key].name}; do sleep 1; done"
]
}
}
// Drain k3s node on destruction in order to safely move all workflows to another node.
resource "null_resource" "servers_drain" {
for_each = var.servers
depends_on = [null_resource.servers_install]
triggers = {
server_name = local.servers_metadata[split(var.separator, each.key)[0]].name
connection_json = base64encode(jsonencode(local.root_server_connection))
drain_timeout = var.drain_timeout
kubectl_cmd = local.kubectl_cmd
}
lifecycle { ignore_changes = [triggers] }
connection {
type = jsondecode(base64decode(self.triggers.connection_json)).type
host = jsondecode(base64decode(self.triggers.connection_json)).host
user = jsondecode(base64decode(self.triggers.connection_json)).user
password = jsondecode(base64decode(self.triggers.connection_json)).password
port = jsondecode(base64decode(self.triggers.connection_json)).port
timeout = jsondecode(base64decode(self.triggers.connection_json)).timeout
script_path = jsondecode(base64decode(self.triggers.connection_json)).script_path
private_key = jsondecode(base64decode(self.triggers.connection_json)).private_key
certificate = jsondecode(base64decode(self.triggers.connection_json)).certificate
agent = jsondecode(base64decode(self.triggers.connection_json)).agent
agent_identity = jsondecode(base64decode(self.triggers.connection_json)).agent_identity
host_key = jsondecode(base64decode(self.triggers.connection_json)).host_key
https = jsondecode(base64decode(self.triggers.connection_json)).https
insecure = jsondecode(base64decode(self.triggers.connection_json)).insecure
use_ntlm = jsondecode(base64decode(self.triggers.connection_json)).use_ntlm
cacert = jsondecode(base64decode(self.triggers.connection_json)).cacert
bastion_host = jsondecode(base64decode(self.triggers.connection_json)).bastion_host
bastion_host_key = jsondecode(base64decode(self.triggers.connection_json)).bastion_host_key
bastion_port = jsondecode(base64decode(self.triggers.connection_json)).bastion_port
bastion_user = jsondecode(base64decode(self.triggers.connection_json)).bastion_user
bastion_password = jsondecode(base64decode(self.triggers.connection_json)).bastion_password
bastion_private_key = jsondecode(base64decode(self.triggers.connection_json)).bastion_private_key
bastion_certificate = jsondecode(base64decode(self.triggers.connection_json)).bastion_certificate
}
provisioner "remote-exec" {
when = destroy
inline = [
"${self.triggers.kubectl_cmd} drain ${self.triggers.server_name} --delete-local-data --force --ignore-daemonsets --timeout=${self.triggers.drain_timeout}"
]
}
}
// Add/remove manually annotation on k3s server
resource "null_resource" "servers_annotation" {
for_each = local.server_annotations
depends_on = [null_resource.servers_install]
triggers = {
server_name = local.servers_metadata[split(var.separator, each.key)[0]].name
annotation_name = split(var.separator, each.key)[1]
on_value_changes = each.value
connection_json = base64encode(jsonencode(local.root_server_connection))
kubectl_cmd = local.kubectl_cmd
}
lifecycle { ignore_changes = [triggers["connection_json"], triggers["kubectl_cmd"]] }
connection {
type = jsondecode(base64decode(self.triggers.connection_json)).type
host = jsondecode(base64decode(self.triggers.connection_json)).host
user = jsondecode(base64decode(self.triggers.connection_json)).user
password = jsondecode(base64decode(self.triggers.connection_json)).password
port = jsondecode(base64decode(self.triggers.connection_json)).port
timeout = jsondecode(base64decode(self.triggers.connection_json)).timeout
script_path = jsondecode(base64decode(self.triggers.connection_json)).script_path
private_key = jsondecode(base64decode(self.triggers.connection_json)).private_key
certificate = jsondecode(base64decode(self.triggers.connection_json)).certificate
agent = jsondecode(base64decode(self.triggers.connection_json)).agent
agent_identity = jsondecode(base64decode(self.triggers.connection_json)).agent_identity
host_key = jsondecode(base64decode(self.triggers.connection_json)).host_key
https = jsondecode(base64decode(self.triggers.connection_json)).https
insecure = jsondecode(base64decode(self.triggers.connection_json)).insecure
use_ntlm = jsondecode(base64decode(self.triggers.connection_json)).use_ntlm
cacert = jsondecode(base64decode(self.triggers.connection_json)).cacert
bastion_host = jsondecode(base64decode(self.triggers.connection_json)).bastion_host
bastion_host_key = jsondecode(base64decode(self.triggers.connection_json)).bastion_host_key
bastion_port = jsondecode(base64decode(self.triggers.connection_json)).bastion_port
bastion_user = jsondecode(base64decode(self.triggers.connection_json)).bastion_user
bastion_password = jsondecode(base64decode(self.triggers.connection_json)).bastion_password
bastion_private_key = jsondecode(base64decode(self.triggers.connection_json)).bastion_private_key
bastion_certificate = jsondecode(base64decode(self.triggers.connection_json)).bastion_certificate
}
provisioner "remote-exec" {
inline = [
"${self.triggers.kubectl_cmd} annotate --overwrite node ${self.triggers.server_name} ${self.triggers.annotation_name}=${self.triggers.on_value_changes}"
]
}
provisioner "remote-exec" {
when = destroy
inline = [
"${self.triggers.kubectl_cmd} annotate node ${self.triggers.server_name} ${self.triggers.annotation_name}-"
]
}
}
// Add/remove manually label on k3s server
resource "null_resource" "servers_label" {
for_each = local.server_labels
depends_on = [null_resource.servers_install]
triggers = {
server_name = local.servers_metadata[split(var.separator, each.key)[0]].name
label_name = split(var.separator, each.key)[1]
on_value_changes = each.value
connection_json = base64encode(jsonencode(local.root_server_connection))
kubectl_cmd = local.kubectl_cmd
}
lifecycle { ignore_changes = [triggers["connection_json"], triggers["kubectl_cmd"]] }
connection {
type = jsondecode(base64decode(self.triggers.connection_json)).type
host = jsondecode(base64decode(self.triggers.connection_json)).host
user = jsondecode(base64decode(self.triggers.connection_json)).user
password = jsondecode(base64decode(self.triggers.connection_json)).password
port = jsondecode(base64decode(self.triggers.connection_json)).port
timeout = jsondecode(base64decode(self.triggers.connection_json)).timeout
script_path = jsondecode(base64decode(self.triggers.connection_json)).script_path
private_key = jsondecode(base64decode(self.triggers.connection_json)).private_key
certificate = jsondecode(base64decode(self.triggers.connection_json)).certificate
agent = jsondecode(base64decode(self.triggers.connection_json)).agent
agent_identity = jsondecode(base64decode(self.triggers.connection_json)).agent_identity
host_key = jsondecode(base64decode(self.triggers.connection_json)).host_key
https = jsondecode(base64decode(self.triggers.connection_json)).https
insecure = jsondecode(base64decode(self.triggers.connection_json)).insecure
use_ntlm = jsondecode(base64decode(self.triggers.connection_json)).use_ntlm
cacert = jsondecode(base64decode(self.triggers.connection_json)).cacert
bastion_host = jsondecode(base64decode(self.triggers.connection_json)).bastion_host
bastion_host_key = jsondecode(base64decode(self.triggers.connection_json)).bastion_host_key
bastion_port = jsondecode(base64decode(self.triggers.connection_json)).bastion_port
bastion_user = jsondecode(base64decode(self.triggers.connection_json)).bastion_user
bastion_password = jsondecode(base64decode(self.triggers.connection_json)).bastion_password
bastion_private_key = jsondecode(base64decode(self.triggers.connection_json)).bastion_private_key
bastion_certificate = jsondecode(base64decode(self.triggers.connection_json)).bastion_certificate
}
provisioner "remote-exec" {
inline = [
"${self.triggers.kubectl_cmd} label --overwrite node ${self.triggers.server_name} ${self.triggers.label_name}=${self.triggers.on_value_changes}"
]
}
provisioner "remote-exec" {
when = destroy
inline = [
"${self.triggers.kubectl_cmd} label node ${self.triggers.server_name} ${self.triggers.label_name}-"
]
}
}
// Add/remove manually taint on k3s server
resource "null_resource" "servers_taint" {
for_each = local.server_taints
depends_on = [null_resource.servers_install]
triggers = {
server_name = local.servers_metadata[split(var.separator, each.key)[0]].name
taint_name = split(var.separator, each.key)[1]
connection_json = base64encode(jsonencode(local.root_server_connection))
on_value_changes = each.value
connection_json = base64encode(jsonencode(local.root_server_connection))
kubectl_cmd = local.kubectl_cmd
}
lifecycle { ignore_changes = [triggers["connection_json"], triggers["kubectl_cmd"]] }
connection {
type = jsondecode(base64decode(self.triggers.connection_json)).type
host = jsondecode(base64decode(self.triggers.connection_json)).host
user = jsondecode(base64decode(self.triggers.connection_json)).user
password = jsondecode(base64decode(self.triggers.connection_json)).password
port = jsondecode(base64decode(self.triggers.connection_json)).port
timeout = jsondecode(base64decode(self.triggers.connection_json)).timeout
script_path = jsondecode(base64decode(self.triggers.connection_json)).script_path
private_key = jsondecode(base64decode(self.triggers.connection_json)).private_key
certificate = jsondecode(base64decode(self.triggers.connection_json)).certificate
agent = jsondecode(base64decode(self.triggers.connection_json)).agent
agent_identity = jsondecode(base64decode(self.triggers.connection_json)).agent_identity
host_key = jsondecode(base64decode(self.triggers.connection_json)).host_key
https = jsondecode(base64decode(self.triggers.connection_json)).https
insecure = jsondecode(base64decode(self.triggers.connection_json)).insecure
use_ntlm = jsondecode(base64decode(self.triggers.connection_json)).use_ntlm
cacert = jsondecode(base64decode(self.triggers.connection_json)).cacert
bastion_host = jsondecode(base64decode(self.triggers.connection_json)).bastion_host
bastion_host_key = jsondecode(base64decode(self.triggers.connection_json)).bastion_host_key
bastion_port = jsondecode(base64decode(self.triggers.connection_json)).bastion_port
bastion_user = jsondecode(base64decode(self.triggers.connection_json)).bastion_user
bastion_password = jsondecode(base64decode(self.triggers.connection_json)).bastion_password
bastion_private_key = jsondecode(base64decode(self.triggers.connection_json)).bastion_private_key
bastion_certificate = jsondecode(base64decode(self.triggers.connection_json)).bastion_certificate
}
provisioner "remote-exec" {
inline = [
"${self.triggers.kubectl_cmd} taint node ${self.triggers.server_name} ${self.triggers.taint_name}=${self.triggers.on_value_changes} --overwrite"
]
}
provisioner "remote-exec" {
when = destroy
inline = [
"${self.triggers.kubectl_cmd} taint node ${self.triggers.server_name} ${self.triggers.taint_name}-"
]
}
}