config/scripts/2.first_start_k8s.sh

89 lines
2.2 KiB
Bash
Raw Normal View History

2023-05-24 18:12:07 +00:00
#!/bin/bash
#
# Starting control plane
prereqs_control_plane() {
for com in kubectl ; do
command -v $com 1>/dev/null || perror "Command $com is not installed, aborting..."
done
}
prereqs_addons() {
for com in curl kubectl ; do
command -v $com 1>/dev/null || perror "Command $com is not installed, aborting..."
done
cluster=$(basename $ctxdir/clusters/*.yaml|sed 's/.yaml//')
}
checkup() {
for host in ${!hosts[*]}; do
tries=3
while :
do
pinfo "Checking availability of node $host..."
ssh root@${hosts[$host]} <<< true &>/dev/null
if [ $? == 0 ]; then
pinfo "VM $host is up!"
break
else
((tries--))
if [ $tries < 1 ]; then
pinfo "Timeout waiting for node detection, please investigate why node $host is not up by now"
break
fi
sleep 30
fi
done
done
}
start_control_plane() {
for host in ${!hosts[*]}; do
ssh root@${hosts[$host]} << EOF
if ls /etc/kubernetes/manifests.static/* &>/dev/null ; then
mv /etc/kubernetes/manifests.static/* /var/lib/kubelet/manifests/
fi
EOF
done
}
install_addons() {
body='{"Kind":"cluster","Name":"'$cluster'","Assets":["addons"]}'
download_id=$(dls /authorize-download -d "$body"|tr -d \")
dls /public/downloads/${download_id}/addons |\
kctl apply -f -
}
approve_kubelet_certificates() {
tries=5
nodes_num=$(kctl get node -oname|wc -l)
while [ "$nodes_num" != "${#hosts[*]}" ] ; do
pinfo "Waiting for certificates requests to be created by Kubelet when it's ready... ($tries/5)"
sleep 60s
csrs="$(kctl get csr|awk '/Pending/ {print $1}')"
if [ "$csrs" != "" ]; then
kctl certificate approve $csrs
fi
((tries--))
if [ "$tries" < 1 ]; then
pinfo "Timeout waiting for kubelet certificates creation, please investigate why all nodes are not up by now"
break
fi
done
pinfo "All kubelets ($nodes_num) are up, enjoy !"
}
source $(dirname $0)/.common
prereqs
prereqs_control_plane
prereqs_addons
check_conf
unlock_store
declare -A hosts && get_hosts
checkup
start_control_plane
create_kubeconfig
install_addons
approve_kubelet_certificates # clients and serving certs