187e539371c8eb6cdf75f09a4b9e5222495dd955
[icn.git] / deploy / kata / kata.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 KATA_VERSION="2.1.0-rc0"
14 KATA_WEBHOOK_VERSION="2.1.0-rc0"
15
16 KATA_DEPLOY_URL="https://raw.githubusercontent.com/kata-containers/kata-containers/${KATA_VERSION}/tools/packaging/kata-deploy"
17 KATA_WEBHOOK_URL="https://raw.githubusercontent.com/kata-containers/tests/${KATA_WEBHOOK_VERSION}/kata-webhook"
18 KATA_WEBHOOK_DIR="/opt/src/kata_webhook"
19 KATA_WEBHOOK_RUNTIMECLASS="kata-clh"
20
21 # This may be used to update the in-place Kata YAML files from the
22 # upstream project.
23 function build_source {
24     mkdir -p ${SCRIPTDIR}/base
25     curl -sL ${KATA_DEPLOY_URL}/kata-rbac/base/kata-rbac.yaml -o ${SCRIPTDIR}/base/kata-rbac.yaml
26     curl -sL ${KATA_DEPLOY_URL}/kata-deploy/base/kata-deploy.yaml -o ${SCRIPTDIR}/base/kata-deploy.yaml
27     curl -sL ${KATA_DEPLOY_URL}/runtimeclasses/kata-runtimeClasses.yaml -o ${SCRIPTDIR}/base/kata-runtimeClasses.yaml
28     pushd ${SCRIPTDIR}/base && kustomize create --autodetect && popd
29 }
30
31 function deploy_webhook {
32     local -r cluster_name=$1
33     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
34
35     # Note that the webhook-registration.yaml.tpl file is fetched here
36     # but webhook-registration.yaml is deployed: this is intentional,
37     # create-certs.sh takes care of converting the .yaml.tpl into the
38     # .yaml file
39     mkdir -p ${BUILDDIR}/webhook/base/deploy
40     curl -sL ${KATA_WEBHOOK_URL}/create-certs.sh -o ${BUILDDIR}/webhook/base/create-certs.sh
41     curl -sL ${KATA_WEBHOOK_URL}/deploy/webhook-registration.yaml.tpl -o ${BUILDDIR}/webhook/base/deploy/webhook-registration.yaml.tpl
42     curl -sL ${KATA_WEBHOOK_URL}/deploy/webhook.yaml -o ${BUILDDIR}/webhook/base/deploy/webhook.yaml
43
44     chmod +x ${BUILDDIR}/webhook/base/create-certs.sh
45     sed 's/value: kata/value: ${KATA_WEBHOOK_RUNTIMECLASS}/g' ${BUILDDIR}/webhook/base/deploy/webhook.yaml | tee ${BUILDDIR}/webhook/base/deploy/webhook-${KATA_WEBHOOK_RUNTIMECLASS}.yaml
46     pushd ${BUILDDIR}/webhook/base && ./create-certs.sh && popd
47
48     cat <<EOF >${BUILDDIR}/webhook/base/kustomization.yaml
49 apiVersion: kustomize.config.k8s.io/v1beta1
50 kind: Kustomization
51 resources:
52 - deploy/webhook-certs.yaml
53 - deploy/webhook-registration.yaml
54 - deploy/webhook-${KATA_WEBHOOK_RUNTIMECLASS}.yaml
55 EOF
56
57     kustomize build ${BUILDDIR}/webhook/base | KUBECONFIG=${cluster_kubeconfig} kubectl apply -f -
58 }
59
60 function clean_webhook {
61     local -r cluster_name=$1
62     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
63
64     kustomize build ${BUILDDIR}/webhook/base | KUBECONFIG=${cluster_kubeconfig} kubectl delete -f -
65 }
66
67 function is_kata_deployed {
68     local -r cluster_name=${CLUSTER_NAME:-icn}
69     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
70     kubectl --kubeconfig=${cluster_kubeconfig} get runtimeclass/kata-qemu
71 }
72
73 function test_kata {
74     # Create a temporary kubeconfig file for the tests
75     local -r cluster_name=${CLUSTER_NAME:-icn}
76     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
77     clusterctl -n metal3 get kubeconfig ${cluster_name} >${cluster_kubeconfig}
78
79     # Ensure that Kata has been deployed first
80     WAIT_FOR_TRIES=30
81     wait_for is_kata_deployed
82
83     deploy_webhook ${cluster_name}
84     clone_kud_repository
85     pushd ${KUDPATH}/kud/tests
86     failed_kud_tests=""
87     KUBECONFIG=${cluster_kubeconfig} bash kata.sh || failed_kud_tests="${failed_kud_tests} kata"
88     popd
89     clean_webhook ${cluster_name}
90     if [[ ! -z "$failed_kud_tests" ]]; then
91         echo "Test cases failed:${failed_kud_tests}"
92         exit 1
93     fi
94     echo "All test cases passed"
95
96     rm ${cluster_kubeconfig}
97 }
98
99 case $1 in
100     "build-source") build_source ;;
101     "test") test_kata ;;
102     *) cat <<EOF
103 Usage: $(basename $0) COMMAND
104
105 The "test" command looks for the CLUSTER_NAME variable in the
106 environment (default: "icn").  This should be the name of the
107 Cluster resource to execute the tests in.
108
109 Commands:
110   build-source  - Rebuild the in-tree Kata YAML files
111   test          - Test Kata
112 EOF
113        ;;
114 esac