Created
March 15, 2023 16:25
-
-
Save Badbond/0f685d133763864a0760bbfc477e1f82 to your computer and use it in GitHub Desktop.
JaCoCo -- Extract coverage data from Kubernetes pods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e -u -o pipefail | |
if [ "${#}" -lt 4 ]; then | |
echo "Usage: ${0} <path-to-jacococli.jar> <kubernetes-context> <kubernetes-namespace> <kubernetes-pod-selector>" | |
echo "For selector syntax, see https://kubernetes.io/docs/concepts/overview/working-with-objects/labels." | |
exit 1 | |
fi | |
JACOCO_CLI_LOCAL="$(realpath "${1}")" | |
CONTEXT="${2}" | |
NAMESPACE="${3}" | |
POD_SELECTOR="${4}" | |
# All resources are stored in a temporary directory. | |
TMP_DIR='/tmp/jacoco-export' | |
mkdir -p "${TMP_DIR}" | |
pushd "${TMP_DIR}" | |
# Fetch coverage data from all pods. | |
kubectl get pods \ | |
--context="${CONTEXT}" \ | |
--namespace="${NAMESPACE}" \ | |
--selector="${POD_SELECTOR}" \ | |
--no-headers \ | |
-o custom-columns=":metadata.name" \ | |
| while read POD; do | |
# One pod at a time, open a tunnel, connect to the JaCoCo agent to fetch | |
# the coverage data and close the tunnel again. | |
echo $CONTEXT - $POD | |
( | |
kubectl port-forward \ | |
--context="${CONTEXT}" \ | |
--namespace "${NAMESPACE}" \ | |
"${POD}" \ | |
"6300:6300" & | |
trap "kill ${!}" ERR EXIT HUP INT TERM | |
java -jar "${JACOCO_CLI_LOCAL}" dump --destfile "jacoco-${POD}.exec" | |
) | |
done | |
# Merge the coverage data into a single file. | |
java -jar "${JACOCO_CLI_LOCAL}" merge --destfile jacoco.exec jacoco-*.exec | |
popd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment