forked from kube-hetzner/terraform-hcloud-kube-hetzner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoscaler-agents.tf
177 lines (157 loc) · 7.09 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
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
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
)
imageList = {
arm64 : tostring(data.hcloud_image.microos_arm_snapshot.id)
amd64 : tostring(data.hcloud_image.microos_x86_snapshot.id)
}
nodeConfigName = var.use_cluster_name_in_node_name ? "${var.cluster_name}-" : ""
cluster_config = {
imagesForArch : local.imageList
nodeConfigs : {
for index, nodePool in var.autoscaler_nodepools :
("${local.nodeConfigName}${nodePool.name}") => {
cloudInit = data.cloudinit_config.autoscaler_config[index].rendered
labels = nodePool.labels
taints = nodePool.taints
}
}
}
isUsingLegacyConfig = length(var.autoscaler_labels) > 0 || length(var.autoscaler_taints) > 0
autoscaler_yaml = length(var.autoscaler_nodepools) == 0 ? "" : templatefile(
"${path.module}/templates/autoscaler.yaml.tpl",
{
cloudinit_config = local.isUsingLegacyConfig ? base64encode(data.cloudinit_config.autoscaler_legacy_config[0].rendered) : ""
ca_image = var.cluster_autoscaler_image
ca_version = var.cluster_autoscaler_version
cluster_autoscaler_extra_args = var.cluster_autoscaler_extra_args
cluster_autoscaler_log_level = var.cluster_autoscaler_log_level
cluster_autoscaler_log_to_stderr = var.cluster_autoscaler_log_to_stderr
cluster_autoscaler_stderr_threshold = var.cluster_autoscaler_stderr_threshold
cluster_autoscaler_server_creation_timeout = tostring(var.cluster_autoscaler_server_creation_timeout)
ssh_key = local.hcloud_ssh_key_id
ipv4_subnet_id = data.hcloud_network.k3s.id
snapshot_id = local.first_nodepool_snapshot_id
cluster_config = base64encode(jsonencode(local.cluster_config))
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 = ["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)
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 = local.k3s_token
kubelet-arg = concat(local.kubelet_arg, var.k3s_global_kubelet_args, var.k3s_autoscaler_kubelet_args, var.autoscaler_nodepools[count.index].kubelet_args)
flannel-iface = local.flannel_iface
node-label = concat(local.default_agent_labels, [for k, v in var.autoscaler_nodepools[count.index].labels : "${k}=${v}"])
node-taint = concat(local.default_agent_taints, [for taint in var.autoscaler_nodepools[count.index].taints : "${taint.key}=${taint.value}:${taint.effect}"])
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 "cloudinit_config" "autoscaler_legacy_config" {
count = length(var.autoscaler_nodepools) > 0 && local.isUsingLegacyConfig ? 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 = local.k3s_token
kubelet-arg = local.kubelet_arg
flannel-iface = local.flannel_iface
node-label = concat(local.default_agent_labels, var.autoscaler_labels)
node-taint = concat(local.default_agent_taints, var.autoscaler_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]
}
}