From f61411dd2e2802ffcf87403a292f06a06b9ea866 Mon Sep 17 00:00:00 2001 From: Atanas Dinov Date: Wed, 26 Jun 2024 21:14:58 +0300 Subject: [PATCH] Apply manifests in k3s templates Signed-off-by: Atanas Dinov --- .../templates/k3s-multi-node-installer.sh.tpl | 32 ++++++++++++++++--- .../k3s-single-node-installer.sh.tpl | 32 ++++++++++++++++--- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/pkg/combustion/templates/k3s-multi-node-installer.sh.tpl b/pkg/combustion/templates/k3s-multi-node-installer.sh.tpl index aa0d59b0..befd950d 100644 --- a/pkg/combustion/templates/k3s-multi-node-installer.sh.tpl +++ b/pkg/combustion/templates/k3s-multi-node-installer.sh.tpl @@ -27,19 +27,43 @@ mount /var mkdir -p /var/lib/rancher/k3s/agent/images/ cp {{ .imagesPath }}/* /var/lib/rancher/k3s/agent/images/ +umount /var + CONFIGFILE={{ .configFilePath }}/$NODETYPE.yaml if [ "$HOSTNAME" = {{ .initialiser }} ]; then CONFIGFILE={{ .configFilePath }}/{{ .initialiserConfigFile }} {{- if .manifestsPath }} - mkdir -p /var/lib/rancher/k3s/server/manifests/ - cp {{ .manifestsPath }}/* /var/lib/rancher/k3s/server/manifests/ + mkdir -p /opt/k8s/manifests + cp {{ .manifestsPath }}/* /opt/k8s/manifests/ + + cat <<- EOF > /etc/systemd/system/kubernetes-resources-install.service + [Unit] + Description=Kubernetes Resources Install + Requires=k3s.service + After=k3s.service + ConditionPathExists=/opt/bin/kubectl + ConditionPathExists=/etc/rancher/k3s/k3s.yaml + + [Install] + WantedBy=multi-user.target + + [Service] + Type=oneshot + Restart=on-failure + RestartSec=30 + ExecStart=/opt/bin/kubectl apply -f /opt/k8s/manifests --kubeconfig=/etc/rancher/k3s/k3s.yaml + # Disable the service and clean up + ExecStartPost=/bin/sh -c "systemctl disable kubernetes-resources-install.service" + ExecStartPost=rm -f /etc/systemd/system/kubernetes-resources-install.service + ExecStartPost=rm -rf /opt/k8s + EOF + + systemctl enable kubernetes-resources-install.service {{- end }} fi -umount /var - {{- if and .apiVIP .apiHost }} echo "{{ .apiVIP }} {{ .apiHost }}" >> /etc/hosts {{- end }} diff --git a/pkg/combustion/templates/k3s-single-node-installer.sh.tpl b/pkg/combustion/templates/k3s-single-node-installer.sh.tpl index 4ae281bb..4460177e 100644 --- a/pkg/combustion/templates/k3s-single-node-installer.sh.tpl +++ b/pkg/combustion/templates/k3s-single-node-installer.sh.tpl @@ -6,13 +6,37 @@ mount /var mkdir -p /var/lib/rancher/k3s/agent/images/ cp {{ .imagesPath }}/* /var/lib/rancher/k3s/agent/images/ +umount /var + {{- if .manifestsPath }} -mkdir -p /var/lib/rancher/k3s/server/manifests/ -cp {{ .manifestsPath }}/* /var/lib/rancher/k3s/server/manifests/ +mkdir -p /opt/k8s/manifests +cp {{ .manifestsPath }}/* /opt/k8s/manifests/ + +cat <<- EOF > /etc/systemd/system/kubernetes-resources-install.service +[Unit] +Description=Kubernetes Resources Install +Requires=k3s.service +After=k3s.service +ConditionPathExists=/opt/bin/kubectl +ConditionPathExists=/etc/rancher/k3s/k3s.yaml + +[Install] +WantedBy=multi-user.target + +[Service] +Type=oneshot +Restart=on-failure +RestartSec=30 +ExecStart=/opt/bin/kubectl apply -f /opt/k8s/manifests --kubeconfig=/etc/rancher/k3s/k3s.yaml +# Disable the service and clean up +ExecStartPost=/bin/sh -c "systemctl disable kubernetes-resources-install.service" +ExecStartPost=rm -f /etc/systemd/system/kubernetes-resources-install.service +ExecStartPost=rm -rf /opt/k8s +EOF + +systemctl enable kubernetes-resources-install.service {{- end }} -umount /var - {{- if and .apiVIP .apiHost }} echo "{{ .apiVIP }} {{ .apiHost }}" >> /etc/hosts {{- end }}