forked from kube-hetzner/terraform-hcloud-kube-hetzner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoscaler-agents.tf
121 lines (108 loc) · 4 KB
/
autoscaler-agents.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
locals {
cluster_prefix = var.use_cluster_name_in_node_name ? "${var.cluster_name}-" : ""
first_nodepool_snapshot_id = length(var.autoscaler_nodepools) == 0 ? "" : (
substr(var.autoscaler_nodepools[0].server_type, 0, 3) == "cax" ? data.hcloud_image.microos_arm_snapshot.id : data.hcloud_image.microos_x86_snapshot.id
)
autoscaler_yaml = length(var.autoscaler_nodepools) == 0 ? "" : templatefile(
"${path.module}/templates/autoscaler.yaml.tpl",
{
cloudinit_config = base64encode(data.cloudinit_config.autoscaler-config[0].rendered)
ca_image = var.cluster_autoscaler_image
ca_version = var.cluster_autoscaler_version
ssh_key = local.hcloud_ssh_key_id
ipv4_subnet_id = hcloud_network.k3s.id
snapshot_id = local.first_nodepool_snapshot_id
firewall_id = hcloud_firewall.k3s.id
cluster_name = local.cluster_prefix
node_pools = var.autoscaler_nodepools
})
# A concatenated list of all autoscaled nodes
autoscaled_nodes = length(var.autoscaler_nodepools) == 0 ? {} : {
for v in concat([
for k, v in data.
hcloud_servers.autoscaled_nodes : [for v in v.servers : v]
]...) : v.name => v
}
}
resource "null_resource" "configure_autoscaler" {
count = length(var.autoscaler_nodepools) > 0 ? 1 : 0
triggers = {
template = local.autoscaler_yaml
}
connection {
user = "root"
private_key = var.ssh_private_key
agent_identity = local.ssh_agent_identity
host = module.control_planes[keys(module.control_planes)[0]].ipv4_address
port = var.ssh_port
}
# Upload the autoscaler resource defintion
provisioner "file" {
content = local.autoscaler_yaml
destination = "/tmp/autoscaler.yaml"
}
# Create/Apply the definition
provisioner "remote-exec" {
inline = [
"set -ex",
"kubectl apply -f /tmp/autoscaler.yaml",
]
}
depends_on = [
null_resource.kustomization,
data.hcloud_image.microos_x86_snapshot
]
}
data "cloudinit_config" "autoscaler-config" {
count = length(var.autoscaler_nodepools) > 0 ? 1 : 0
gzip = true
base64_encode = true
# Main cloud-config configuration file.
part {
filename = "init.cfg"
content_type = "text/cloud-config"
content = templatefile(
"${path.module}/templates/autoscaler-cloudinit.yaml.tpl",
{
hostname = "autoscaler"
sshAuthorizedKeys = concat([var.ssh_public_key], var.ssh_additional_public_keys)
k3s_config = yamlencode({
server = "https://${var.use_control_plane_lb ? hcloud_load_balancer_network.control_plane.*.ip[0] : module.control_planes[keys(module.control_planes)[0]].private_ipv4_address}:6443"
token = random_password.k3s_token.result
kubelet-arg = local.kubelet_arg
flannel-iface = local.flannel_iface
node-label = local.default_agent_labels
node-taint = local.default_agent_taints
selinux = true
})
install_k3s_agent_script = join("\n", concat(local.install_k3s_agent, ["systemctl start k3s-agent"]))
cloudinit_write_files_common = local.cloudinit_write_files_common
cloudinit_runcmd_common = local.cloudinit_runcmd_common
}
)
}
}
data "hcloud_servers" "autoscaled_nodes" {
for_each = toset(var.autoscaler_nodepools[*].name)
with_selector = "hcloud/node-group=${local.cluster_prefix}${each.value}"
}
resource "null_resource" "autoscaled_nodes_registries" {
for_each = local.autoscaled_nodes
triggers = {
registries = var.k3s_registries
}
connection {
user = "root"
private_key = var.ssh_private_key
agent_identity = local.ssh_agent_identity
host = each.value.ipv4_address
port = var.ssh_port
}
provisioner "file" {
content = var.k3s_registries
destination = "/tmp/registries.yaml"
}
provisioner "remote-exec" {
inline = [local.k3s_registries_update_script]
}
}