84ec0394 by Adam Heath

Inline a bunch of scripts, while also fixing wait-for-system-pods phase.

1 parent 1f6a1a80
1 #!/bin/bash 1 #!/bin/bash
2 set -e
3
4 APISERVER_DIR="$(cd "$(dirname "$0")/.."; echo "$PWD")"
5 export APISERVER_DIR
2 6
3 CONTEXT_DIR="$APISERVER_DIR" 7 CONTEXT_DIR="$APISERVER_DIR"
4 8
...@@ -52,6 +56,158 @@ done ...@@ -52,6 +56,158 @@ done
52 56
53 export CONTEXT_DIR 57 export CONTEXT_DIR
54 58
59 ETCD_ENDPOINTS="http://etcd1:2380,http://etcd2:2380,http://etcd3:2380"
60
61 declare -a animations=('-' '\' '|' '/')
62 declare -i animation_index=0
63
55 _compose() { 64 _compose() {
56 docker-compose --project-directory "$CONTEXT_DIR" "${compose_files[@]}" "$@" 65 docker-compose --project-directory "$CONTEXT_DIR" "${compose_files[@]}" "$@"
57 } 66 }
67
68 etcdctl() {
69 _compose exec etcd1 etcdctl "$@"
70 }
71
72
73 _start_animation() {
74 animation_index=0
75 animation_message="$1"
76 }
77
78 _animation_progress() {
79 printf "$animation_message: %s %s\r" "${animations[$animation_index]}" "$2"
80 animation_index=$(( ($animation_index + 1) % ${#animations[*]} ))
81 }
82
83 _stop_animation() {
84 printf "\r\33[2K"
85 if [[ $1 -eq 0 ]]; then
86 printf "$animation_message: done\n"
87 else
88 printf "$animation_message: error\n"
89 fi
90 }
91
92 _wait_for_etcd() {
93 declare -i cnt=5
94 _start_animation 'Waiting for etcd cluster'
95 while [ $cnt -ne 0 ]; do
96 _animation_progress
97 _compose up -d etcd1 etcd2 etcd3 1>/dev/null 2>/dev/null
98 if etcdctl --endpoints "$ETCD_ENDPOINTS" endpoint health 1>/dev/null 2>/dev/null; then
99 if [ $cnt -ne 5 ]; then
100 printf ' '
101 fi
102 _stop_animation 0
103 return
104 fi
105 sleep 1
106 cnt=$(($cnt - 1))
107 done
108 _stop_animation 1
109 echo "etcd failed to initialize!" 1>&2
110 exit 1
111 }
112
113 _wait_for_master() {
114 declare -i cnt=10
115 _start_animation "Waiting for k3s-master-1"
116 while [ $cnt -ne 0 ]; do
117 _animation_progress
118 if kubectl get --raw '/readyz' > /dev/null 2>/dev/null; then
119 if [ $cnt -ne 10 ]; then
120 printf ' '
121 fi
122 _stop_animation 0
123 return
124 fi
125 sleep 1
126 cnt=$(($cnt - 1))
127 done
128 _stop_animation 1
129 echo 'k3s-master-1 failed to initialize!' 1>&2
130 exit 1
131 }
132
133 _wait_for_system_pods() {
134 declare -i wanted got total
135 declare -A items
136 declare -a output
137 declare item output
138 for item in "$@"; do
139 items[$item]=0/0
140 done
141 _start_animation "Waiting for system pods"
142 while :; do
143 total=$#
144 while read item status rest; do
145 wanted=${status%/*}
146 got=${status%*/}
147 if [[ $wanted -eq $got && ! $wanted -eq 0 ]]; then
148 total=$(($total - 1))
149 fi
150 items[$item]="$status"
151 done < <(kubectl get --namespace kube-system --no-headers --show-kind "$@" 2>/dev/null || true)
152 output=()
153 for item in "$@"; do
154 : item "$item"
155 output+=("${items[$item]}")
156 done
157 _animation_progress "${output[*]}"
158 if [[ $total -eq 0 ]]; then
159 break
160 fi
161 sleep 1
162 done
163 _stop_animation 0
164 }
165
166 _wait_for_system() {
167 _wait_for_system_pods deployment.apps/metrics-server
168 }
169
170 cmd="$1"
171 shift
172 case "$cmd" in
173 (switch-to)
174 "$APISERVER_DIR/scripts/update-docker-kubeconfig.sh" "$CONTEXT_DIR"
175 exit
176 ;;
177 (wait-for-system)
178 _wait_for_system
179 ;;
180 (start)
181 # Verify that the compose files have valid syntax.
182 if ! _compose ls 1>/dev/null 2>/dev/null; then
183 _compose ls
184 fi
185
186 "$APISERVER_DIR/scripts/ensure-certs.sh" "$CONTEXT_DIR"
187 _wait_for_etcd
188
189 _compose up -d k3s-master-1
190 "$APISERVER_DIR/scripts/update-docker-kubeconfig.sh" "$CONTEXT_DIR"
191 _wait_for_master
192
193 _compose up -d k3s-coredns-1 k3s-coredns-2 k3s-coredns-3
194 "$APISERVER_DIR/scripts/install-cluster-dns.sh" "$CONTEXT_DIR"
195 _compose up -d k3s-agent-1 k3s-agent-2 "${k8s_nodes[@]}"
196 _compose up -d k3s-master-2 k3s-master-3
197 #"$APISERVER_DIR/scripts/wait-for-system-pods.sh" 1
198 _wait_for_system
199 #_compose up -d k3s-proxy
200
201 #[[ ${features[istio]} ]] && istioctl install -yf "$APISERVER_DIR/istio-minimal-operator.yaml"
202 ;;
203 (stop)
204 _compose down "$@"
205 ;;
206 ("")
207 ;;
208 (*)
209 echo "Unknown command: $1" 1>&2
210 exit 1
211 ;;
212 esac
213
......
1 #!/bin/bash
2
3 set -e
4
5 APISERVER_DIR="$(cd "$(dirname "$0")/.."; echo "$PWD")"
6 export APISERVER_DIR
7
8 . "$APISERVER_DIR/scripts/_parse_args.bash"
9
10 case "$1" in
11 (switch-to)
12 "$APISERVER_DIR/scripts/update-docker-kubeconfig.sh" "$CONTEXT_DIR"
13 exit
14 ;;
15 ("")
16 ;;
17 (*)
18 echo "Unknown command: $1" 1>&2
19 exit 1
20 ;;
21 esac
22 if ! _compose ls 1>/dev/null 2>/dev/null; then
23 _compose ls
24 fi
25 "$APISERVER_DIR/scripts/ensure-certs.sh" "$CONTEXT_DIR"
26 "$APISERVER_DIR/scripts/wait-for-etcd.sh" "$CONTEXT_DIR"
27
28 _compose up -d k3s-master-1
29 "$APISERVER_DIR/scripts/update-docker-kubeconfig.sh" "$CONTEXT_DIR"
30 "$APISERVER_DIR/scripts/wait-for-master-1.sh"
31
32 _compose up -d k3s-coredns-1 k3s-coredns-2 k3s-coredns-3
33 "$APISERVER_DIR/scripts/install-cluster-dns.sh" "$CONTEXT_DIR"
34 _compose up -d k3s-agent-1 k3s-agent-2 "${k8s_nodes[@]}"
35 _compose up -d k3s-master-2 k3s-master-3
36 "$APISERVER_DIR/scripts/wait-for-system-pods.sh" 1
37 #_compose up -d k3s-proxy
38
39 #[[ ${features[istio]} ]] && istioctl install -yf "$APISERVER_DIR/istio-minimal-operator.yaml"
40
41 cd "$APISERVER_DIR"
42
43 #helmfile apply
1 #!/bin/bash
2
3 set -e
4
5 APISERVER_DIR="$(cd "$(dirname "$0")/.."; echo "$PWD")"
6 export APISERVER_DIR
7
8 . "$APISERVER_DIR/scripts/_parse_args.bash"
9
10 _compose down "$@"
11
1 #!/bin/sh
2
3 set -e
4
5 TOP_DIR="$(cd "$(dirname "$0")/.."; echo "$PWD")"
6 export TOP_DIR
7
8 CONTEXT_DIR="$1"
9 ETCD_ENDPOINTS="http://etcd1:2380,http://etcd2:2380,http://etcd3:2380"
10
11 _compose() {
12 docker-compose --project-directory "$CONTEXT_DIR" -f "$TOP_DIR/docker-compose.yaml" "$@"
13 }
14
15 etcdctl() {
16 _compose exec etcd1 etcdctl "$@"
17 }
18
19 cnt=5
20 printf 'Waiting for etcd cluster: '
21
22 while [ $cnt -ne 0 ]; do
23 _compose up -d etcd1 etcd2 etcd3 1>/dev/null 2>/dev/null
24 if etcdctl --endpoints "$ETCD_ENDPOINTS" endpoint health 1>/dev/null 2>/dev/null; then
25 if [ $cnt -ne 5 ]; then
26 printf ' '
27 fi
28 printf 'done\n'
29 exit
30 fi
31 printf '.'
32 sleep 1
33 cnt=$(($cnt - 1))
34 done
35 printf ' error\n'
36
37 echo "etcd failed to initialize!" 1>&2
38 exit 1
1 #!/bin/sh
2
3 cnt=10
4 printf 'Waiting for k3s-master-1: '
5 while [ $cnt -ne 0 ]; do
6 if kubectl get --raw '/readyz' > /dev/null 2>/dev/null; then
7 if [ $cnt -ne 10 ]; then
8 printf ' '
9 fi
10 printf 'done\n'
11 exit
12 fi
13 printf '.'
14 sleep 1
15 cnt=$(($cnt - 1))
16 done
17 printf ' error\n'
18
19 echo 'k3s-master-1 failed to initialize!' 1>&2
20 exit 1
1 #!/bin/bash
2
3 set -e
4
5 needed_pods="$1"
6
7 echo "Waiting for cluster to be ready"
8 declare -i column_count=0 system_pod_count=0
9 while :; do
10 system_pods="$(kubectl get pods --namespace kube-system --no-headers 2>/dev/null || true)"
11 column_count="$(($column_count + 1))"
12 if [[ -z $system_pods ]]; then
13 echo -n "."
14 else
15 system_pod_count="$(egrep -ci '1/1[[:space:]]+Running' <<< "$system_pods" || true)"
16 echo -n "$system_pod_count"
17 if [[ $system_pod_count -eq ${needed_pods} ]]; then
18 break
19 fi
20 fi
21 if [[ $column_count -eq 40 ]]; then
22 echo
23 column_count=0
24 fi
25 sleep 1
26 done
27 if [[ $column_count -ne 0 ]]; then
28 echo
29 column_count=0
30 fi