#!/bin/bash

# Resource list
resources=()

# KubeVirt HCO related namespaces
resources+=(ns/openshift-cnv ns/kubevirt-hyperconverged ns/openshift-operator-lifecycle-manager ns/openshift-marketplace)

# KubeVirt network related namespaces
resources+=(ns/cluster-network-addons ns/openshift-sdn ns/sriov-network-operator)

# KubeVirt Web UI namespaces
resources+=(ns/kubevirt-web-ui)

# HCO
resources+=(catalogsource clusterserviceversion)

# VM
resources+=(virtualmachines)

# VMI
resources+=(virtualmachineinstances virtualmachineinstancereplicasets virtualmachineinstancepresets virtualmachineinstancemigrations)

# v2v
resources+=(v2vvmwares virtualmachineimports)

resources+=(Network-attachment-definitions)

resources+=(networkaddonsconfig)

# CDI
resources+=(ns/cdi)

resources+=(datavolumes)

# NodeNetworkState
resources+=(nodenetworkstates)

# ImageStreamTag
resources+=(istag)

# Nodes and Machines
resources+=(nodes)
resources+=(machines)

# Run the collection of resources using must-gather
for resource in ${resources[@]}; do
    echo "Inspecting resource ${resource}..."
    /usr/bin/oc adm inspect --dest-dir must-gather --all-namespaces ${resource} &> /dev/null
done

# Collect HCO details (first, because it's there before anything else)
/usr/bin/gather_hco

# Collect the CRDs
/usr/bin/gather_crds

# Collect the CRs explicitely - some of them, like VMs, are collected by steps below
/usr/bin/gather_crs

# Collect the apiservices
/usr/bin/gather_apiservices

# Collect namespaces that contains VMs
/usr/bin/gather_vms_namespaces

# Collect VMs details
/usr/bin/gather_vms_details

# Collect SSP details
/usr/bin/gather_ssp

# Collect nodes details
/usr/bin/gather_nodes

# Collect CDI details
/usr/bin/gather_cdi

# Collect the webhooks
/usr/bin/gather_webhooks

# Collect v2v details
/usr/bin/gather_v2v

# Collect image details
# Workaround for: https://github.com/openshift/must-gather/issues/122
/usr/bin/gather_images

exit 0
