From 868d81e76a3aacf0c0cbecf9a36d8e46ebea6f53 Mon Sep 17 00:00:00 2001 From: Niraj Yadav Date: Wed, 7 Aug 2024 21:08:52 +0530 Subject: [PATCH] Include pod metadata in must-gather output This PR adds support of fetching the pod metadata using the kubernetes API. An additional pod-metadata.json file is created in the report with the dump of entire metadata for the pod. A much simpler debug log stating the image version being used is printed to the console as well. Signed-off-by: Niraj Yadav --- collection-scripts/gather | 3 +++ collection-scripts/utils.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/collection-scripts/gather b/collection-scripts/gather index e069369..d40356f 100755 --- a/collection-scripts/gather +++ b/collection-scripts/gather @@ -132,6 +132,9 @@ START_TIME=$(date +%r) start=$(date +%s) dbglog "collection started at: ${START_TIME}" +# Print and export must-gather pod details +export_pod_image_details + if [ -n "${LOG_FILTER_ARGS:-}" ]; then dbglog "Logs will be filtered using: ${LOG_FILTER_ARGS}" fi diff --git a/collection-scripts/utils.sh b/collection-scripts/utils.sh index e9892a6..f44bda7 100755 --- a/collection-scripts/utils.sh +++ b/collection-scripts/utils.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2155 # # Please Note: @@ -79,7 +80,33 @@ parse_since_time() { fi } +# export_pod_image_details fetches the pod metadata +# using kubernetes API +function export_pod_image_details() { + # We do not override the hostname in odf-mg, hence hostname = pod name + local POD_NAME=$(hostname) + + # Kubernetes API token and endpoints + local API_URL="https://kubernetes.default.svc" + local NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace) + local TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) + + # Get pod metadata + local POD_METADATA=$(curl -s --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt \ + -H "Authorization: Bearer $TOKEN" \ + "$API_URL/api/v1/namespaces/$NAMESPACE/pods/$POD_NAME") + + # Extract image details + local IMAGE=$(echo "$POD_METADATA" | awk -F'"' '/"image":/ {print $4; exit}') + + # Also save the pod metadata to a file + echo "$POD_METADATA" >"${BASE_COLLECTION_PATH}"/pod-metadata.json + + dbglog "must-gather is using image: $IMAGE" +} + # Export the functions so that the file needs to be sourced only once export -f dbglog export -f dbglogf export -f parse_since_time +export -f export_pod_image_details