#!/bin/bash

BASE_COLLECTION_PATH="/must-gather"

for ns in $(oc get namespace --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 --no-headers -o=custom-columns=NAME:.metadata.name 2>/dev/null)
    do
        if [[ $pod == kubevirt-v2v-conversion-* || $pod == vm-import-* ]]; then
            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
        fi
    done
done
