k3s/scripts/sonobuoy

224 lines
4.8 KiB
Plaintext
Raw Normal View History

2019-04-29 20:54:51 +00:00
#!/bin/bash
set -xe
cd $(dirname $0)/..
if [ -z "${K3S_IMAGE}" ]; then
2019-10-27 01:22:44 +00:00
echo 'K3S_IMAGE environment variable should be defined'
2019-04-29 20:54:51 +00:00
exit 1
fi
# ---
port-used() {
(cat </dev/null >/dev/tcp/127.0.0.1/$1) 2>/dev/null
}
export -f port-used
2019-07-04 15:07:26 +00:00
# ---
2019-04-29 20:54:51 +00:00
get-port() {
while
PORT=$((10000 + RANDOM % 50000))
port-used ${PORT}
do continue; done
echo ${PORT}
}
export -f get-port
# ---
fetch-kubeconfig() {
2019-05-03 18:35:58 +00:00
docker cp ${K3S_SERVER}:/etc/rancher/k3s/k3s.yaml ${KUBECONFIG} 2>/dev/null
2019-04-29 20:54:51 +00:00
}
export -f fetch-kubeconfig
2019-07-04 15:07:26 +00:00
# ---
2019-04-29 20:54:51 +00:00
wait-for-kubeconfig() {
2019-05-03 18:35:58 +00:00
while ! fetch-kubeconfig; do
2019-10-27 01:22:44 +00:00
echo 'Waiting for kubeconfig to become available...'
2019-04-29 20:54:51 +00:00
sleep 5
done
}
export -f wait-for-kubeconfig
2019-05-03 18:35:58 +00:00
# ---
2019-04-29 20:54:51 +00:00
count-ready-nodes() {
kubectl get nodes -o json \
| jq '.items[].status.conditions[] | select(.type == "Ready" and .status == "True") | .type' \
| wc -l \
| tr -d '[:space:]'
}
export -f count-ready-nodes
2019-07-04 15:07:26 +00:00
# ---
2019-04-29 20:54:51 +00:00
wait-for-nodes() {
2019-10-27 01:22:44 +00:00
while [[ $(count-ready-nodes) -ne $1 ]]; do
echo 'Waiting for nodes to be ready...'
2019-04-29 20:54:51 +00:00
sleep 5
done
}
export -f wait-for-nodes
# ---
pod-ready() {
kubectl get pods -n kube-system -o json \
| jq ".items[].status.containerStatuses[] | select(.name == \"$1\") | .ready" 2>/dev/null
}
export -f pod-ready
2019-07-04 15:07:26 +00:00
# ---
2019-04-29 20:54:51 +00:00
wait-for-services() {
2019-10-27 01:22:44 +00:00
for service in $@; do
while [[ "$(pod-ready ${service})" != 'true' ]]; do
2019-04-29 20:54:51 +00:00
echo "Waiting for service ${service} to be ready..."
sleep 5
done
echo "Service ${service} is ready"
done
}
export -f wait-for-services
# ---
verify-valid-version() {
if docker exec $@ 2>&1 | grep -iE '(dev|head|unknown|fail|refuse)'; then
return 1
fi
}
export -f verify-valid-version
# ---
verify-valid-versions() {
verify-valid-version $1 kubectl version
verify-valid-version $1 ctr version
verify-valid-version $1 crictl version
}
export -f verify-valid-versions
# ---
2019-07-02 17:07:26 +00:00
dump-container-logs() {
2019-07-28 00:01:10 +00:00
mkdir -p ${LOGS}
2019-07-02 17:07:26 +00:00
for container in ${CONTAINERS}; do
2019-07-28 00:01:10 +00:00
docker cp ${container}:/var/lib/rancher/k3s/agent/containerd/containerd.log ${LOGS}/containerd-${container}.log
docker logs ${container} >${LOGS}/${container}.log 2>&1
2019-07-02 17:07:26 +00:00
done
}
export -f dump-container-logs
# ---
2019-04-29 20:54:51 +00:00
2019-10-14 21:27:39 +00:00
retrieve-sonobuoy-logs() {
2019-10-27 01:22:44 +00:00
if sonobuoy status | grep -q -E ' +e2e +complete +passed +'; then
2019-10-14 21:27:39 +00:00
status=passed
exit_code=0
else
status=failed
exit_code=1
2019-07-02 17:07:26 +00:00
fi
2019-07-04 15:07:26 +00:00
mkdir -p ${E2E}
sonobuoy retrieve ${E2E}
tar x -z -f ${E2E}/*_sonobuoy_*.tar.gz -C ${E2E} ${E2E_LOG}
2019-07-02 17:07:26 +00:00
if [ ! -s ${RESULTS} ]; then
return 1
2019-04-29 20:54:51 +00:00
fi
2019-10-14 21:27:39 +00:00
if [ -n "${E2E_LOG_OUTPUT}" ]; then
cp ${RESULTS} $(sed -e "s/-STATUS-/-${status}-/g" <<< "${E2E_LOG_OUTPUT}")
fi
awk '/^Summarizing .* Failures:$/,0' ${RESULTS}
return ${exit_code}
2019-07-02 17:07:26 +00:00
}
2019-10-14 21:27:39 +00:00
export -f retrieve-sonobuoy-logs
2019-07-02 17:07:26 +00:00
# ---
2019-10-14 21:27:39 +00:00
sonobuoy-test() {
time sonobuoy run \
--config=scripts/sonobuoy-config.json \
--plugin-env=e2e.E2E_USE_GO_RUNNER=true \
--wait=30 \
"${@}" &
SONOBUOY_PID=$!
wait $SONOBUOY_PID
retrieve-sonobuoy-logs
2019-07-02 17:07:26 +00:00
}
2019-10-14 21:27:39 +00:00
export -f sonobuoy-test
2019-07-02 17:07:26 +00:00
# ---
2019-07-04 15:07:26 +00:00
cleanup() {
exit_status=$?
set +e
2019-10-27 01:22:44 +00:00
echo 'Cleaning up'
2019-10-14 21:27:39 +00:00
trap - EXIT
[ -n "$SONOBUOY_PID" ] && kill $SONOBUOY_PID 2>/dev/null
2019-07-26 22:10:39 +00:00
if [ "${exit_status}" -ne "0" ]; then
dump-container-logs
fi
2019-07-04 15:07:26 +00:00
docker rm -f ${CONTAINERS} 2>/dev/null
rm ${KUBECONFIG}
exit ${exit_status}
}
trap cleanup EXIT
# ---
K3S_PORT=$(timeout --foreground 5s bash -c get-port)
OUTPUT=$(pwd)/sonobuoy-output/${K3S_PORT}
2019-07-28 00:01:10 +00:00
LOGS=$(pwd)/logs/$$
2019-07-04 15:07:26 +00:00
E2E="${OUTPUT}/e2e"
2019-10-27 01:22:44 +00:00
E2E_LOG='plugins/e2e/results/global/e2e.log'
2019-07-04 15:07:26 +00:00
RESULTS="${E2E}/${E2E_LOG}"
SECRET=random-$((100000 + RANDOM % 999999))
export K3S_AGENT=sonobuoy-k3s-agent-${K3S_PORT}
export K3S_SERVER=sonobuoy-k3s-server-${K3S_PORT}
export CONTAINERS="${K3S_SERVER} ${K3S_AGENT}"
export KUBECONFIG=${OUTPUT}/kubeconfig.yaml
2019-10-14 21:27:39 +00:00
mkdir -p ${OUTPUT}
2019-07-04 15:07:26 +00:00
# ---
docker run -d --name ${K3S_SERVER} --privileged \
-p 127.0.0.1:${K3S_PORT}:${K3S_PORT} \
-e K3S_CLUSTER_SECRET=${SECRET} \
${K3S_IMAGE} server --no-deploy=traefik --https-listen-port=${K3S_PORT}
K3S_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${K3S_SERVER})
echo "Started ${K3S_SERVER} @ ${K3S_IP}:${K3S_PORT}"
timeout --foreground 1m bash -c wait-for-kubeconfig
verify-valid-versions ${K3S_SERVER}
# ---
2019-07-04 15:07:26 +00:00
docker run -d --name ${K3S_AGENT} --privileged \
-e K3S_CLUSTER_SECRET=${SECRET} \
-e K3S_URL=https://${K3S_IP}:${K3S_PORT} \
${K3S_IMAGE} agent
echo "Started ${K3S_AGENT}"
# ---
2019-10-27 01:22:44 +00:00
timeout --foreground 1m bash -c 'wait-for-nodes 2'
timeout --foreground 1m bash -c 'wait-for-services coredns local-path-provisioner metrics-server'
2019-07-04 15:07:26 +00:00
2019-10-27 01:22:44 +00:00
if [ "$ARCH" = 'arm' ]; then
echo "Aborting sonobuoy tests, images not available for $ARCH"
exit 0
fi
2019-10-27 01:22:44 +00:00
echo 'Starting sonobuoy tests'
2019-10-14 21:27:39 +00:00
sonobuoy-test "${@}"