From 85c40c5644f1db5ec3c868849531a1d549157777 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Mon, 28 Jul 2025 21:59:18 +0200 Subject: [PATCH] refactor: Add template language to the template file name --- terraform/modules/k8s-cluster/manager-node.tf | 2 +- .../cloud-init-k8s-manager.yaml.tftpl | 91 +++++++++++++++++++ .../cloud-init-k8s-worker.yaml.tftpl | 62 +++++++++++++ terraform/modules/k8s-cluster/worker-node.tf | 2 +- 4 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 terraform/modules/k8s-cluster/templates/cloud-init-k8s-manager.yaml.tftpl create mode 100644 terraform/modules/k8s-cluster/templates/cloud-init-k8s-worker.yaml.tftpl diff --git a/terraform/modules/k8s-cluster/manager-node.tf b/terraform/modules/k8s-cluster/manager-node.tf index 9fd722e..da4dc09 100644 --- a/terraform/modules/k8s-cluster/manager-node.tf +++ b/terraform/modules/k8s-cluster/manager-node.tf @@ -80,7 +80,7 @@ resource "hcloud_server" "manager" { "manager" : "yes" } - user_data = templatefile("${path.module}/templates/cloud-init-k8s-manager.tftpl", { + user_data = templatefile("${path.module}/templates/cloud-init-k8s-manager.yaml.tftpl", { admin_public_ssh_key : format("%s %s", var.admin_ssh_key.public_key, var.admin_ssh_key.name) admin_user : var.admin_user terraform_public_ssh_key : var.terraform_public_ssh_key diff --git a/terraform/modules/k8s-cluster/templates/cloud-init-k8s-manager.yaml.tftpl b/terraform/modules/k8s-cluster/templates/cloud-init-k8s-manager.yaml.tftpl new file mode 100644 index 0000000..bc693a0 --- /dev/null +++ b/terraform/modules/k8s-cluster/templates/cloud-init-k8s-manager.yaml.tftpl @@ -0,0 +1,91 @@ +#cloud-config +users: + - name: ${admin_user} + groups: users, admin, microk8s + sudo: ALL=(ALL) NOPASSWD:ALL + shell: /bin/bash + ssh_authorized_keys: + - ${admin_public_ssh_key} + - name: terraform + groups: users, admin + sudo: ALL=(ALL) NOPASSWD:ALL + ssh_authorized_keys: + - ${terraform_public_ssh_key} +packages: + - linux-generic-hwe-22.04 +package_update: true +package_upgrade: true +package_reboot_if_required: true +write_files: + - owner: root:root + content: "${microk8s_config}" + path: /run/tmpfiles.d/microk8s.yaml + encoding: base64 + defer: true + - owner: root:root + content: "${snapcraft}" + path: /run/tmpfiles.d/snapcraft.yaml + encoding: base64 + defer: true + - owner: root:root + content: "${packages_setup}" + path: /run/tmpfiles.d/install-packages.sh + encoding: base64 + defer: true + - owner: root:root + content: "${firewall_setup}" + path: /run/tmpfiles.d/firewall-setup.sh + encoding: base64 + defer: true + - owner: root:root + content: "${ssh_setup}" + path: /run/tmpfiles.d/ssh-setup.sh + encoding: base64 + defer: true + - owner: root:root + content: "${sysctl_setup}" + path: /run/tmpfiles.d/sysctl-setup.sh + encoding: base64 + defer: true + - owner: root:root + content: "${microk8s_setup}" + path: /run/tmpfiles.d/microk8s-setup.sh + encoding: base64 + defer: true +%{ if main_node ~} + - owner: root:root + content: "${cluster_setup}" + path: /run/tmpfiles.d/cluster-setup.sh + encoding: base64 + defer: true + - owner: root:root + content: "${cluster_setup_values}" + path: /run/tmpfiles.d/cluster-setup-values.yaml + encoding: base64 + defer: true + - owner: root:root + content: "${argocd_ha_values}" + path: /run/tmpfiles.d/argocd-values-ha.yaml + encoding: base64 + defer: true + - owner: root:root + content: "${argocd_values}" + path: /run/tmpfiles.d/argocd-values.yaml + encoding: base64 + defer: true + - owner: root:root + content: "${cilium_values}" + path: /run/tmpfiles.d/cilium-values.yaml + encoding: base64 + defer: true +%{ endif ~} +runcmd: + - install /run/tmpfiles.d/*.sh /usr/local/bin + - /usr/local/bin/install-packages.sh + - /usr/local/bin/firewall-setup.sh + - /usr/local/bin/ssh-setup.sh + - /usr/local/bin/sysctl-setup.sh + - /usr/local/bin/microk8s-setup.sh +%{ if main_node ~} + - sudo /usr/local/bin/cluster-setup.sh +%{ endif ~} \ No newline at end of file diff --git a/terraform/modules/k8s-cluster/templates/cloud-init-k8s-worker.yaml.tftpl b/terraform/modules/k8s-cluster/templates/cloud-init-k8s-worker.yaml.tftpl new file mode 100644 index 0000000..dca9317 --- /dev/null +++ b/terraform/modules/k8s-cluster/templates/cloud-init-k8s-worker.yaml.tftpl @@ -0,0 +1,62 @@ +#cloud-config +users: + - name: ${admin_user} + groups: users, admin, microk8s + sudo: ALL=(ALL) NOPASSWD:ALL + shell: /bin/bash + ssh_authorized_keys: + - ${admin_public_ssh_key} + - name: terraform + groups: users, admin + sudo: ALL=(ALL) NOPASSWD:ALL + ssh_authorized_keys: + - ${terraform_public_ssh_key} +packages: + - linux-generic-hwe-22.04 +package_update: true +package_upgrade: true +package_reboot_if_required: true +write_files: + - owner: root:root + content: "${microk8s_config}" + path: /run/tmpfiles.d/microk8s.yaml + encoding: base64 + defer: true + - owner: root:root + content: "${snapcraft}" + path: /run/tmpfiles.d/snapcraft.yaml + encoding: base64 + defer: true + - owner: root:root + content: "${packages_setup}" + path: /run/tmpfiles.d/install-packages.sh + encoding: base64 + defer: true + - owner: root:root + content: "${firewall_setup}" + path: /run/tmpfiles.d/firewall-setup.sh + encoding: base64 + defer: true + - owner: root:root + content: "${ssh_setup}" + path: /run/tmpfiles.d/ssh-setup.sh + encoding: base64 + defer: true + - owner: root:root + content: "${sysctl_setup}" + path: /run/tmpfiles.d/sysctl-setup.sh + encoding: base64 + defer: true + - owner: root:root + content: "${microk8s_setup}" + path: /run/tmpfiles.d/microk8s-setup.sh + encoding: base64 + defer: true +runcmd: + - install /run/tmpfiles.d/*.sh /usr/local/bin + - /usr/local/bin/install-packages.sh + - /usr/local/bin/firewall-setup.sh + - /usr/local/bin/ssh-setup.sh + - /usr/local/bin/sysctl-setup.sh + - /usr/local/bin/microk8s-setup.sh + diff --git a/terraform/modules/k8s-cluster/worker-node.tf b/terraform/modules/k8s-cluster/worker-node.tf index 6a44929..4af0e5b 100644 --- a/terraform/modules/k8s-cluster/worker-node.tf +++ b/terraform/modules/k8s-cluster/worker-node.tf @@ -59,7 +59,7 @@ resource "hcloud_server" "worker" { "worker" : "yes" } - user_data = templatefile("${path.module}/templates/cloud-init-k8s-worker.tftpl", { + user_data = templatefile("${path.module}/templates/cloud-init-k8s-worker.yaml.tftpl", { admin_public_ssh_key : format("%s %s", var.admin_ssh_key.public_key, var.admin_ssh_key.name) admin_user : var.admin_user terraform_public_ssh_key : var.terraform_public_ssh_key