diff options
Diffstat (limited to 'playbooks.d/k3s-node/playbook.bash')
-rw-r--r-- | playbooks.d/k3s-node/playbook.bash | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/playbooks.d/k3s-node/playbook.bash b/playbooks.d/k3s-node/playbook.bash new file mode 100644 index 0000000..bc041df --- /dev/null +++ b/playbooks.d/k3s-node/playbook.bash @@ -0,0 +1,98 @@ +#!/usr/bin/env bash + +BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.entry.host]="required" +BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.internal-ip]="required" + +playbook_add() { + local host + local role + local token + + host="$(config "$BASHTARD_PLAYBOOK.entry.host")" + role="$(config "$BASHTARD_PLAYBOOK.role" "agent")" + token="$(config "$BASHTARD_PLAYBOOK.entry.token" "")" + + pkg install curl + + # If token is not supplied manually, attempt to fetch it from the entry + # host over ssh + if [[ -z "$token" ]] + then + # Dealing with an IPv6 address, strip away the brackets + if [[ "${host:0:1}" == "[" && "${host: -1}" == "]" ]] + then + host="${host:1:-1}" + fi + + info "$BASHTARD_PLAYBOOK/add" "Attempting to fetch token from $host" + token="$(ssh "$host" -- cat /var/lib/rancher/k3s/server/token)" + fi + + if [[ -z "$token" ]] + then + emerg "$BASHTARD_PLAYBOOK/add" "No entry token found, set one with bashtard var -s $BASHTARD_PLAYBOOK.entry.token <value>" + return 1 + fi + + info "$BASHTARD_PLAYBOOK/add" "Writing config.yaml for k3s" + mkdir -pv -- /etc/rancher/k3s + { + cat <<-EOF + node-name: "${BASHTARD_PLATFORM[fqdn]}" + node-ip: "$(config "$BASHTARD_PLAYBOOK.internal-ip" "127.0.0.1")" + server: "https://$(config "$BASHTARD_PLAYBOOK.entry.host"):$(config "$BASHTARD_PLAYBOOK.entry.port" "6443")" + token: "$token" + EOF + + if [[ -n "$(config_subkeys "$BASHTARD_PLAYBOOK.flannel.interfaces")" ]] + then + printf "flannel-iface:\n" + + while read -r iface + do + printf -- "- \"%s\"\n" "$(config "$BASHTARD_PLAYBOOK.flannel.interfaces.$iface")" + done < <( config_subkeys "$BASHTARD_PLAYBOOK.flannel.interfaces" ) + fi + + if [[ -n "$(config "$BASHTARD_PLAYBOOK.external-ip" "")" ]] + then + printf "%s: \"%s\"\n" "node-external-ip" "$(config "$BASHTARD_PLAYBOOK.external-ip" "")" + fi + + if [[ "$role" == "server" ]] + then + info "$BASHTARD_PLAYBOOK/add" "Node is a server, adding master configuration" + cat <<-EOF >> /etc/rancher/k3s/config.yaml + cluster-cidr: "$(config "$BASHTARD_PLAYBOOK.cluster-cidr" "172.19.0.0/16")" + cluster-domain: "$(config "$BASHTARD_PLAYBOOK.cluster-domain" "cluster.local")" + service-cidr: "$(config "$BASHTARD_PLAYBOOK.service-cidr" "172.20.0.0/16")" + service-node-port-range: "$(config "$BASHTARD_PLAYBOOK.service-node-port-min" "30000")-$(config "$BASHTARD_PLAYBOOK.service-node-port-max" "32767")" + flannel-ipv6-masq: true + disable: + - traefik + EOF + fi + } > "$(config "fs.etcdir")/rancher/k3s/config.yaml" + + info "$BASHTARD_PLAYBOOK/add" "Installing k3s" + curl -sfL https://get.k3s.io | sh -s - "$role" +} + +playbook_sync() { + :; +} + +playbook_del() { + local role + + role="$(config "$BASHTARD_PLAYBOOK.role" "agent")" + + case "$role" in + server) + /usr/local/bin/k3s-uninstall.sh + ;; + agent) + /usr/local/bin/k3s-agent-uninstall.sh + ;; + esac +} |