#!/bin/bash

# generate /must-gather/version file
. 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-node-maintenance"

# resource list
resources=()

# nodes & machines
resources+=(nodes)
resources+=(machines)

# node maintenance CRs
resources+=(nodemaintenance)

# nodes
resources+=(nodes)

# 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
