summaryrefslogtreecommitdiff
path: root/playbooks.d/k3s-node/playbook.bash
blob: bcf49302f5594198b07697168cecbccc26747254 (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
#!/usr/bin/env bash

BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.entry.host]="required"
BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.entry.token]="required"

playbook_add() {
	local role

	role="$(config "$BASHTARD_PLAYBOOK.role" "agent")"

	pkg install curl

	info "$BASHTARD_PLAYBOOK" "Writing config.yaml for k3s"
	mkdir -pv -- /etc/rancher/k3s
	cat <<-EOF > /etc/rancher/k3s/config.yaml
	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: "$(config "$BASHTARD_PLAYBOOK.entry.token")"
	EOF

	if [[ -n "$(config "$BASHTARD_PLAYBOOK.external-ip" "")" ]]
	then
		printf "%s: %s\n" "node-external-ip" "$(config "$BASHTARD_PLAYBOOK.external-ip" "")"\
			>> /etc/rancher/k3s/config.yaml
	fi

	if [[ "$role" == "server" ]]
	then
		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

	info "$BASHTARD_PLAYBOOK" "Installing k3s"
	curl -sfL https://get.k3s.io | sh -s - "$role"

	notice "$BASHTARD_PLAYBOOK" "Waiting for node to become available"
	{ grep -q -m 1 "${BASHTARD_PLATFORM[fqdn]}[[:space:]]\+Ready"; kill $!; } < <(k3s kubectl get node -w)
}

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
}