From 83e1c95b06231a54cbb92bdefde312fcee438270 Mon Sep 17 00:00:00 2001 From: Vegard Hagen Date: Sun, 23 Mar 2025 12:36:38 +0100 Subject: [PATCH] refactor(talos): condensate machine config into two files Signed-off-by: Vegard Hagen --- tofu/kubernetes/main.tf | 2 +- tofu/kubernetes/talos/config.tf | 50 ++++++++----------- .../talos/machine-config/common.yaml.tftpl | 21 ++++++-- .../machine-config/network-no-vip.yaml.tftpl | 12 ----- .../machine-config/network-vip.yaml.tftpl | 14 ------ tofu/kubernetes/talos/variables.tf | 16 +++--- 6 files changed, 47 insertions(+), 68 deletions(-) delete mode 100644 tofu/kubernetes/talos/machine-config/network-no-vip.yaml.tftpl delete mode 100644 tofu/kubernetes/talos/machine-config/network-vip.yaml.tftpl diff --git a/tofu/kubernetes/main.tf b/tofu/kubernetes/main.tf index 3efdd1b..4ba6116 100644 --- a/tofu/kubernetes/main.tf +++ b/tofu/kubernetes/main.tf @@ -25,7 +25,7 @@ module "talos" { # Not sure how it works if connected to the same router via ethernet (does it act as a switch then???) # Ref: https://www.talos.dev/v1.9/talos-guides/network/vip/#requirements # Note This is Kubernetes API endpoint. Different from all mentions of Talos endpoints. - endpoint = "192.168.1.102" + endpoint = "192.168.1.102" # Omit this if devices are not connected on layer 2 vip = "192.168.1.99" gateway = "192.168.1.1" diff --git a/tofu/kubernetes/talos/config.tf b/tofu/kubernetes/talos/config.tf index 8f79095..7b3db9f 100644 --- a/tofu/kubernetes/talos/config.tf +++ b/tofu/kubernetes/talos/config.tf @@ -20,32 +20,22 @@ data "talos_machine_configuration" "this" { talos_version = var.cluster.talos_version machine_type = each.value.machine_type machine_secrets = talos_machine_secrets.this.machine_secrets - config_patches = [ + config_patches = [ templatefile("${path.module}/machine-config/common.yaml.tftpl", { - node_name = each.value.host_node - cluster_name = var.cluster.proxmox_cluster - }) - , each.value.machine_type == "controlplane" ? - templatefile("${path.module}/machine-config/control-plane.yaml.tftpl", { - cilium_values = var.cilium.values - cilium_install = var.cilium.install - base_domain = var.cluster.base_domain - }) : "" - , (each.value.machine_type == "controlplane" && var.cluster.vip != null) - ? templatefile("${path.module}/machine-config/network-vip.yaml.tftpl", { - hostname = each.key - ip = each.value.ip - mac_address = lower(each.value.mac_address) - gateway = var.cluster.gateway - subnet_mask = var.cluster.subnet_mask - vip = var.cluster.vip - }) : templatefile("${path.module}/machine-config/network-no-vip.yaml.tftpl", { - hostname = each.key - ip = each.value.ip - mac_address = lower(each.value.mac_address) - gateway = var.cluster.gateway - subnet_mask = var.cluster.subnet_mask - }) + node_name = each.value.host_node + cluster_name = var.cluster.proxmox_cluster + hostname = each.key + ip = each.value.ip + mac_address = lower(each.value.mac_address) + gateway = var.cluster.gateway + subnet_mask = var.cluster.subnet_mask + vip = var.cluster.vip + }), each.value.machine_type == "controlplane" ? + templatefile("${path.module}/machine-config/control-plane.yaml.tftpl", { + cilium_values = var.cilium.values + cilium_install = var.cilium.install + base_domain = var.cluster.base_domain + }) : "" ] } @@ -62,7 +52,7 @@ resource "talos_machine_configuration_apply" "this" { } resource "talos_machine_bootstrap" "this" { - depends_on = [talos_machine_configuration_apply.this] + depends_on = [talos_machine_configuration_apply.this] # Bootstrap with the first node. VIP not yet available at this stage, so cant use var.cluster.endpoint as it may be set to VIP # ref - https://www.talos.dev/v1.9/talos-guides/network/vip/#caveats node = [for k, v in var.nodes : v.ip if v.machine_type == "controlplane"][0] @@ -75,10 +65,10 @@ data "talos_cluster_health" "this" { talos_machine_bootstrap.this ] skip_kubernetes_checks = false - client_configuration = data.talos_client_configuration.this.client_configuration - control_plane_nodes = [for k, v in var.nodes : v.ip if v.machine_type == "controlplane"] - worker_nodes = [for k, v in var.nodes : v.ip if v.machine_type == "worker"] - endpoints = data.talos_client_configuration.this.endpoints + client_configuration = data.talos_client_configuration.this.client_configuration + control_plane_nodes = [for k, v in var.nodes : v.ip if v.machine_type == "controlplane"] + worker_nodes = [for k, v in var.nodes : v.ip if v.machine_type == "worker"] + endpoints = data.talos_client_configuration.this.endpoints timeouts = { read = "10m" } diff --git a/tofu/kubernetes/talos/machine-config/common.yaml.tftpl b/tofu/kubernetes/talos/machine-config/common.yaml.tftpl index 5fb0204..6ad0a58 100644 --- a/tofu/kubernetes/talos/machine-config/common.yaml.tftpl +++ b/tofu/kubernetes/talos/machine-config/common.yaml.tftpl @@ -1,4 +1,22 @@ machine: + nodeLabels: + topology.kubernetes.io/region: ${cluster_name} + topology.kubernetes.io/zone: ${node_name} + network: + hostname: ${hostname} + interfaces: + - deviceSelector: + hardwareAddr: ${mac_address} + addresses: + - ${ip}/${subnet_mask} + routes: + - network: 0.0.0.0/0 + gateway: ${gateway} + dhcp: false +%{ if vip != null } + vip: + ip: ${vip} +%{ endif } sysctls: fs.inotify.max_user_watches: 1048576 # Watchdog fs.inotify.max_user_instances: 8192 # Watchdog @@ -12,6 +30,3 @@ machine: net.ipv4.tcp_wmem: 4096 65536 33554432 # 10Gb/s net.ipv4.tcp_window_scaling: 1 # 10Gb/s vm.nr_hugepages: 1024 # PostgreSQL - nodeLabels: - topology.kubernetes.io/region: ${cluster_name} - topology.kubernetes.io/zone: ${node_name} diff --git a/tofu/kubernetes/talos/machine-config/network-no-vip.yaml.tftpl b/tofu/kubernetes/talos/machine-config/network-no-vip.yaml.tftpl deleted file mode 100644 index 5cc4191..0000000 --- a/tofu/kubernetes/talos/machine-config/network-no-vip.yaml.tftpl +++ /dev/null @@ -1,12 +0,0 @@ -machine: - network: - hostname: ${hostname} - interfaces: - - deviceSelector: - hardwareAddr: ${mac_address} - addresses: - - ${ip}/${subnet_mask} - routes: - - network: 0.0.0.0/0 - gateway: ${gateway} - dhcp: false diff --git a/tofu/kubernetes/talos/machine-config/network-vip.yaml.tftpl b/tofu/kubernetes/talos/machine-config/network-vip.yaml.tftpl deleted file mode 100644 index 32c3e5f..0000000 --- a/tofu/kubernetes/talos/machine-config/network-vip.yaml.tftpl +++ /dev/null @@ -1,14 +0,0 @@ -machine: - network: - hostname: ${hostname} - interfaces: - - deviceSelector: - hardwareAddr: ${mac_address} - addresses: - - ${ip}/${subnet_mask} - routes: - - network: 0.0.0.0/0 - gateway: ${gateway} - dhcp: false - vip: - ip: ${vip} diff --git a/tofu/kubernetes/talos/variables.tf b/tofu/kubernetes/talos/variables.tf index 0751748..c2089c9 100644 --- a/tofu/kubernetes/talos/variables.tf +++ b/tofu/kubernetes/talos/variables.tf @@ -15,15 +15,15 @@ variable "image" { variable "cluster" { description = "Cluster configuration" type = object({ - name = string - endpoint = string - vip = optional(string) - gateway = string - subnet_mask = optional(string, "24") - talos_version = string - proxmox_cluster = string + name = string + endpoint = string + vip = optional(string) + gateway = string + subnet_mask = optional(string, "24") + talos_version = string + proxmox_cluster = string kubernetes_version = string - base_domain = string + base_domain = string }) }