#!/bin/bash

resources=()

for I in $(/usr/bin/oc get crd | grep local.storage.openshift.io | awk '{print $1}'); do
  resources+=($I)
done

for resource in ${resources[@]}; do
  /usr/bin/oc get ${resource} --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace --no-headers 2> /dev/null | \
  while read ocresource; do
    ocobject=$(echo $ocresource | awk '{print $1}')
    ocns=$(echo $ocresource | awk '{print $2}')
    if [ -z "${ocns}" ]|[ "${ocns}" == "<none>" ]; then
      object_collection_path=must-gather/cluster-scoped-resources/${resource}
      mkdir -p ${object_collection_path}
      /usr/bin/oc get ${resource} -o yaml ${ocobject} > ${object_collection_path}/${ocobject}.yaml
    else
      object_collection_path=must-gather/namespaces/${ocns}/crs/${resource}
      mkdir -p ${object_collection_path}
      /usr/bin/oc get ${resource} -n ${ocns} -o yaml ${ocobject} > ${object_collection_path}/${ocobject}.yaml
    fi
  done
done

exit 0
