Files
home-cluster/.taskfiles/talos/Taskfile.yaml
T
2024-10-29 10:58:40 -04:00

125 lines
4.9 KiB
YAML

---
# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: '3'
vars:
TALHELPER_CLUSTER_DIR: '{{.KUBERNETES_DIR}}/bootstrap/talos/clusterconfig'
TALHELPER_CONFIG_FILE: '{{.KUBERNETES_DIR}}/bootstrap/talos/talconfig.yaml'
TALHELPER_SECRET_FILE: '{{.KUBERNETES_DIR}}/bootstrap/talos/talsecret.sops.yaml'
TALOSCONFIG: '{{.TALHELPER_CLUSTER_DIR}}/talosconfig'
env:
TALOSCONFIG: '{{.TALOSCONFIG}}'
tasks:
generate-config:
desc: Generate Talos configuration
cmd: talhelper genconfig --config-file {{.TALHELPER_CONFIG_FILE}} --secret-file {{.TALHELPER_SECRET_FILE}} --out-dir {{.TALHELPER_CLUSTER_DIR}}
preconditions:
- msg: Missing talhelper config file
sh: test -f {{.TALHELPER_CONFIG_FILE}}
- msg: Missing Sops config file
sh: test -f {{.SOPS_CONFIG_FILE}}
- msg: Missing Sops Age key file
sh: test -f {{.SOPS_AGE_KEY_FILE}}
apply-config:
desc: Apply Talos configuration to a node
cmds:
- '{{if eq .MODE "reboot"}}kubectl drain {{.HOSTNAME}} --ignore-daemonsets --delete-emptydir-data --force{{end}}'
- talosctl apply-config --mode={{.MODE}} --nodes {{.HOSTNAME}} --file {{.TALHELPER_CLUSTER_DIR}}/{{.CLUSTER_NAME}}-{{.HOSTNAME}}.yaml
- talosctl --nodes {{.HOSTNAME}} health --wait-timeout=10m --server=false
- '{{if eq .MODE "reboot"}}kubectl uncordon {{.HOSTNAME}}{{end}}'
vars:
MODE: '{{.MODE | default "no-reboot"}}'
CLUSTER_NAME:
sh: yq '.clusterName' {{.TALHELPER_CONFIG_FILE}}
requires:
vars: [HOSTNAME]
preconditions:
- msg: Missing talosconfig
sh: test -f {{.TALOSCONFIG}}
- msg: Unable to retrieve Talos config
sh: talosctl config info &>/dev/null
- msg: Node not found
sh: talosctl --nodes {{.HOSTNAME}} get machineconfig &>/dev/null
- msg: Talos config for node not found
sh: test -f {{.TALHELPER_CLUSTER_DIR}}/{{.CLUSTER_NAME}}-{{.HOSTNAME}}.yaml
upgrade-node:
desc: Upgrade Talos on a single node
cmds:
- task: '{{if ne .ROLLOUT true}}down{{else}}noop{{end}}'
- talosctl --nodes {{.HOSTNAME}} upgrade --image="factory.talos.dev/installer/{{.TALOS_SCHEMATIC_ID}}:{{.TALOS_VERSION}}" --timeout=10m
- talosctl --nodes {{.HOSTNAME}} health --wait-timeout=10m --server=false
- task: '{{if ne .ROLLOUT true}}up{{else}}noop{{end}}'
vars:
TALOS_SCHEMATIC_ID:
sh: kubectl get node {{.HOSTNAME}} --output=jsonpath='{.metadata.annotations.extensions\.talos\.dev/schematic}'
TALOS_VERSION:
sh: yq '.talosVersion' {{.TALHELPER_CONFIG_FILE}}
requires:
vars: [HOSTNAME]
preconditions:
- msg: Missing talosconfig
sh: test -f {{.TALOSCONFIG}}
- msg: Unable to retrieve Talos config
sh: talosctl config info &>/dev/null
- msg: Node not found
sh: talosctl --nodes {{.HOSTNAME}} get machineconfig &>/dev/null
- msg: Upstream Talos version not found
sh: curl -fsSL -o /dev/null --fail https://github.com/siderolabs/talos/releases/tag/{{.TALOS_VERSION}}
upgrade-cluster:
desc: Upgrade Talos on the whole cluster
cmds:
- task: down
- for: { var: HOSTNAMES }
task: upgrade-node
vars:
HOSTNAME: '{{.ITEM}}'
ROLLOUT: true
- task: up
vars:
HOSTNAMES:
sh: kubectl get nodes --output=jsonpath='{.items[*].metadata.name}'
upgrade-k8s:
desc: Upgrade Kubernetes
cmd: talosctl --nodes {{.KUBERNETES_CONTROLLER}} upgrade-k8s --to {{.KUBERNETES_VERSION}}
vars:
KUBERNETES_CONTROLLER:
sh: talosctl config info --output json | jq --raw-output '.endpoints[]' | shuf -n 1
KUBERNETES_VERSION:
sh: yq '.kubernetesVersion' {{.TALHELPER_CONFIG_FILE}}
preconditions:
- msg: Missing talosconfig
sh: test -f {{.TALOSCONFIG}}
- msg: Unable to retrieve Talos config
sh: talosctl config info &>/dev/null
- msg: Node not found
sh: talosctl --nodes {{.KUBERNETES_CONTROLLER}} get machineconfig &>/dev/null
- msg: Upstream Kubernetes version not found
sh: curl -fsSL -o /dev/null --fail https://github.com/siderolabs/kubelet/releases/tag/{{.KUBERNETES_VERSION}}
reset:
desc: Resets nodes back to maintenance mode
dir: "{{.KUBERNETES_DIR}}/bootstrap/talos"
prompt: This will destroy your cluster and reset the nodes back to maintenance mode... continue?
cmd: talhelper gencommand reset --config-file {{.TALHELPER_CONFIG_FILE}} --out-dir {{.TALHELPER_CLUSTER_DIR}} --extra-flags="--reboot {{- if eq .CLI_FORCE false }} --system-labels-to-wipe STATE --system-labels-to-wipe EPHEMERAL{{ end }} --graceful=false --wait=false" | bash
down:
internal: true
cmd: flux --namespace flux-system suspend kustomization --all
up:
internal: true
cmd: flux --namespace flux-system resume kustomization --all
# Ref: https://github.com/go-task/task/issues/608
noop:
internal: true
silent: true
cmd: noop() { :; }