theshire/.taskfiles/talos/Taskfile.yaml

149 lines
4.9 KiB
YAML
Raw Permalink Normal View History

---
# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: "3"
tasks:
bootstrap:
desc: Bootstrap Talos
summary: |
Args:
2024-11-06 08:04:05 -06:00
CONTROLLER: Controller node to run command against (required)
prompt: Bootstrap Talos on the '{{.K8S_CLUSTER}}' cluster... continue?
cmds:
- task: bootstrap-etcd
vars: &vars
2024-11-06 08:04:05 -06:00
CONTROLLER: "{{.CONTROLER}}"
- task: fetch-kubeconfig
vars: *vars
- task: bootstrap-integrations
vars: *vars
requires:
vars:
2024-11-06 08:04:05 -06:00
- K8S_CLUSTER
- CONTROLLER
bootstrap-etcd:
desc: Bootstrap Etcd
2024-11-06 08:04:05 -06:00
cmd: until talosctl --nodes {{.CONTROLLER}} bootstrap; do sleep 10; done
requires:
vars:
2024-11-06 08:04:05 -06:00
- CONTROLLER
bootstrap-integrations:
desc: Bootstrap core integrations needed for Talos
cmds:
2024-11-06 08:04:05 -06:00
- until kubectl wait --for=condition=Ready=False nodes --all --timeout=600s; do sleep 10; done
- helmfile --kube-context {{.K8S_CLUSTER}} --file {{.K8S_CLUSTER_DIR}}/bootstrap/helmfile.yaml apply --skip-diff-on-install --suppress-diff
- until kubectl wait --for=condition=Ready nodes --all --timeout=600s; do sleep 10; done
requires:
vars:
- K8S_CLUSTER
preconditions:
- which helmfile
2024-11-06 08:04:05 -06:00
- sh: kubectl config get-contexts {{.K8S_CLUSTER}}
msg: "Kubectl context {{.K8S_CLUSTER}} not found"
- test -f {{.K8S_CLUSTER_DIR}}/bootstrap/helmfile.yaml
fetch-kubeconfig:
desc: Fetch kubeconfig from Talos controllers
cmd: |
2024-11-06 08:04:05 -06:00
talosctl kubeconfig --nodes {{.CONTROLLER}} \
--force --force-context-name {{.K8S_CLUSTER}} {{.K8S_CLUSTER_DIR}}
requires:
vars:
- K8S_CLUSTER
generate-clusterconfig:
desc: Generate clusterconfig for Talos
cmds:
- talhelper genconfig
2024-11-06 08:04:05 -06:00
--env-file {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talenv.sops.yaml
--secret-file {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talsecret.sops.yaml
--config-file {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talconfig.yaml
--out-dir {{.K8S_CLUSTER_DIR}}/bootstrap/talos/clusterconfig
requires:
vars:
- K8S_CLUSTER
preconditions:
2024-11-06 08:04:05 -06:00
- test -f {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talenv.sops.yaml
- test -f {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talsecret.sops.yaml
- test -f {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talconfig.yaml
upgrade:
desc: Upgrade Talos version for a node
vars:
TALOS_VERSION:
sh: |
2024-11-06 08:04:05 -06:00
yq -r ".talosVersion" {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talconfig.yaml
TALOS_IMAGE:
sh: |
talhelper genurl installer \
2024-11-06 08:04:05 -06:00
--env-file {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talenv.sops.yaml \
--config-file {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talconfig.yaml \
| grep {{.NODE}} \
| awk '{split($0,u," "); print u[2]}'
cmds:
2024-11-06 08:04:05 -06:00
- talosctl upgrade -n {{.NODE}} --image {{.TALOS_IMAGE }}
requires:
vars:
2024-11-06 08:04:05 -06:00
- K8S_CLUSTER
- NODE
preconditions:
2024-11-06 08:04:05 -06:00
- test -f {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talenv.sops.yaml
- test -f {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talconfig.yaml
- msg: "Talos image could not be determined for node={{.NODE}}"
sh: 'test -n "{{.TALOS_IMAGE}}"'
upgrade-k8s:
desc: Upgrade Kubernetes version for a Talos cluster
2024-11-06 08:04:05 -06:00
silent: false
vars:
KUBERNETES_VERSION:
sh: |
2024-11-06 08:04:05 -06:00
yq -r ".kubernetesVersion" {{.K8S_CLUSTER_DIR}}/bootstrap/talos/talconfig.yaml
TALOS_CONTROLLER:
sh: talosctl config info --output json | jq --raw-output '.endpoints[]' | shuf -n 1
cmds:
2024-11-06 08:04:05 -06:00
- until kubectl wait --timeout=5m --for=condition=Complete jobs --all --all-namespaces; do sleep 10; done
- talosctl upgrade-k8s -n {{.TALOS_CONTROLLER}} --to {{.KUBERNETES_VERSION}}
requires:
vars:
- K8S_CLUSTER
preconditions:
2024-11-06 08:04:05 -06:00
- talosctl config info &>/dev/null
- talosctl --nodes {{.TALOS_CONTROLLER}} get machineconfig &>/dev/null
apply-clusterconfig:
desc: Apply clusterconfig for a Talos cluster
vars:
CLUSTERCONFIG_FILES:
2024-11-06 08:04:05 -06:00
sh: find {{.K8S_CLUSTER_DIR}}/bootstrap/talos/clusterconfig -type f -name '*.yaml' -printf '%f\n'
cmds:
- for:
var: CLUSTERCONFIG_FILES
task: _apply-machineconfig
vars:
filename: "{{.ITEM}}"
hostname: |-
2024-11-06 08:04:05 -06:00
{{ trimPrefix (printf "%s-" .K8S_CLUSTER) .ITEM | trimSuffix ".yaml" }}
DRY_RUN: "{{ .DRY_RUN }}"
requires:
vars:
- K8S_CLUSTER
_apply-machineconfig:
internal: true
desc: Apply a single Talos machineConfig to a Talos node
cmds:
2024-11-06 08:04:05 -06:00
- talosctl apply-config
--nodes "{{.hostname}}"
2024-11-06 08:04:05 -06:00
--file "{{.K8S_CLUSTER_DIR}}/bootstrap/talos/clusterconfig/{{.filename}}"
{{ if eq "true" .DRY_RUN }}--dry-run{{ end }}
requires:
vars:
2024-11-06 08:04:05 -06:00
- K8S_CLUSTER
- hostname
- filename
preconditions:
2024-11-06 08:04:05 -06:00
- test -f {{.K8S_CLUSTER_DIR}}/bootstrap/talos/clusterconfig/{{.filename}}