-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubeainer.sh
executable file
·125 lines (111 loc) · 3.1 KB
/
kubeainer.sh
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
set -eu
KUBEAINER_APPS=${KUBEAINER_APPS:-/etc/kubeainer/apps}
usage() {
echo "Usage: $0 {install [APP...]|kubeconfig}" >&2
}
# Args: MSG
die() {
echo "ERROR: $1" >&2
exit 1
}
# Args: SERVICE WAITMAXSECONDS
waitForSystemdService() {
START_TIME=`date +%s`
systemctl status $1 >/dev/null 2>&1 && return 0 || true
printf 'Waiting for %s' $1
for i in $(seq 0 $2); do
sleep 1
printf .
systemctl status $1 >/dev/null && printf ' [completed in %ds]\n' $(expr $(date +%s) - $START_TIME) && return 0 || ! systemctl is-failed $1 >/dev/null \
|| (printf '\n\nERROR: Service %s failed to start! Dumping logs...\n\n' $1 >&2; journalctl -u $1 | sed -E 's/^/ /g'; false) || return 1
done
printf '\n\nERROR: Timed out after %ds waiting for service %s to start! Dumping logs...\n\n' $2 $1 >&2
journalctl -u $1 | sed -E 's/^/ /g'
return 1
}
waitForSystemdServices() {
waitForSystemdService crio 20
waitForSystemdService kubeadm 240
}
waitForNodes() {
echo Waiting for node to become ready
# use mounted kubeconfig secret when not running on master node
[ -f /output/kubeconfig.yaml ] || export KUBECONFIG=/secrets/kubeconfig.yaml
kubectl wait --for condition=ready --timeout 160s node/$(cat /etc/hostname) >/dev/null \
|| (echo kube-system pods:; kubectl -n kube-system get pods; false) \
|| (echo nodes:; kubectl get nodes; false) \
|| die "node $(cat /etc/hostname) did not become ready!"
}
# ARGS: SECONDS CMD...
retry() {
SECONDS="$1"
shift
for i in $(seq 0 "$SECONDS"); do
"$@" >/dev/null 2>&1 && return 0
sleep 1
done
echo "ERROR: timed out after $SECONDS attempts: $@" >&2
"$@"
}
exportKubeconfig() {
cp -f /etc/kubernetes/admin.conf /output/kubeconfig.yaml
chown $(stat -c '%u' /output) /output/kubeconfig.yaml
}
installApps() {
for APP in "$@"; do
installApp "$APP"
done
}
#installHostDNS() {
# [ -f /host/etc/systemd/resolv.conf ] || die /host/etc/systemd/resolv.conf is not present
#}
# ARGS: APP_NAME_OR_DIR
appDir() {
APP_DIR="$1"
[ -d "$APP_DIR" ] || APP_DIR="$KUBEAINER_APPS/$1"
[ -d "$APP_DIR" ] || (echo "ERROR: App '$1' not found! Supported apps: $(ls $KUBEAINER_APPS | xargs)" >&2; false)
echo "$APP_DIR"
}
# ARG: MANIFEST_DIR
installApp() {
APP_DIR="$(appDir "$1")"
echo Installing $1
[ -f "$APP_DIR/inventory-template.yaml" ] || kpt live init "$APP_DIR"
kpt live apply "$APP_DIR" >/dev/null 2>&1 || kpt live apply "$APP_DIR"
[ ! -f "$APP_DIR/post-apply-hook.sh" ] || (cd "$APP_DIR" && sh ./post-apply-hook.sh)
kpt live status --poll-until=current --timeout=90s "$APP_DIR" >/dev/null || die "Failed to deploy app $1!"
[ ! -f "$APP_DIR/post-install-hook.sh" ] || (cd "$APP_DIR" && sh ./post-install-hook.sh)
}
case "${1:-}" in
install)
shift
waitForSystemdServices
waitForNodes
installApps "$@"
echo 'The cluster is ready. Run `export KUBECONFIG=$PWD/kubeconfig.yaml` to use it'
;;
install-app)
shift
installApps "$@"
;;
retry)
SECONDS="$2"
shift 2
retry "$SECONDS" "$@"
;;
export-kubeconfig)
exportKubeconfig
;;
kubeconfig)
[ -f /output/kubeconfig.yaml ] || exportKubeconfig
cat /output/kubeconfig.yaml
;;
help)
usage
;;
*)
usage
exit 1
;;
esac