c8a680811f53547e68b9fd28eeec7b1834bbf33b
[icn.git] / deploy / addons / addons.sh
1 #!/usr/bin/env bash
2 set -eux -o pipefail
3
4 SCRIPTDIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
5 LIBDIR="$(dirname $(dirname ${SCRIPTDIR}))/env/lib"
6
7 source $LIBDIR/logging.sh
8 source $LIBDIR/common.sh
9
10 BUILDDIR=${SCRIPTDIR/deploy/build}
11 mkdir -p ${BUILDDIR}
12
13 function install_deps {
14     apt-get install -y jq
15 }
16
17 function is_emco_ready {
18     local -r cluster_name=${CLUSTER_NAME:-icn}
19     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
20     kubectl --kubeconfig=${cluster_kubeconfig} -n emco wait pod --all --for=condition=Ready --timeout=0s >/dev/null 2>&1
21 }
22
23 function register_emco_controllers {
24     wait_for is_emco_ready
25     local -r cluster_name=${CLUSTER_NAME:-icn}
26     local -r host=$(kubectl -n metal3 get cluster/${cluster_name} -o jsonpath='{.spec.controlPlaneEndpoint.host}')
27     cat <<EOF >${BUILDDIR}/${cluster_name}-config.yaml
28 orchestrator:
29   host: ${host}
30   port: 30415
31 EOF
32     cat <<EOF >${BUILDDIR}/${cluster_name}-controllers.yaml
33 ---
34 version: emco/v2
35 resourceContext:
36   anchor: controllers
37 metadata:
38   name: rsync
39 spec:
40   host: ${host}
41   port: 30441
42 ---
43 version: emco/v2
44 resourceContext:
45   anchor: controllers
46 metadata:
47   name: gac
48 spec:
49   host: ${host}
50   port: 30493
51   type: "action"
52   priority: 1
53 ---
54 version: emco/v2
55 resourceContext:
56   anchor: controllers
57 metadata:
58   name: ovnaction
59 spec:
60   host: ${host}
61   port: 30473
62   type: "action"
63   priority: 1
64 ---
65 version: emco/v2
66 resourceContext:
67   anchor: controllers
68 metadata:
69   name: dtc
70 spec:
71   host: ${host}
72   port: 30483
73   type: "action"
74   priority: 1
75 EOF
76     emcoctl --config ${BUILDDIR}/${cluster_name}-config.yaml apply -f ${BUILDDIR}/${cluster_name}-controllers.yaml
77 }
78
79 function unregister_emco_controllers {
80     local -r cluster_name=${CLUSTER_NAME:-icn}
81     emcoctl --config ${BUILDDIR}/${cluster_name}-config.yaml delete -f ${BUILDDIR}/${cluster_name}-controllers.yaml
82 }
83
84 function is_addon_ready {
85     local -r addon=$1
86     local -r cluster_name=${CLUSTER_NAME:-icn}
87     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
88     [[ $(kubectl --kubeconfig=${cluster_kubeconfig} -n kud get Kustomization/${addon} -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]]
89
90     # Additional addon specific checks
91     case ${addon} in
92         "cpu-manager")
93             for node in $(kubectl --kubeconfig=${cluster_kubeconfig} -n kud get pods -l app=cmk-reconcile-ds-all -o jsonpath='{range .items[*]}{.spec.nodeName}{"\n"}{end}' | sort | uniq); do
94                 kubectl --kubeconfig=${cluster_kubeconfig} get cmk-nodereport ${node}
95             done
96             ;;
97     esac
98 }
99
100 function test_addons {
101     install_deps
102
103     # Create a temporary kubeconfig file for the tests
104     local -r cluster_name=${CLUSTER_NAME:-icn}
105     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
106     clusterctl -n metal3 get kubeconfig ${cluster_name} >${cluster_kubeconfig}
107
108     clone_kud_repository
109     # The vFW test in EMCO v21.12 does not use KubeVirt, so patch the
110     # KuD test and continue to use it
111     pushd ${KUDPATH}
112     patch -p1 --forward <${SCRIPTDIR}/plugin_fw_v2.patch || true
113     popd
114
115     pushd ${KUDPATH}/kud/tests
116     failed_kud_tests=""
117     container_runtime=$(KUBECONFIG=${cluster_kubeconfig} kubectl get nodes -o jsonpath='{.items[].status.nodeInfo.containerRuntimeVersion}')
118     # TODO Temporarily remove kubevirt from kud_tests below.  The
119     # kubevirt self-test needs AllowTcpForwarding yes in
120     # /etc/ssh/sshd_config which is currently disabled by the OS
121     # security hardening.
122     if [[ "${container_runtime}" == "containerd://1.2.13" ]]; then
123         # With containerd 1.2.13, the qat test container image fails to unpack.
124         kud_tests="topology-manager-sriov:sriov-network multus:multus-cni ovn4nfv:nodus-network nfd:node-feature-discovery sriov-network:sriov-network cmk:cpu-manager"
125     else
126         kud_tests="topology-manager-sriov:sriov-network multus:multus-cni ovn4nfv:nodus-network nfd:node-feature-discovery sriov-network:sriov-network qat:qat-plugin cmk:cpu-manager"
127     fi
128     for kud_test in ${kud_tests}; do
129         addon="${kud_test#*:}"
130         test="${kud_test%:*}"
131         if [[ ! -z ${addon} ]]; then
132             wait_for is_addon_ready ${addon}
133         fi
134         KUBECONFIG=${cluster_kubeconfig} bash ${test}.sh || failed_kud_tests="${failed_kud_tests} ${test}"
135     done
136     # The plugin_fw_v2 test needs the EMCO controllers in place
137     register_emco_controllers
138     DEMO_FOLDER=${KUDPATH}/kud/demo KUBECONFIG=${cluster_kubeconfig} bash plugin_fw_v2.sh --external || failed_kud_tests="${failed_kud_tests} plugin_fw_v2"
139     unregister_emco_controllers
140     popd
141     if [[ ! -z "$failed_kud_tests" ]]; then
142         echo "Test cases failed:${failed_kud_tests}"
143         exit 1
144     fi
145     echo "All test cases passed"
146
147     rm ${cluster_kubeconfig}
148 }
149
150 case $1 in
151     "test") test_addons ;;
152     *) cat <<EOF
153 Usage: $(basename $0) COMMAND
154
155 The "test" command looks for the CLUSTER_NAME variable in the
156 environment (default: "icn").  This should be the name of the
157 Cluster resource to execute the tests in.
158
159 Commands:
160   test  - Test the addons
161 EOF
162        ;;
163 esac