Skip to content

Commit 4a63550

Browse files
committed
Merge branch 'staging'
2 parents 94b4e68 + 0e2c23e commit 4a63550

File tree

4 files changed

+57
-26
lines changed

4 files changed

+57
-26
lines changed

agents.tf

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,28 @@ module "agents" {
4040
]
4141
}
4242

43-
resource "null_resource" "agents" {
43+
locals {
44+
k3s-agent-config = { for k, v in local.agent_nodes : k => merge(
45+
{
46+
node-name = module.agents[k].name
47+
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"
48+
token = local.k3s_token
49+
kubelet-arg = local.kubelet_arg
50+
flannel-iface = local.flannel_iface
51+
node-ip = module.agents[k].private_ipv4_address
52+
node-label = v.labels
53+
node-taint = v.taints
54+
selinux = true
55+
},
56+
) }
57+
}
58+
59+
resource "null_resource" "agent_config" {
4460
for_each = local.agent_nodes
4561

4662
triggers = {
4763
agent_id = module.agents[each.key].id
64+
config = sha1(yamlencode(local.k3s-agent-config[each.key]))
4865
}
4966

5067
connection {
@@ -57,20 +74,30 @@ resource "null_resource" "agents" {
5774

5875
# Generating k3s agent config file
5976
provisioner "file" {
60-
content = yamlencode({
61-
node-name = module.agents[each.key].name
62-
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"
63-
token = local.k3s_token
64-
kubelet-arg = local.kubelet_arg
65-
flannel-iface = local.flannel_iface
66-
node-ip = module.agents[each.key].private_ipv4_address
67-
node-label = each.value.labels
68-
node-taint = each.value.taints
69-
selinux = true
70-
})
77+
content = yamlencode(local.k3s-agent-config[each.key])
7178
destination = "/tmp/config.yaml"
7279
}
7380

81+
provisioner "remote-exec" {
82+
inline = [local.k3s_config_update_script]
83+
}
84+
}
85+
86+
resource "null_resource" "agents" {
87+
for_each = local.agent_nodes
88+
89+
triggers = {
90+
agent_id = module.agents[each.key].id
91+
}
92+
93+
connection {
94+
user = "root"
95+
private_key = var.ssh_private_key
96+
agent_identity = local.ssh_agent_identity
97+
host = module.agents[each.key].ipv4_address
98+
port = var.ssh_port
99+
}
100+
74101
# Install k3s agent
75102
provisioner "remote-exec" {
76103
inline = local.install_k3s_agent
@@ -94,6 +121,7 @@ resource "null_resource" "agents" {
94121

95122
depends_on = [
96123
null_resource.first_control_plane,
124+
null_resource.agent_config,
97125
hcloud_network_subnet.agent
98126
]
99127
}

control_planes.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ locals {
8787
module.control_planes[k].private_ipv4_address == module.control_planes[keys(module.control_planes)[0]].private_ipv4_address ?
8888
module.control_planes[keys(module.control_planes)[1]].private_ipv4_address :
8989
module.control_planes[keys(module.control_planes)[0]].private_ipv4_address}:6443"
90-
token = random_password.k3s_token.result
90+
token = local.k3s_token
9191
disable-cloud-controller = true
9292
disable = local.disable_extras
9393
kubelet-arg = local.kubelet_arg

docs/terraform.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
| Name | Version |
1515
|------|---------|
16-
| <a name="provider_cloudinit"></a> [cloudinit](#provider\_cloudinit) | n/a |
17-
| <a name="provider_github"></a> [github](#provider\_github) | >= 5.38.0 |
18-
| <a name="provider_hcloud"></a> [hcloud](#provider\_hcloud) | >= 1.43.0 |
19-
| <a name="provider_local"></a> [local](#provider\_local) | >= 2.4.0 |
20-
| <a name="provider_null"></a> [null](#provider\_null) | n/a |
21-
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
22-
| <a name="provider_remote"></a> [remote](#provider\_remote) | >= 0.1.2 |
16+
| <a name="provider_cloudinit"></a> [cloudinit](#provider\_cloudinit) | 2.3.2 |
17+
| <a name="provider_github"></a> [github](#provider\_github) | 5.38.0 |
18+
| <a name="provider_hcloud"></a> [hcloud](#provider\_hcloud) | 1.43.0 |
19+
| <a name="provider_local"></a> [local](#provider\_local) | 2.4.0 |
20+
| <a name="provider_null"></a> [null](#provider\_null) | 3.2.1 |
21+
| <a name="provider_random"></a> [random](#provider\_random) | 3.5.1 |
22+
| <a name="provider_remote"></a> [remote](#provider\_remote) | 0.1.2 |
2323

2424
### Modules
2525

@@ -55,6 +55,7 @@
5555
| [local_file.nginx_values](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
5656
| [local_file.traefik_values](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
5757
| [local_sensitive_file.kubeconfig](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/sensitive_file) | resource |
58+
| [null_resource.agent_config](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
5859
| [null_resource.agents](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
5960
| [null_resource.autoscaled_nodes_registries](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
6061
| [null_resource.configure_autoscaler](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |

locals.tf

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ locals {
101101
})
102102

103103
apply_k3s_selinux = ["/sbin/semodule -v -i /usr/share/selinux/packages/k3s.pp"]
104-
swap_node_label = ["node.kubernetes.io/server-swap=enabled"]
104+
swap_node_label = ["node.kubernetes.io/server-swap=enabled"]
105105

106106
install_k3s_server = concat(local.common_pre_install_k3s_commands, [
107107
"curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_START=true INSTALL_K3S_SKIP_SELINUX_RPM=true INSTALL_K3S_CHANNEL=${var.initial_k3s_channel} INSTALL_K3S_EXEC='server ${var.k3s_exec_server_args}' sh -"
@@ -120,7 +120,7 @@ locals {
120120
labels : concat(local.default_control_plane_labels, nodepool_obj.swap_size != "" ? local.swap_node_label : [], nodepool_obj.labels),
121121
taints : concat(local.default_control_plane_taints, nodepool_obj.taints),
122122
backups : nodepool_obj.backups,
123-
swap_size: nodepool_obj.swap_size,
123+
swap_size : nodepool_obj.swap_size,
124124
index : node_index
125125
}
126126
}
@@ -138,7 +138,7 @@ locals {
138138
labels : concat(local.default_agent_labels, nodepool_obj.swap_size != "" ? local.swap_node_label : [], nodepool_obj.labels),
139139
taints : concat(local.default_agent_taints, nodepool_obj.taints),
140140
backups : lookup(nodepool_obj, "backups", false),
141-
swap_size: nodepool_obj.swap_size,
141+
swap_size : nodepool_obj.swap_size,
142142
index : node_index
143143
}
144144
}
@@ -632,8 +632,10 @@ DATE=`date +%Y-%m-%d_%H-%M-%S`
632632
if cmp -s /tmp/config.yaml /etc/rancher/k3s/config.yaml; then
633633
echo "No update required to the config.yaml file"
634634
else
635-
echo "Backing up /etc/rancher/k3s/config.yaml to /tmp/config_$DATE.yaml"
636-
cp /etc/rancher/k3s/config.yaml /tmp/config_$DATE.yaml
635+
if [ -f "/etc/rancher/k3s/config.yaml" ]; then
636+
echo "Backing up /etc/rancher/k3s/config.yaml to /tmp/config_$DATE.yaml"
637+
cp /etc/rancher/k3s/config.yaml /tmp/config_$DATE.yaml
638+
fi
637639
echo "Updated config.yaml detected, restart of k3s service required"
638640
cp /tmp/config.yaml /etc/rancher/k3s/config.yaml
639641
if systemctl is-active --quiet k3s; then
@@ -643,7 +645,7 @@ else
643645
else
644646
echo "No active k3s or k3s-agent service found"
645647
fi
646-
echo "k3s service or k3s-agent service restarted successfully"
648+
echo "k3s service or k3s-agent service (re)started successfully"
647649
fi
648650
EOF
649651

0 commit comments

Comments
 (0)