2019-04-29 20:54:51 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -xe
|
|
|
|
|
|
|
|
cd $(dirname $0)/..
|
|
|
|
|
|
|
|
if [ -z "${K3S_IMAGE}" ]; then
|
|
|
|
echo "K3S_IMAGE environment variable should be defined"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# ---
|
|
|
|
|
|
|
|
port-used() {
|
|
|
|
(cat </dev/null >/dev/tcp/127.0.0.1/$1) 2>/dev/null
|
|
|
|
}
|
|
|
|
export -f port-used
|
|
|
|
|
|
|
|
get-port() {
|
|
|
|
while
|
|
|
|
PORT=$((10000 + RANDOM % 50000))
|
|
|
|
port-used ${PORT}
|
|
|
|
do continue; done
|
|
|
|
echo ${PORT}
|
|
|
|
}
|
|
|
|
export -f get-port
|
|
|
|
|
2019-05-02 19:04:45 +00:00
|
|
|
K3S_PORT=$(timeout --foreground 5s bash -c get-port)
|
2019-04-29 20:54:51 +00:00
|
|
|
OUTPUT=$(pwd)/sonobuoy-output/${K3S_PORT}
|
|
|
|
mkdir -p ${OUTPUT}
|
|
|
|
|
|
|
|
SECRET=random-$((100000 + RANDOM % 999999))
|
|
|
|
export K3S_AGENT=sonobuoy-k3s-agent-${K3S_PORT}
|
|
|
|
export K3S_SERVER=sonobuoy-k3s-server-${K3S_PORT}
|
|
|
|
export KUBECONFIG=${OUTPUT}/kubeconfig.yaml
|
|
|
|
|
|
|
|
# ---
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
exit_status=$?
|
|
|
|
set +e
|
|
|
|
echo "Cleaning up"
|
2019-05-03 18:35:58 +00:00
|
|
|
for container in ${K3S_SERVER} ${K3S_AGENT}; do
|
|
|
|
docker cp ${container}:/var/lib/rancher/k3s/agent/containerd/containerd.log ${OUTPUT}/${container}-containerd.log
|
|
|
|
docker logs ${container} >${OUTPUT}/${container}.log 2>&1
|
|
|
|
docker rm -f ${container} 2>/dev/null
|
|
|
|
if [ ${exit_status} -ne 0 ]; then
|
|
|
|
cat ${OUTPUT}/${container}-containerd.log
|
|
|
|
cat ${OUTPUT}/${container}.log
|
|
|
|
fi
|
|
|
|
done
|
2019-04-29 20:54:51 +00:00
|
|
|
rm ${KUBECONFIG}
|
|
|
|
exit ${exit_status}
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
# ---
|
|
|
|
|
|
|
|
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}"
|
|
|
|
|
|
|
|
# ---
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
wait-for-kubeconfig() {
|
2019-05-03 18:35:58 +00:00
|
|
|
while ! fetch-kubeconfig; do
|
2019-04-29 20:54:51 +00:00
|
|
|
echo "Waiting for kubeconfig to become available..."
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
}
|
|
|
|
export -f wait-for-kubeconfig
|
|
|
|
|
2019-05-02 19:04:45 +00:00
|
|
|
timeout --foreground 1m bash -c wait-for-kubeconfig
|
2019-04-29 20:54:51 +00:00
|
|
|
|
|
|
|
# ---
|
|
|
|
|
2019-05-03 18:35:58 +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-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
|
|
|
|
|
|
|
|
wait-for-nodes() {
|
|
|
|
while [[ "$(count-ready-nodes)" != "2" ]]; do
|
|
|
|
echo "Waiting for nodes to be ready..."
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
}
|
|
|
|
export -f wait-for-nodes
|
|
|
|
|
2019-05-02 19:04:45 +00:00
|
|
|
timeout --foreground 1m bash -c wait-for-nodes
|
2019-04-29 20:54:51 +00:00
|
|
|
|
|
|
|
# ---
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
wait-for-services() {
|
|
|
|
for service in coredns; do
|
|
|
|
while [[ "$(pod-ready ${service})" != "true" ]]; do
|
|
|
|
echo "Waiting for service ${service} to be ready..."
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
echo "Service ${service} is ready"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
export -f wait-for-services
|
|
|
|
|
2019-05-02 19:04:45 +00:00
|
|
|
timeout --foreground 1m bash -c wait-for-services
|
2019-04-29 20:54:51 +00:00
|
|
|
|
|
|
|
# ---
|
|
|
|
|
|
|
|
echo "Starting sonobuoy tests"
|
|
|
|
|
2019-05-02 19:04:45 +00:00
|
|
|
timeout --foreground 30m sonobuoy run \
|
2019-04-29 20:54:51 +00:00
|
|
|
--config scripts/sonobuoy-config.json \
|
|
|
|
--wait \
|
|
|
|
${@}
|
|
|
|
sonobuoy status
|
|
|
|
sonobuoy retrieve ${OUTPUT}
|
|
|
|
|
|
|
|
(
|
|
|
|
cd ${OUTPUT}
|
|
|
|
tar xzf *_sonobuoy_*.tar.gz
|
|
|
|
results="./plugins/e2e/results/e2e.log"
|
|
|
|
[ -s ${results} ] || exit 1
|
2019-05-03 18:35:58 +00:00
|
|
|
tail -20 ${results}
|
|
|
|
status=$(tail -5 ${results} | grep '^SUCCESS!.*| 0 Failed |' >/dev/null && echo passed || echo failed)
|
2019-04-29 20:54:51 +00:00
|
|
|
if [ -n "${E2E_LOG_OUTPUT}" ]; then
|
2019-05-03 18:35:58 +00:00
|
|
|
cp ${results} $(echo ${E2E_LOG_OUTPUT} | sed -e "s/-STATUS-/-${status}-/g")
|
2019-04-29 20:54:51 +00:00
|
|
|
fi
|
|
|
|
)
|