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


# Resource List
resources=()
# collect storagecluster resources

# TODO: Re enable the collection of storagecluster via inspect command
# resources+=(storageclusters)

# NOTE: This is a temporary fix for collecting the storagecluster as we are not able to collect the storagecluster using the inspect command
{ oc get storageclusters --all-namespaces -o yaml; } > $BASE_COLLECTION_PATH/storagecluster.yaml 2>&1

# 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 "collecting dump of ${resource}" | tee -a  ${BASE_COLLECTION_PATH}/gather-debug.log
    { oc adm --dest-dir=${BASE_COLLECTION_PATH} inspect "${resource}" --all-namespaces; } >> ${BASE_COLLECTION_PATH}/gather-debug.log 2>&1
done

# collection path for OC commands
mkdir -p ${BASE_COLLECTION_PATH}/oc_output/

# Command List
commands=()

# collect oc output of OC commands
commands+=("get clusterversion -oyaml")
commands+=("get pods -owide -n openshift-storage")
commands+=("get nodes --show-labels")
commands+=("describe nodes")
commands+=("describe pods -n openshift-storage")
commands+=("get pvc --all-namespaces")
commands+=("get pv")
commands+=("get infrastructures.config -oyaml")
commands+=("get subscription -n openshift-storage")
commands+=("get csv -n openshift-storage")
commands+=("get sc")
commands+=("get installplan -n openshift-storage")
commands+=("get events -n openshift-storage")

# Run the Collection of Resources to list
for command in "${commands[@]}"; do
     echo "collecting oc command ${command}" | tee -a ${BASE_COLLECTION_PATH}/gather-debug.log
     COMMAND_OUTPUT_FILE=${BASE_COLLECTION_PATH}/oc_output/${command// /_}
     timeout 120 oc ${command} >> "${COMMAND_OUTPUT_FILE}"
done

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

echo "deleting empty files" >> ${BASE_COLLECTION_PATH}/gather-debug.log
find "${BASE_COLLECTION_PATH}" -empty -delete >> ${BASE_COLLECTION_PATH}/gather-debug.log 2>&1
exit 0
