summaryrefslogtreecommitdiff
path: root/playbooks.d/k3s-node/playbook.bash
blob: 0cf54c2c89a997702a9b857d60b671ca42fd54fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/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")"
		EOF

		if [[ -n "$(config "$BASHTARD_PLAYBOOK.flannel-iface" "")" ]]
		then
			printf "flannel-iface: \"%s\"\n" "$(config "$BASHTARD_PLAYBOOK.flannel-iface")"
		fi

		if [[ -n "$(config "$BASHTARD_PLAYBOOK.external-ip" "")" ]]
		then
			printf "node-external-ip: \"%s\"\n" "$(config "$BASHTARD_PLAYBOOK.external-ip")"
		fi

		if [[ "$role" == "server" ]]
		then
			info "$BASHTARD_PLAYBOOK/add" "Node is a server, adding master configuration"
			cat <<-EOF
			bind-address: "$(config "$BASHTARD_PLAYBOOK.bind-address" "0.0.0.0")"
			cluster-cidr: "$(config "$BASHTARD_PLAYBOOK.cluster-cidr" "172.19.0.0/16")"
			cluster-domain: "$(config "$BASHTARD_PLAYBOOK.cluster-domain" "cluster.local")"
			flannel-backend: wireguard-native
			flannel-ipv6-masq: true
			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")"
			disable:
			- traefik
			EOF
		fi

		printf "\n"
		printf "server: %s\n" "https://$(config "$BASHTARD_PLAYBOOK.entry.host"):$(config "$BASHTARD_PLAYBOOK.entry.port" "6443")"
		printf "token: %s\n" "$token"
	} > "$(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
}