84ec0394 by Adam Heath

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

1 parent 1f6a1a80
#!/bin/bash
set -e
APISERVER_DIR="$(cd "$(dirname "$0")/.."; echo "$PWD")"
export APISERVER_DIR
CONTEXT_DIR="$APISERVER_DIR"
......@@ -52,6 +56,158 @@ done
export CONTEXT_DIR
ETCD_ENDPOINTS="http://etcd1:2380,http://etcd2:2380,http://etcd3:2380"
declare -a animations=('-' '\' '|' '/')
declare -i animation_index=0
_compose() {
docker-compose --project-directory "$CONTEXT_DIR" "${compose_files[@]}" "$@"
}
etcdctl() {
_compose exec etcd1 etcdctl "$@"
}
_start_animation() {
animation_index=0
animation_message="$1"
}
_animation_progress() {
printf "$animation_message: %s %s\r" "${animations[$animation_index]}" "$2"
animation_index=$(( ($animation_index + 1) % ${#animations[*]} ))
}
_stop_animation() {
printf "\r\33[2K"
if [[ $1 -eq 0 ]]; then
printf "$animation_message: done\n"
else
printf "$animation_message: error\n"
fi
}
_wait_for_etcd() {
declare -i cnt=5
_start_animation 'Waiting for etcd cluster'
while [ $cnt -ne 0 ]; do
_animation_progress
_compose up -d etcd1 etcd2 etcd3 1>/dev/null 2>/dev/null
if etcdctl --endpoints "$ETCD_ENDPOINTS" endpoint health 1>/dev/null 2>/dev/null; then
if [ $cnt -ne 5 ]; then
printf ' '
fi
_stop_animation 0
return
fi
sleep 1
cnt=$(($cnt - 1))
done
_stop_animation 1
echo "etcd failed to initialize!" 1>&2
exit 1
}
_wait_for_master() {
declare -i cnt=10
_start_animation "Waiting for k3s-master-1"
while [ $cnt -ne 0 ]; do
_animation_progress
if kubectl get --raw '/readyz' > /dev/null 2>/dev/null; then
if [ $cnt -ne 10 ]; then
printf ' '
fi
_stop_animation 0
return
fi
sleep 1
cnt=$(($cnt - 1))
done
_stop_animation 1
echo 'k3s-master-1 failed to initialize!' 1>&2
exit 1
}
_wait_for_system_pods() {
declare -i wanted got total
declare -A items
declare -a output
declare item output
for item in "$@"; do
items[$item]=0/0
done
_start_animation "Waiting for system pods"
while :; do
total=$#
while read item status rest; do
wanted=${status%/*}
got=${status%*/}
if [[ $wanted -eq $got && ! $wanted -eq 0 ]]; then
total=$(($total - 1))
fi
items[$item]="$status"
done < <(kubectl get --namespace kube-system --no-headers --show-kind "$@" 2>/dev/null || true)
output=()
for item in "$@"; do
: item "$item"
output+=("${items[$item]}")
done
_animation_progress "${output[*]}"
if [[ $total -eq 0 ]]; then
break
fi
sleep 1
done
_stop_animation 0
}
_wait_for_system() {
_wait_for_system_pods deployment.apps/metrics-server
}
cmd="$1"
shift
case "$cmd" in
(switch-to)
"$APISERVER_DIR/scripts/update-docker-kubeconfig.sh" "$CONTEXT_DIR"
exit
;;
(wait-for-system)
_wait_for_system
;;
(start)
# Verify that the compose files have valid syntax.
if ! _compose ls 1>/dev/null 2>/dev/null; then
_compose ls
fi
"$APISERVER_DIR/scripts/ensure-certs.sh" "$CONTEXT_DIR"
_wait_for_etcd
_compose up -d k3s-master-1
"$APISERVER_DIR/scripts/update-docker-kubeconfig.sh" "$CONTEXT_DIR"
_wait_for_master
_compose up -d k3s-coredns-1 k3s-coredns-2 k3s-coredns-3
"$APISERVER_DIR/scripts/install-cluster-dns.sh" "$CONTEXT_DIR"
_compose up -d k3s-agent-1 k3s-agent-2 "${k8s_nodes[@]}"
_compose up -d k3s-master-2 k3s-master-3
#"$APISERVER_DIR/scripts/wait-for-system-pods.sh" 1
_wait_for_system
#_compose up -d k3s-proxy
#[[ ${features[istio]} ]] && istioctl install -yf "$APISERVER_DIR/istio-minimal-operator.yaml"
;;
(stop)
_compose down "$@"
;;
("")
;;
(*)
echo "Unknown command: $1" 1>&2
exit 1
;;
esac
......
#!/bin/bash
set -e
APISERVER_DIR="$(cd "$(dirname "$0")/.."; echo "$PWD")"
export APISERVER_DIR
. "$APISERVER_DIR/scripts/_parse_args.bash"
case "$1" in
(switch-to)
"$APISERVER_DIR/scripts/update-docker-kubeconfig.sh" "$CONTEXT_DIR"
exit
;;
("")
;;
(*)
echo "Unknown command: $1" 1>&2
exit 1
;;
esac
if ! _compose ls 1>/dev/null 2>/dev/null; then
_compose ls
fi
"$APISERVER_DIR/scripts/ensure-certs.sh" "$CONTEXT_DIR"
"$APISERVER_DIR/scripts/wait-for-etcd.sh" "$CONTEXT_DIR"
_compose up -d k3s-master-1
"$APISERVER_DIR/scripts/update-docker-kubeconfig.sh" "$CONTEXT_DIR"
"$APISERVER_DIR/scripts/wait-for-master-1.sh"
_compose up -d k3s-coredns-1 k3s-coredns-2 k3s-coredns-3
"$APISERVER_DIR/scripts/install-cluster-dns.sh" "$CONTEXT_DIR"
_compose up -d k3s-agent-1 k3s-agent-2 "${k8s_nodes[@]}"
_compose up -d k3s-master-2 k3s-master-3
"$APISERVER_DIR/scripts/wait-for-system-pods.sh" 1
#_compose up -d k3s-proxy
#[[ ${features[istio]} ]] && istioctl install -yf "$APISERVER_DIR/istio-minimal-operator.yaml"
cd "$APISERVER_DIR"
#helmfile apply
#!/bin/bash
set -e
APISERVER_DIR="$(cd "$(dirname "$0")/.."; echo "$PWD")"
export APISERVER_DIR
. "$APISERVER_DIR/scripts/_parse_args.bash"
_compose down "$@"
#!/bin/sh
set -e
TOP_DIR="$(cd "$(dirname "$0")/.."; echo "$PWD")"
export TOP_DIR
CONTEXT_DIR="$1"
ETCD_ENDPOINTS="http://etcd1:2380,http://etcd2:2380,http://etcd3:2380"
_compose() {
docker-compose --project-directory "$CONTEXT_DIR" -f "$TOP_DIR/docker-compose.yaml" "$@"
}
etcdctl() {
_compose exec etcd1 etcdctl "$@"
}
cnt=5
printf 'Waiting for etcd cluster: '
while [ $cnt -ne 0 ]; do
_compose up -d etcd1 etcd2 etcd3 1>/dev/null 2>/dev/null
if etcdctl --endpoints "$ETCD_ENDPOINTS" endpoint health 1>/dev/null 2>/dev/null; then
if [ $cnt -ne 5 ]; then
printf ' '
fi
printf 'done\n'
exit
fi
printf '.'
sleep 1
cnt=$(($cnt - 1))
done
printf ' error\n'
echo "etcd failed to initialize!" 1>&2
exit 1
#!/bin/sh
cnt=10
printf 'Waiting for k3s-master-1: '
while [ $cnt -ne 0 ]; do
if kubectl get --raw '/readyz' > /dev/null 2>/dev/null; then
if [ $cnt -ne 10 ]; then
printf ' '
fi
printf 'done\n'
exit
fi
printf '.'
sleep 1
cnt=$(($cnt - 1))
done
printf ' error\n'
echo 'k3s-master-1 failed to initialize!' 1>&2
exit 1
#!/bin/bash
set -e
needed_pods="$1"
echo "Waiting for cluster to be ready"
declare -i column_count=0 system_pod_count=0
while :; do
system_pods="$(kubectl get pods --namespace kube-system --no-headers 2>/dev/null || true)"
column_count="$(($column_count + 1))"
if [[ -z $system_pods ]]; then
echo -n "."
else
system_pod_count="$(egrep -ci '1/1[[:space:]]+Running' <<< "$system_pods" || true)"
echo -n "$system_pod_count"
if [[ $system_pod_count -eq ${needed_pods} ]]; then
break
fi
fi
if [[ $column_count -eq 40 ]]; then
echo
column_count=0
fi
sleep 1
done
if [[ $column_count -ne 0 ]]; then
echo
column_count=0
fi