Some of the repetitive violations were suppressed.
3.30KiB; Unix Shell | 2019-02-15 10:15:10+01 | SLOC 88
1
#!/bin/bash
2
# Q&D test'em all: creates full DIND kubespray deploys
3
# for each distro, verifying it via netchecker.
4
5
info() {
6
    local msg="$*"
7 1
    local date="$(date -Isec)"
8
    echo "INFO: [$date] $msg"
9
}
10
pass_or_fail() {
11
    local rc="$?"
12
    local msg="$*"
13 1
    local date="$(date -Isec)"
14
    [ $rc -eq 0 ] && echo "PASS: [$date] $msg" || echo "FAIL: [$date] $msg"
15
    return $rc
16
}
17
test_distro() {
18
    local distro=${1:?};shift
19
    local extra="${*:-}"
20
    local prefix="$distro[${extra}]}"
21 1
    ansible-playbook -i hosts dind-cluster.yaml -e node_distro=$distro
22
    pass_or_fail "$prefix: dind-nodes" || return 1
23
    (cd ../..
24
        INVENTORY_DIR=inventory/local-dind
25
        mkdir -p ${INVENTORY_DIR}
26
        rm -f ${INVENTORY_DIR}/hosts.ini
27
        CONFIG_FILE=${INVENTORY_DIR}/hosts.ini /tmp/kubespray.dind.inventory_builder.sh
28
        # expand $extra with -e in front of each word
29
        extra_args=""; for extra_arg in $extra; do extra_args="$extra_args -e $extra_arg"; done
30 1
        ansible-playbook --become -e ansible_ssh_user=$distro -i \
31
            ${INVENTORY_DIR}/hosts.ini cluster.yml \
32 2
            -e @contrib/dind/kubespray-dind.yaml -e bootstrap_os=$distro ${extra_args}
33
        pass_or_fail "$prefix: kubespray"
34
    ) || return 1
35
    local node0=${NODES[0]}
36 1
    docker exec ${node0} kubectl get pod --all-namespaces
37
    pass_or_fail "$prefix: kube-api" || return 1
38
    let retries=60
39
    while ((retries--)); do
40
        # Some CNI may set NodePort on "main" node interface address (thus no localhost NodePort)
41
        # e.g. kube-router: https://github.com/cloudnativelabs/kube-router/pull/217
42 2
        docker exec ${node0} curl -m2 -s http://${NETCHECKER_HOST:?}:31081/api/v1/connectivity_check | grep successfully && break
43
        sleep 2
44
    done
45
    [ $retries -ge 0 ]
46
    pass_or_fail "$prefix: netcheck" || return 1
47
}
48
49
NODES=($(egrep ^kube-node hosts))
50
NETCHECKER_HOST=localhost
51
52 1
: ${OUTPUT_DIR:=./out}
53 1
mkdir -p ${OUTPUT_DIR}
54
55
# The SPEC file(s) must have two arrays as e.g.
56
# DISTROS=(debian centos)
57
# EXTRAS=(
58
#     'kube_network_plugin=calico'
59
#     'kube_network_plugin=flannel'
60
#     'kube_network_plugin=weave'
61
# )
62
# that will be tested in a "combinatory" way (e.g. from above there'll be
63
# be 6 test runs), creating a sequenced <spec_filename>-nn.out with each output.
64
#
65
# Each $EXTRAS element will be whitespace split, and passed as --extra-vars
66
# to main kubespray ansible-playbook run.
67
68
SPECS=${*:?Missing SPEC files, e.g. test-most_distros-some_CNIs.env}
69
for spec in ${SPECS}; do
70
    unset DISTROS EXTRAS
71
    echo "Loading file=${spec} ..."
72 2
    . ${spec} || continue
73
    : ${DISTROS:?} || continue
74 1
    echo "DISTROS=${DISTROS[@]}"
75
    echo "EXTRAS->"
76
    printf "  %s\n" "${EXTRAS[@]}"
77
    let n=1
78 1
    for distro in ${DISTROS[@]}; do
79
        for extra in "${EXTRAS[@]:-NULL}"; do
80
            # Magic value to let this for run once:
81
            [[ ${extra} == NULL ]] && unset extra
82 1
            docker rm -f ${NODES[@]}
83
            printf -v file_out "%s/%s-%02d.out" ${OUTPUT_DIR} ${spec} $((n++))
84
            {
85
                info "${distro}[${extra}] START: file_out=${file_out}"
86
                time test_distro ${distro} ${extra}
87
            } |& tee ${file_out}
88
            # sleeping for the sake of the human to verify if they want
89
            sleep 2m
90
        done
91
    done
92
done
93 1
egrep -H '^(....:|real)' $(ls -tr ${OUTPUT_DIR}/*.out)