Skip to content

Commit ce1e967

Browse files
committed
Merge branch 'feature/talosctl-improvements'
2 parents 7cdd862 + b318d0b commit ce1e967

File tree

9 files changed

+290
-85
lines changed

9 files changed

+290
-85
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ Talos Linux is a secure, minimal, and immutable OS for Kubernetes, removing SSH
134134

135135
- [terraform](https://developer.hashicorp.com/terraform/install) or [tofu](https://opentofu.org/docs/intro/install/) to deploy the Cluster
136136
- [packer](https://developer.hashicorp.com/packer/install) to upload Talos Images
137-
- [talosctl](https://www.talos.dev/latest/talos-guides/install/talosctl/) to control the Talos Cluster
137+
- [jq](https://jqlang.org/download/) for internal API Communication
138+
- [talosctl](https://www.talos.dev/latest/talos-guides/install/talosctl) to control the Talos Cluster
138139
- [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl) to control Kubernetes (optional)
139140

140141
> [!IMPORTANT]
@@ -349,7 +350,7 @@ cluster_autoscaler_helm_values = {
349350
##### Talos Upgrades and Configuration Changes
350351
Cluster Autoscaler does not support upgrading nodes or changing their configuration, as its primary purpose is to manage short-lived nodes that handle load peaks. If you require long-lived autoscaled nodes, you can upgrade them manually using `talosctl` or use this Terraform module, which supports discovery of autoscaled nodes and manages their upgrades and configuration changes.
351352

352-
To enable this feature, install [jq](https://jqlang.org/download/) and add the following to your configuration:
353+
To enable this feature, add the following to your configuration:
353354
```hcl
354355
cluster_autoscaler_discovery_enabled = true
355356
```

client.tf

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,39 @@ resource "terraform_data" "create_kubeconfig" {
127127
depends_on = [talos_machine_configuration_apply.control_plane]
128128
}
129129

130+
data "external" "client_prerequisites_check" {
131+
count = var.client_prerequisites_check_enabled ? 1 : 0
132+
133+
program = [
134+
"sh", "-c", <<-EOT
135+
set -eu
136+
137+
missing=0
138+
139+
if ! command -v packer >/dev/null 2>&1; then
140+
printf '\n%s' ' - packer is not installed or not in PATH. Install it at https://developer.hashicorp.com/packer/install' >&2
141+
missing=1
142+
fi
143+
144+
if ! command -v jq >/dev/null 2>&1; then
145+
printf '\n%s' ' - jq is not installed or not in PATH. Install it at https://jqlang.org/download/' >&2
146+
missing=1
147+
fi
148+
149+
if ! command -v talosctl >/dev/null 2>&1; then
150+
printf '\n%s' ' - talosctl is not installed or not in PATH. Install it at https://www.talos.dev/latest/talos-guides/install/talosctl' >&2
151+
missing=1
152+
fi
153+
154+
printf '%s' '{}'
155+
exit "$missing"
156+
EOT
157+
]
158+
}
159+
130160
data "external" "talosctl_version_check" {
161+
count = var.talosctl_version_check_enabled ? 1 : 0
162+
131163
program = [
132164
"sh", "-c", <<-EOT
133165
set -eu
@@ -140,7 +172,7 @@ data "external" "talosctl_version_check" {
140172
r=$${v#*.}
141173
min=$${r%%.*}
142174
patch=$${r#*.}
143-
patch=$${patch%%[^0-9]*}
175+
patch=$${patch%%[!0-9]*}
144176
printf '%s %s %s\n' "$maj" "$min" "$patch"
145177
return 0
146178
;;
@@ -157,18 +189,24 @@ data "external" "talosctl_version_check" {
157189
fi
158190
done
159191
)
160-
[ -n "$parsed_version" ] || { echo "Could not parse talosctl client version" >&2; exit 1; }
192+
193+
if [ -z "$parsed_version" ]; then
194+
printf '%s\n' "Could not parse talosctl client version" >&2
195+
exit 1
196+
fi
161197
162198
set -- $parsed_version; major=$1; minor=$2; patch=$3
163199
if [ "$major" -lt "${local.talos_version_major}" ] ||
164200
{ [ "$major" -eq "${local.talos_version_major}" ] && [ "$minor" -lt "${local.talos_version_minor}" ]; } ||
165201
{ [ "$major" -eq "${local.talos_version_major}" ] && [ "$minor" -eq "${local.talos_version_minor}" ] && [ "$patch" -lt "${local.talos_version_patch}" ]; }
166202
then
167-
echo "talosctl version ($major.$minor.$patch) is lower than Talos target version: ${local.talos_version_major}.${local.talos_version_minor}.${local.talos_version_patch}" >&2
203+
printf '%s\n' "talosctl version ($major.$minor.$patch) is lower than Talos target version: ${local.talos_version_major}.${local.talos_version_minor}.${local.talos_version_patch}" >&2
168204
exit 1
169205
fi
170206
171-
printf '%s\n' "{\"talosctl_version\": \"$major.$minor.$patch\"}"
207+
printf '%s' "{\"talosctl_version\": \"$major.$minor.$patch\"}"
172208
EOT
173209
]
210+
211+
depends_on = [data.external.client_prerequisites_check]
174212
}

image.tf

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ resource "terraform_data" "packer_init" {
117117
working_dir = "${path.module}/packer/"
118118
command = "packer init -upgrade requirements.pkr.hcl"
119119
}
120+
121+
depends_on = [data.external.client_prerequisites_check]
120122
}
121123

122124
resource "terraform_data" "amd64_image" {
@@ -150,7 +152,10 @@ resource "terraform_data" "amd64_image" {
150152
}
151153
}
152154

153-
depends_on = [terraform_data.packer_init]
155+
depends_on = [
156+
data.external.client_prerequisites_check,
157+
terraform_data.packer_init
158+
]
154159
}
155160

156161
resource "terraform_data" "arm64_image" {
@@ -184,7 +189,10 @@ resource "terraform_data" "arm64_image" {
184189
}
185190
}
186191

187-
depends_on = [terraform_data.packer_init]
192+
depends_on = [
193+
data.external.client_prerequisites_check,
194+
terraform_data.packer_init
195+
]
188196
}
189197

190198
data "hcloud_image" "amd64" {

network.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ locals {
3737
worker_private_ipv4_list = compact(distinct([for server in hcloud_server.worker : tolist(server.network)[0].ip]))
3838

3939
# Lists for cluster autoscaler nodes
40-
cluster_autoscaler_public_ipv4_list = compact(distinct([for server in local.cluster_autoscaler_server : server.public_ipv4_address]))
41-
cluster_autoscaler_public_ipv6_list = compact(distinct([for server in local.cluster_autoscaler_server : server.public_ipv6_address]))
42-
cluster_autoscaler_private_ipv4_list = compact(distinct([for server in local.cluster_autoscaler_server : server.private_ipv4_address]))
40+
cluster_autoscaler_public_ipv4_list = compact(distinct([for server in local.talos_discovery_cluster_autoscaler : server.public_ipv4_address]))
41+
cluster_autoscaler_public_ipv6_list = compact(distinct([for server in local.talos_discovery_cluster_autoscaler : server.public_ipv6_address]))
42+
cluster_autoscaler_private_ipv4_list = compact(distinct([for server in local.talos_discovery_cluster_autoscaler : server.private_ipv4_address]))
4343
}
4444

4545
data "hcloud_location" "this" {

packer/image_amd64.pkr.hcl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ build {
5050
<<-EOT
5151
set -euo pipefail
5252
53-
echo 'Zeroing disk first before writing Talos image'
53+
printf '%s\n' 'Zeroing disk first before writing Talos image'
5454
blkdiscard -v /dev/sda 2>/dev/null
5555
56-
echo 'Download Talos ${var.talos_version} image (${var.talos_schematic_id})'
56+
printf '%s\n' 'Download Talos ${var.talos_version} image (${var.talos_schematic_id})'
5757
wget \
5858
--quiet \
5959
--timeout=20 \
@@ -66,7 +66,7 @@ build {
6666
| xz -T0 -dc \
6767
| dd of=/dev/sda bs=1M iflag=fullblock oflag=direct conv=fsync status=none
6868
69-
echo 'Talos ${var.talos_version} image (${var.talos_schematic_id}) downloaded'
69+
printf '%s\n' 'Talos ${var.talos_version} image (${var.talos_schematic_id}) downloaded'
7070
EOT
7171
]
7272
}

packer/image_arm64.pkr.hcl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ build {
5050
<<-EOT
5151
set -euo pipefail
5252
53-
echo 'Zeroing disk first before writing Talos image'
53+
printf '%s\n' 'Zeroing disk first before writing Talos image'
5454
blkdiscard -v /dev/sda 2>/dev/null
5555
56-
echo 'Download Talos ${var.talos_version} image (${var.talos_schematic_id})'
56+
printf '%s\n' 'Download Talos ${var.talos_version} image (${var.talos_schematic_id})'
5757
wget \
5858
--quiet \
5959
--timeout=20 \
@@ -66,7 +66,7 @@ build {
6666
| xz -T0 -dc \
6767
| dd of=/dev/sda bs=1M iflag=fullblock oflag=direct conv=fsync status=none
6868
69-
echo 'Talos ${var.talos_version} image (${var.talos_schematic_id}) downloaded'
69+
printf '%s\n' 'Talos ${var.talos_version} image (${var.talos_schematic_id}) downloaded'
7070
EOT
7171
]
7272
}

server.tf

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ locals {
149149
# documentation 2001:db8::/32, IPv4-mapped ::ffff:0:0/96
150150
ipv6_non_public_pattern = "^(::$|::1$|fe[89ab][0-9a-f]:|f[cd][0-9a-f]*:|ff[0-9a-f]*:|2001:db8:|::ffff:)"
151151

152-
cluster_autoscaler_server = var.cluster_autoscaler_discovery_enabled ? {
153-
for m in jsondecode(data.external.talos_members[0].result.cluster_autoscaler) : m.spec.hostname => {
152+
talos_discovery_cluster_autoscaler = {
153+
for m in jsondecode(data.external.talos_member.result.cluster_autoscaler) : m.spec.hostname => {
154154
nodepool = regex(local.cluster_autoscaler_hostname_pattern, m.spec.hostname)[0]
155155

156156
private_ipv4_address = try(
@@ -183,12 +183,10 @@ locals {
183183
][0], null
184184
)
185185
}
186-
} : {}
186+
}
187187
}
188188

189-
data "external" "talos_members" {
190-
count = var.cluster_autoscaler_discovery_enabled ? 1 : 0
191-
189+
data "external" "talos_member" {
192190
program = [
193191
"sh", "-c", <<-EOT
194192
set -eu
@@ -198,15 +196,30 @@ data "external" "talos_members" {
198196
jq -r '.talosconfig' > "$talosconfig"
199197
200198
if ${local.cluster_initialized}; then
201-
if json=$(talosctl --talosconfig "$talosconfig" get member -n '${terraform_data.talos_access_data.output.talos_primary_node}' -o json); then
202-
printf '%s' "$json" | \
203-
jq -c -s '{cluster_autoscaler: (map(select(.spec.hostname | test("${local.cluster_autoscaler_hostname_pattern}"))) | tostring)}'
199+
if talos_member_json=$(talosctl --talosconfig "$talosconfig" get member -n '${terraform_data.talos_access_data.output.talos_primary_node}' -o json); then
200+
printf '%s' "$talos_member_json" | jq -c -s '{
201+
control_plane: (
202+
map(select(.spec.machineType == "controlplane")) | tostring
203+
),
204+
worker: (
205+
map(select(
206+
.spec.machineType == "worker"
207+
and (.spec.hostname | test("${local.cluster_autoscaler_hostname_pattern}") | not)
208+
)) | tostring
209+
),
210+
cluster_autoscaler: (
211+
map(select(
212+
.spec.machineType == "worker"
213+
and (.spec.hostname | test("${local.cluster_autoscaler_hostname_pattern}"))
214+
)) | tostring
215+
)
216+
}'
204217
else
205-
echo "talosctl failed" >&2
218+
printf '%s\n' "talosctl failed" >&2
206219
exit 1
207220
fi
208221
else
209-
echo '{"cluster_autoscaler": "[]"}'
222+
printf '%s\n' '{"control_plane":"[]","cluster_autoscaler":"[]","worker":"[]"}'
210223
fi
211224
EOT
212225
]
@@ -216,8 +229,10 @@ data "external" "talos_members" {
216229
}
217230

218231
depends_on = [
232+
data.external.client_prerequisites_check,
219233
data.external.talosctl_version_check,
220-
terraform_data.upgrade_control_plane,
221-
terraform_data.upgrade_worker
234+
data.talos_machine_configuration.control_plane,
235+
data.talos_machine_configuration.worker,
236+
data.talos_machine_configuration.cluster_autoscaler
222237
]
223238
}

0 commit comments

Comments
 (0)