#!/bin/bash

mkdir -p /must-gather/

# generate /must-gather/version file
DIR_NAME=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
. ${DIR_NAME}/version
echo "node-maintenance-operator/must-gather" > /must-gather/version
version >> /must-gather/version

# get namespace of node-maintenance-operator
NMO_NAMESPACE=$( oc get pods -A -l name='node-maintenance-operator' -o=jsonpath='{.items[0].metadata.namespace}{"\n"}' )
# fallback to default
[ -z "${NMO_NAMESPACE}" ] && NMO_NAMESPACE="openshift-operators"

# TODO get nmo logs

# init resource list
resources=()

# nodes
resources+=(nodes)

# node maintenance CRs
resources+=(nodemaintenances)

# run the collection of resources using must-gather
for resource in ${resources[@]}; do
  /usr/bin/oc adm inspect --dest-dir must-gather --all-namespaces ${resource}
done

exit 0
