#!/bin/bash

BASE_COLLECTION_PATH="/must-gather"
mkdir -p ${BASE_COLLECTION_PATH}

ANSIBLECRD="ansiblejobs.tower.ansible.com automationcontrollerbackups.automationcontroller.ansible.com automationcontrollerrestores.automationcontroller.ansible.com automationcontrollers.automationcontroller.ansible.com automationhubbackups.automationhub.ansible.com automationhubrestores.automationhub.ansible.com automationhubs.automationhub.ansible.com jobtemplates.tower.ansible.com"

for i in $ANSIBLECRD; do
   oc adm inspect $i --all-namespaces --dest-dir=must-gather 
   ANSIBLENS+=`oc get $i --all-namespaces --no-headers=true 2> /dev/null | awk '{print "\n" $1}'`
done

NS=`echo ${ANSIBLENS[@]}  | tr ' ' '\n'  | sort | uniq`

for a in $NS; do
  oc adm inspect ns/"$a" --all-namespaces  --dest-dir=must-gather
  oc get pods -n $a >> ${BASE_COLLECTION_PATH}/gather-aap.log
done

echo "WARNING: Collecting kernel messages on ALL nodes in your cluster. This could take a large amount of time."
oc adm node-logs --role=master -l kubernetes.io/os=linux --grep kernel > "${BASE_COLLECTION_PATH}/master_dmesg.log"
oc adm node-logs --role=worker -l kubernetes.io/os=linux --grep kernel > "${BASE_COLLECTION_PATH}/worker_dmesg.log"
