856e2497af8f54e21e40a91fc7b909891b224d6b
[icn.git] / deploy / site / pod11 / pod11.sh
1 #!/usr/bin/env bash
2 set -eux -o pipefail
3
4 SCRIPTDIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
5 LIBDIR="$(dirname $(dirname $(dirname ${SCRIPTDIR})))/env/lib"
6
7 source $LIBDIR/common.sh
8 source $SCRIPTDIR/../common.sh
9
10 BUILDDIR=${SCRIPTDIR/deploy/build}
11 mkdir -p ${BUILDDIR}
12
13 SITE_REPO=${SITE_REPO:-"https://gerrit.akraino.org/r/icn"}
14 SITE_BRANCH=${SITE_BRANCH:-"master"}
15 SITE_PATH=${SITE_PATH:-"deploy/site/pod11/deployment"}
16
17 function deploy {
18     # TODO Replace ICN test key with real key
19     flux_create_site ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH} ${FLUX_SOPS_KEY_NAME}
20 }
21
22 function clean {
23     kubectl -n flux-system delete kustomization $(flux_site_kustomization_name ${SITE_REPO} ${SITE_BRANCH} ${SITE_PATH})
24 }
25
26 function is_cluster_ready {
27     [[ $(kubectl -n ${SITE_NAMESPACE} get cluster icn -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]]
28 }
29
30 function is_control_plane_ready {
31     # Checking the Cluster resource status is not sufficient, it
32     # reports the control plane as ready before the nodes forming the
33     # control plane are ready
34     local -r replicas=$(kubectl -n ${SITE_NAMESPACE} get kubeadmcontrolplane icn -o jsonpath='{.spec.replicas}')
35     [[ $(kubectl --kubeconfig=${BUILDDIR}/icn-admin.conf get nodes -l node-role.kubernetes.io/control-plane -o jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Ready")].status}{"\n"}{end}' | grep -c True) == ${replicas} ]]
36 }
37
38 function wait_for_all_ready {
39     WAIT_FOR_INTERVAL=60s
40     WAIT_FOR_TRIES=30
41     wait_for is_cluster_ready
42     clusterctl -n ${SITE_NAMESPACE} get kubeconfig icn >${BUILDDIR}/icn-admin.conf
43     chmod 600 ${BUILDDIR}/icn-admin.conf
44     wait_for is_control_plane_ready
45 }
46
47 function is_cluster_deleted {
48     ! kubectl -n ${SITE_NAMESPACE} get cluster icn
49 }
50
51 function wait_for_all_deleted {
52     WAIT_FOR_INTERVAL=60s
53     WAIT_FOR_TRIES=30
54     wait_for is_cluster_deleted
55 }
56
57 case $1 in
58     "clean") clean ;;
59     "deploy") deploy ;;
60     "wait") wait_for_all_ready ;;
61     "wait-clean") wait_for_all_deleted ;;
62     *) cat <<EOF
63 Usage: $(basename $0) COMMAND
64
65 Commands:
66   clean         - Remove the site
67   deploy        - Deploy the site
68   wait          - Wait for the site to be ready
69 EOF
70        ;;
71 esac