forked from hcloud-k8s/terraform-hcloud-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtalos.tf
More file actions
429 lines (366 loc) · 14.1 KB
/
Copy pathtalos.tf
File metadata and controls
429 lines (366 loc) · 14.1 KB
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
locals {
# Talos Version
talos_version_parts = regex("^v?(?P<major>[0-9]+)\\.(?P<minor>[0-9]+)\\.(?P<patch>[0-9]+)", var.talos_version)
talos_version_major = local.talos_version_parts.major
talos_version_minor = local.talos_version_parts.minor
talos_version_patch = local.talos_version_parts.patch
# Talos Nodes
talos_primary_node_name = sort(keys(hcloud_server.control_plane))[0]
talos_primary_node_private_ipv4 = tolist(hcloud_server.control_plane[local.talos_primary_node_name].network)[0].ip
talos_primary_node_public_ipv4 = hcloud_server.control_plane[local.talos_primary_node_name].ipv4_address
talos_primary_node_public_ipv6 = hcloud_server.control_plane[local.talos_primary_node_name].ipv6_address
# Talos API
talos_api_port = 50000
talos_primary_endpoint = var.cluster_access == "private" ? local.talos_primary_node_private_ipv4 : coalesce(
local.talos_primary_node_public_ipv4, local.talos_primary_node_public_ipv6
)
talos_endpoints = compact(
var.cluster_access == "private" ? local.control_plane_private_ipv4_list : concat(
local.network_public_ipv4_enabled ? local.control_plane_public_ipv4_list : [],
local.network_public_ipv6_enabled ? local.control_plane_public_ipv6_list : []
)
)
# Kubernetes API
kube_api_private_ipv4 = (
var.kube_api_load_balancer_enabled ? local.kube_api_load_balancer_private_ipv4 :
var.control_plane_private_vip_ipv4_enabled ? local.control_plane_private_vip_ipv4 :
local.talos_primary_node_private_ipv4
)
kube_api_port = 6443
kube_api_host = coalesce(
var.kube_api_hostname,
var.cluster_access == "private" ? local.kube_api_private_ipv4 : null,
(
var.kube_api_load_balancer_enabled && local.kube_api_load_balancer_public_network_enabled ?
coalesce(local.kube_api_load_balancer_public_ipv4, local.kube_api_load_balancer_public_ipv6) : null
),
var.control_plane_public_vip_ipv4_enabled ? local.control_plane_public_vip_ipv4 : null,
local.talos_primary_node_public_ipv4,
local.talos_primary_node_public_ipv6
)
kube_api_url_internal = "https://${local.kube_api_private_ipv4}:${local.kube_api_port}"
kube_api_url_external = "https://${local.kube_api_host}:${local.kube_api_port}"
# KubePrism
kube_prism_host = "127.0.0.1"
kube_prism_port = 7445
# Talos Control
talosctl_commands = templatefile("${path.module}/templates/talosctl_commands.sh.tftpl", {
talos_upgrade_debug = var.talos_upgrade_debug
talos_upgrade_force = var.talos_upgrade_force
talos_upgrade_insecure = var.talos_upgrade_insecure
talos_upgrade_stage = var.talos_upgrade_stage
talos_upgrade_reboot_mode = var.talos_upgrade_reboot_mode
talos_installer_image_url = local.talos_installer_image_url
talosctl_retries = var.talosctl_retries
healthcheck_enabled = var.cluster_healthcheck_enabled
talos_primary_node = local.talos_primary_node_private_ipv4
kube_api_url = local.kube_api_url_external
kubernetes_version = var.kubernetes_version
control_plane_nodes = local.control_plane_private_ipv4_list
worker_nodes = concat(
local.worker_private_ipv4_list,
local.cluster_autoscaler_private_ipv4_list
)
})
# Cluster Status
cluster_initialized = length(data.hcloud_certificates.state.certificates) > 0
}
data "hcloud_certificates" "state" {
with_selector = join(",",
[
"cluster=${var.cluster_name}",
"state=initialized"
]
)
}
resource "talos_machine_secrets" "this" {
talos_version = var.talos_version
lifecycle {
prevent_destroy = true
}
}
resource "terraform_data" "upgrade_control_plane" {
triggers_replace = [
var.talos_version,
local.talos_schematic_id
]
provisioner "local-exec" {
when = create
quiet = true
command = local.cluster_initialized ? join("\n", [
"set -eu",
local.talosctl_commands,
"printf '%s\\n' \"Start upgrading Control Plane Nodes\"",
templatefile("${path.module}/templates/talos_upgrade.sh.tftpl", {
upgrade_nodes = local.control_plane_private_ipv4_list
talos_version = var.talos_version
talos_schematic_id = local.talos_schematic_id
}),
"printf '%s\\n' \"Control Plane Nodes upgraded successfully\"",
]) : "printf '%s\\n' \"Cluster not initialized, skipping Control Plane Node upgrade\""
environment = {
TALOSCONFIG = nonsensitive(data.talos_client_configuration.this.talos_config)
}
}
depends_on = [
data.external.talosctl_version_check,
data.talos_machine_configuration.control_plane,
data.talos_client_configuration.this
]
}
resource "terraform_data" "upgrade_worker" {
triggers_replace = [
var.talos_version,
local.talos_schematic_id
]
provisioner "local-exec" {
when = create
quiet = true
command = local.cluster_initialized ? join("\n", [
"set -eu",
local.talosctl_commands,
"printf '%s\\n' \"Start upgrading Worker Nodes\"",
templatefile("${path.module}/templates/talos_upgrade.sh.tftpl", {
upgrade_nodes = local.worker_private_ipv4_list
talos_version = var.talos_version
talos_schematic_id = local.talos_schematic_id
}),
"printf '%s\\n' \"Worker Nodes upgraded successfully\"",
]) : "printf '%s\\n' \"Cluster not initialized, skipping Worker Node upgrade\""
environment = {
TALOSCONFIG = nonsensitive(data.talos_client_configuration.this.talos_config)
}
}
depends_on = [
data.external.talosctl_version_check,
data.talos_machine_configuration.worker,
terraform_data.upgrade_control_plane
]
}
resource "terraform_data" "upgrade_cluster_autoscaler" {
count = var.cluster_autoscaler_discovery_enabled ? 1 : 0
triggers_replace = [
var.talos_version,
local.talos_schematic_id
]
provisioner "local-exec" {
when = create
quiet = true
command = local.cluster_initialized ? join("\n", [
"set -eu",
local.talosctl_commands,
"printf '%s\\n' \"Start upgrading Cluster Autoscaler Nodes\"",
templatefile("${path.module}/templates/talos_upgrade.sh.tftpl", {
upgrade_nodes = local.cluster_autoscaler_private_ipv4_list
talos_version = var.talos_version
talos_schematic_id = local.talos_schematic_id
}),
"printf '%s\\n' \"Cluster Autoscaler Nodes upgraded successfully\"",
]) : "printf '%s\\n' \"Cluster not initialized, skipping Cluster Autoscaler Node upgrade\""
environment = {
TALOSCONFIG = nonsensitive(data.talos_client_configuration.this.talos_config)
}
}
depends_on = [
data.external.talosctl_version_check,
data.talos_machine_configuration.cluster_autoscaler,
terraform_data.upgrade_control_plane,
terraform_data.upgrade_worker
]
}
resource "terraform_data" "upgrade_kubernetes" {
triggers_replace = [var.kubernetes_version]
provisioner "local-exec" {
when = create
quiet = true
command = join("\n",
[
"set -eu",
local.cluster_initialized ? join("\n",
[
local.talosctl_commands,
"printf '%s\\n' \"Start upgrading Kubernetes\"",
templatefile("${path.module}/templates/talos_upgrade_k8s.sh.tftpl", {}),
"printf '%s\\n' \"Kubernetes upgraded successfully\"",
]
) : "printf '%s\\n' \"Cluster not initialized, skipping Kubernetes upgrade\"",
])
environment = {
TALOSCONFIG = nonsensitive(data.talos_client_configuration.this.talos_config)
}
}
depends_on = [
data.external.talosctl_version_check,
terraform_data.upgrade_control_plane,
terraform_data.upgrade_worker,
terraform_data.upgrade_cluster_autoscaler
]
}
resource "talos_machine_configuration_apply" "control_plane" {
for_each = { for control_plane in hcloud_server.control_plane : control_plane.name => control_plane }
client_configuration = talos_machine_secrets.this.client_configuration
machine_configuration_input = data.talos_machine_configuration.control_plane[each.key].machine_configuration
endpoint = var.cluster_access == "private" ? tolist(each.value.network)[0].ip : coalesce(each.value.ipv4_address, each.value.ipv6_address)
node = tolist(each.value.network)[0].ip
apply_mode = var.talos_machine_configuration_apply_mode
on_destroy = {
graceful = var.cluster_graceful_destroy
reset = true
reboot = false
}
depends_on = [
hcloud_load_balancer_service.kube_api,
terraform_data.upgrade_kubernetes
]
}
resource "talos_machine_configuration_apply" "worker" {
for_each = { for worker in hcloud_server.worker : worker.name => worker }
client_configuration = talos_machine_secrets.this.client_configuration
machine_configuration_input = data.talos_machine_configuration.worker[each.key].machine_configuration
endpoint = var.cluster_access == "private" ? tolist(each.value.network)[0].ip : coalesce(each.value.ipv4_address, each.value.ipv6_address)
node = tolist(each.value.network)[0].ip
apply_mode = var.talos_machine_configuration_apply_mode
on_destroy = {
graceful = var.cluster_graceful_destroy
reset = true
reboot = false
}
depends_on = [
terraform_data.upgrade_kubernetes,
talos_machine_configuration_apply.control_plane
]
}
resource "terraform_data" "talos_machine_configuration_apply_cluster_autoscaler" {
count = var.cluster_autoscaler_discovery_enabled ? 1 : 0
triggers_replace = [
nonsensitive(sha1(jsonencode({
for k, r in data.talos_machine_configuration.cluster_autoscaler :
k => r.machine_configuration
})))
]
provisioner "local-exec" {
when = create
quiet = true
command = join("\n", [
"set -eu",
local.talosctl_commands,
templatefile("${path.module}/templates/talos_apply_config.sh.tftpl", {
target_nodes = local.cluster_autoscaler_private_ipv4_list
})
])
environment = merge(
{ TALOSCONFIG = nonsensitive(data.talos_client_configuration.this.talos_config) },
{
for server in local.talos_discovery_cluster_autoscaler :
"TALOS_MC_${replace(server.private_ipv4_address, ".", "_")}" =>
nonsensitive(data.talos_machine_configuration.cluster_autoscaler[server.nodepool].machine_configuration)
}
)
}
depends_on = [
data.external.talosctl_version_check,
terraform_data.upgrade_kubernetes,
talos_machine_configuration_apply.control_plane,
talos_machine_configuration_apply.worker
]
}
resource "talos_machine_bootstrap" "this" {
client_configuration = talos_machine_secrets.this.client_configuration
endpoint = local.talos_primary_endpoint
node = local.talos_primary_node_private_ipv4
depends_on = [
talos_machine_configuration_apply.control_plane,
talos_machine_configuration_apply.worker,
terraform_data.talos_machine_configuration_apply_cluster_autoscaler
]
}
resource "terraform_data" "synchronize_manifests" {
triggers_replace = [
nonsensitive(sha1(jsonencode(local.talos_inline_manifests))),
nonsensitive(sha1(jsonencode(local.talos_manifests))),
]
provisioner "local-exec" {
when = create
quiet = true
command = join("\n",
[
"set -eu",
local.cluster_initialized ? join("\n",
[
local.talosctl_commands,
"printf '%s\\n' \"Start synchronizing Kubernetes manifests\"",
templatefile("${path.module}/templates/talos_upgrade_k8s.sh.tftpl", {}),
"printf '%s\\n' \"Kubernetes manifests synchronized successfully\"",
]
) : "printf '%s\\n' \"Cluster not initialized, skipping Kubernetes manifest synchronization\"",
]
)
environment = {
TALOSCONFIG = nonsensitive(data.talos_client_configuration.this.talos_config)
}
}
depends_on = [
data.external.talosctl_version_check,
talos_machine_bootstrap.this,
talos_machine_configuration_apply.control_plane,
talos_machine_configuration_apply.worker,
terraform_data.talos_machine_configuration_apply_cluster_autoscaler
]
}
resource "tls_private_key" "state" {
algorithm = "RSA"
rsa_bits = 2048
}
resource "tls_self_signed_cert" "state" {
private_key_pem = tls_private_key.state.private_key_pem
subject { common_name = var.cluster_name }
allowed_uses = ["server_auth"]
validity_period_hours = 876600
}
resource "hcloud_uploaded_certificate" "state" {
name = "${var.cluster_name}-state"
private_key = tls_private_key.state.private_key_pem
certificate = tls_self_signed_cert.state.cert_pem
labels = {
cluster = var.cluster_name
state = "initialized"
}
depends_on = [terraform_data.synchronize_manifests]
}
resource "terraform_data" "talos_access_data" {
input = {
kube_api_source = local.firewall_kube_api_sources
talos_api_source = local.firewall_talos_api_sources
talos_primary_node = local.talos_primary_node_private_ipv4
endpoints = local.talos_endpoints
control_plane_nodes = local.control_plane_private_ipv4_list
worker_nodes = local.worker_private_ipv4_list
kube_api_url = local.kube_api_url_external
}
}
data "http" "kube_api_health" {
count = var.cluster_healthcheck_enabled ? 1 : 0
url = "${terraform_data.talos_access_data.output.kube_api_url}/version"
insecure = true
retry {
attempts = 60
min_delay_ms = 5000
max_delay_ms = 5000
}
lifecycle {
postcondition {
condition = self.status_code == 401
error_message = "Status code invalid"
}
}
depends_on = [terraform_data.synchronize_manifests]
}
data "talos_cluster_health" "this" {
count = var.cluster_healthcheck_enabled && (var.cluster_access == "private") ? 1 : 0
client_configuration = talos_machine_secrets.this.client_configuration
endpoints = terraform_data.talos_access_data.output.endpoints
control_plane_nodes = terraform_data.talos_access_data.output.control_plane_nodes
worker_nodes = terraform_data.talos_access_data.output.worker_nodes
skip_kubernetes_checks = false
depends_on = [data.http.kube_api_health]
}