#!/bin/bash -x

BASE_COLLECTION_PATH="${BASE_COLLECTION_PATH:-/must-gather}"
PROS=${PROS:-5}

function gather_cdi_pod() {
  line=$1
  ns=$(echo "${line}" | awk -F_ '{print $1}')
  pod=$(echo "${line}" | awk -F_ '{print $2}')

  echo "NS: ${ns}, POD: ${pod}"
  pod_collection_path="${BASE_COLLECTION_PATH}/namespaces/${ns}/${pod}"
  mkdir -p "${pod_collection_path}"

  oc adm inspect --dest-dir "${BASE_COLLECTION_PATH}" -n "${ns}" "pod/${pod}"
  oc logs "${pod}" -n "${ns}" > "${pod_collection_path}/${pod}.log"
}

function gather_pvc() {
  line=$1
  ns=$(echo "${line}" | awk -F_ '{print $1}')
  pvc=$(echo "${line}" | awk -F_ '{print $2}')

  echo "NS: ${ns}, PVC: ${pvc}"
  pvc_collection_path="${BASE_COLLECTION_PATH}/namespaces/${ns}/core/persistentvolumeclaims"
  mkdir -p "$pvc_collection_path"

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

export -f gather_cdi_pod
cdi_pods=$(oc get pods -l cdi.kubevirt.io --no-headers -A | awk '{print $1 "_" $2}')
echo "${cdi_pods}" | tr ' ' '\n' | xargs -t -I{} -P "${PROS}" --max-args=1 sh -c 'gather_cdi_pod $1' -- {}

export -f gather_pvc
pvcs=$(oc get pvc --no-headers -A | awk '{print $1 "_" $2}')
echo "${pvcs}" | tr ' ' '\n' | xargs -t -I{} -P "${PROS}" --max-args=1 sh -c 'gather_pvc $1' -- {}
