Skip to content

Commit

Permalink
Collect init container logs
Browse files Browse the repository at this point in the history
When doing the request for pods in the namespace, set init = true
when the pod have init containers. When collecting pod logs make
another request for a pods initcontainers and collect their logs.
  • Loading branch information
hjensas committed Jan 2, 2025
1 parent 17f971f commit ddf7648
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions collection-scripts/gather_ctlplane_resources
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ function gather_ctlplane_resources {
run_bg /usr/bin/oc -n "${NS}" get pvc '>' "${NAMESPACE_PATH}/${NS}/pvc.log"
run_bg /usr/bin/oc -n "${NS}" get network-attachment-definitions -o yaml '>' "${NAMESPACE_PATH}/${NS}/nad.log"

# We make a single request to get lines in the form <pod> <container> <previous_log>
# We make a single request to get lines in the form <pod> <container> <previous_log> <init_containers>
# mark pods that are in Pending state (they won't have
# a status.containerStatuses field) with a null container name
data=$(oc -n "${NS}" get pods -o json | jq -r '
.items[] |
.metadata.name as $pod |
.status.containerStatuses[]? // null |
"\($pod) \(.name) \(.lastState | if .terminated then true else false end)"
"\($pod) \(.status.containerStatuses[]?.name // null) \(.status.containerStatuses[]?.lastState // null | if .terminated then true else false end) \(.status.initContainerStatuses[]? // null | if . then true else false end)"
')
while read -r pod container previous; do
while read -r pod container previous init; do
pod_dir="${NAMESPACE_PATH}/${NS}/pods/${pod}"
log_dir="${pod_dir}/logs"
if [ ! -d "$log_dir" ]; then
Expand All @@ -85,6 +84,13 @@ function gather_ctlplane_resources {
if [[ "$previous" == true ]]; then
run_bg oc -n "$NS" logs "$pod" -c "$container" --previous '>' "${log_dir}/${container}-previous.log";
fi
if [[ "$init" == true ]]; then
echo "Dump init container logs from ${pod} pod";
initcontainers=$(oc -n "${NS}" get pod "${pod}" -o json | jq -r '.status.initContainerStatuses[]?.name')
for initcontainer in ${initcontainers}; do
run_bg oc -n "$NS" logs "$pod" -c "$initcontainer" '>' "${log_dir}/${initcontainer}.log"
done
fi
done <<< "$data"

# get the required resources
Expand Down

0 comments on commit ddf7648

Please sign in to comment.