#!/bin/bash

BASE_COLLECTION_PATH="/must-gather"

for ns in $(oc get namespace "$ns" --no-headers -o=custom-columns=NAME:.metadata.name 2> /dev/null)
do
    ns_collection_path=$BASE_COLLECTION_PATH/namespaces/$ns/
    mkdir -p "$ns_collection_path"

    for pod in $(oc get pods -n "$ns" -l cdi.kubevirt.io --no-headers -o=custom-columns=NAME:.metadata.name 2>/dev/null)
    do
        echo "NS: $ns, POD: $pod"
        pod_collection_path=$ns_collection_path/$pod
        mkdir -p "$pod_collection_path"

        /usr/bin/oc adm inspect --dest-dir must-gather -n "$ns" "pod/$pod"
        oc logs "$pod" -n "$ns" > "$pod_collection_path/$pod.log"
    done

    for pvc in $(oc get pvc -n "$ns" --no-headers -o=custom-columns=NAME:.metadata.name 2>/dev/null)
    do
        echo "NS: $ns, PVC: $pvc"
        pvc_collection_path=$ns_collection_path/core/persistentvolumeclaims
        mkdir -p "$pvc_collection_path"

        /usr/bin/oc adm inspect --dest-dir must-gather -n "$ns" "pvc/$pvc"
        oc describe pvc "$pvc" -n "$ns" > "$pvc_collection_path/${pvc}_describe.txt"
    done

done
