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