#!/bin/bash
BASE_COLLECTION_PATH="/must-gather"
mkdir -p ${BASE_COLLECTION_PATH}

echo "Collecting operator pod logs"
operatorPodLogCollectionPath="${BASE_COLLECTION_PATH}/namespaces"
operatorPodLogCollectionJSONPath="{range .items[*]}mkdir -p ${operatorPodLogCollectionPath}/{@.metadata.namespace}/logs/{@.metadata.name};oc logs {@.metadata.name}  --all-containers -n {@.metadata.namespace} &> ${operatorPodLogCollectionPath}/{@.metadata.namespace}/logs/{@.metadata.name}/{@.metadata.name}.log;{end}"
operatorPodPreviousLogCollectionJSONPath="{range .items[*]}mkdir -p ${operatorPodLogCollectionPath}/{@.metadata.namespace}/logs/{@.metadata.name};oc logs {@.metadata.name} -p --all-containers -n {@.metadata.namespace} &> ${operatorPodLogCollectionPath}/{@.metadata.namespace}/logs/{@.metadata.name}/{@.metadata.name}-previous.log;{end}"
{
    timeout 120 oc get pods --all-namespaces -l 'name in (local-storage-operator,ocs-operator)' -o jsonpath="${operatorPodLogCollectionJSONPath}";
    timeout 120 oc get pods --all-namespaces -l 'name in (local-storage-operator,ocs-operator)' -o jsonpath="${operatorPodPreviousLogCollectionJSONPath}";
    timeout 120 oc get pods --all-namespaces -l 'app in (noobaa,rook-ceph-operator)' -o jsonpath="${operatorPodLogCollectionJSONPath}";
    timeout 120 oc get pods --all-namespaces -l 'app in (noobaa,rook-ceph-operator)' -o jsonpath="${operatorPodPreviousLogCollectionJSONPath}";
} >> collector.sh
chmod +x collector.sh
./collector.sh
# Resource List
resources=()
# collect storagecluster resources
resources+=(storageclusters)

# collect OB/OBC resoureces
resources+=(objectbucketclaims)
resources+=(objectbuckets)

# Add general resources to list if necessary 

# Run the Collection of Resources using must-gather
for resource in "${resources[@]}"; do
    echo "Collection dump of ${resource}"
    timeout 120 openshift-must-gather --base-dir=${BASE_COLLECTION_PATH} inspect "${resource}" --all-namespaces
done

# Call other gather scripts
gather_noobaa_resources ${BASE_COLLECTION_PATH}
gather_ceph_resources ${BASE_COLLECTION_PATH}

find "${BASE_COLLECTION_PATH}" -empty -delete
exit 0