#!/bin/bash
#set -x
# Copyright (c) 2021 Red Hat, Inc.
# Copyright Contributors to the Open Cluster Management project


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

CLUSTER="SPOKE"
HUBNAMESPACE=""
MCNAMESPACE=""

check_managed_clusters() {
    echo "The list of managed clusters that are configured on this Hub:" >> ${BASE_COLLECTION_PATH}/gather-managed.log
    #These calls will change with new API
    oc get managedclusters --all-namespaces >> ${BASE_COLLECTION_PATH}/gather-managed.log
    
    #to capture details in the managed cluster namespace to debug hive issues
    #refer https://github.com/open-cluster-management/backlog/issues/2682
    MCNAMESPACE=`oc get managedclusters --all-namespaces --no-headers=true -o custom-columns="NAMESPACE:.metadata.name"`
    for mcns in ${MCNAMESPACE[@]};
    do
        #oc kubectl get pods -n "$mcns" >> ${BASE_COLLECTION_PATH}/gather-managed.log
        oc adm inspect  ns/"$mcns"  --dest-dir=must-gather
    done

}

check_if_hub () {
    HUBNAMESPACE=`oc get multiclusterhubs.operator.open-cluster-management.io --all-namespaces --no-headers=true| awk '{ print $1 }'`
    echo "$HUBNAMESPACE"
     #if [[ "$NAMESPACE" != error* ]];
     #   then CLUSTER="HUB"
     #fi  
     if [[ -z "$HUBNAMESPACE" ]] ;
        then CLUSTER="SPOKE"
     else  CLUSTER="HUB"
     fi 
}

gather_spoke () {

    oc adm inspect subscriptions.apps.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect helmreleases.apps.open-cluster-management.io --all-namespaces  --dest-dir=must-gather

    oc adm inspect klusterlets.operator.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect clusterclaims.cluster.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect ns/open-cluster-management-agent --dest-dir=must-gather
    oc adm inspect ns/open-cluster-management-agent-addon --dest-dir=must-gather
    #oc adm inspect endpoints.multicloud.ibm.com --all-namespaces  --dest-dir=must-gather
    oc adm inspect workmanagers.agent.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect applicationmanagers.agent.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect certpolicycontrollers.agent.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect iampolicycontrollers.agent.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect policycontrollers.agent.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect searchcollectors.agent.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    
    oc adm inspect policies.policy.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect certificatepolicies.policy.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect iampolicies.policy.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect cispolicies.policy.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    
    oc adm inspect ns/open-cluster-management-observability --dest-dir=must-gather
    oc adm inspect ns/open-cluster-management-addon-observability --dest-dir=must-gather

    oc adm inspect ns/openshift-gatekeeper-system --dest-dir=must-gather
    oc adm inspect ns/openshift-operators --dest-dir=must-gather # gatekeeper operator will be installed in this ns in production
    oc adm inspect ns/openshift-gatekeeper-operator --dest-dir=must-gather
}

check_if_hub

echo "$CLUSTER"

case "$CLUSTER" in 
    #case 1 
    "HUB")

    check_managed_clusters
    #oc adm inspect  ns/open-cluster-management  --dest-dir=must-gather
    oc get pods -n "$HUBNAMESPACE" > ${BASE_COLLECTION_PATH}/gather-acm.log 
    oc adm inspect  ns/"$HUBNAMESPACE"  --dest-dir=must-gather
    oc adm inspect  ns/open-cluster-management-hub  --dest-dir=must-gather
    # request from https://bugzilla.redhat.com/show_bug.cgi?id=1853485
    oc get proxy -o yaml > ${BASE_COLLECTION_PATH}/gather-proxy.log
    oc adm inspect  ns/hive  --dest-dir=must-gather
    oc adm inspect  multiclusterhubs.operator.open-cluster-management.io --all-namespaces  --dest-dir=must-gather

    #oc adm inspect endpointconfigs.multicloud.ibm.com --all-namespaces  --dest-dir=must-gather
    oc adm inspect hiveconfigs.hive.openshift.io --all-namespaces  --dest-dir=must-gather

    oc adm inspect baremetalassets.inventory.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect baremetalhosts.metal3.io --all-namespaces  --dest-dir=must-gather

    oc adm inspect applications.app.k8s.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect channels.apps.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect deployables.apps.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect helmreleases.apps.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect placementrules.apps.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect subscriptions.apps.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect policies.policy.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect policyautomations.policy.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect placementbindings.policy.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect clusterdeployments.hive.openshift.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect syncsets.hive.openshift.io --all-namespaces --dest-dir=must-gather
    oc adm inspect clusterimagesets.hive.openshift.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect machinesets.machine.openshift.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect clustercurators.cluster.open-cluster-management.io --all-namespaces --dest-dir=must-gather

    oc adm inspect  managedclusterviews.view.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect  managedclusteractions.action.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect  manifestworks.work.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect  managedclusters.cluster.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect  managedclusterinfos.internal.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect  clustermanagers.operator.open-cluster-management.io --all-namespaces  --dest-dir=must-gather

    oc adm inspect  klusterletaddonconfigs.agent.open-cluster-management.io --all-namespaces  --dest-dir=must-gather

    oc adm inspect validatingwebhookconfigurations.admissionregistration.k8s.io --all-namespaces --dest-dir=must-gather
    oc adm inspect mutatingwebhookconfigurations.admissionregistration.k8s.io --all-namespaces --dest-dir=must-gather

    oc adm inspect observabilityaddons.observability.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect multiclusterobservabilities.observability.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect managedclusteraddons.addon.open-cluster-management.io  --all-namespaces  --dest-dir=must-gather
    oc adm inspect ns/open-cluster-management-observability --dest-dir=must-gather
    oc adm inspect observatoria.core.observatorium.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect ns/open-cluster-management-addon-observability --dest-dir=must-gather

    oc adm inspect  discoveredclusterrefreshes.discovery.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect  discoveredclusters.discovery.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect  discoveryconfigs.discovery.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect searchoperators.search.open-cluster-management.io --all-namespaces  --dest-dir=must-gather
    oc adm inspect searchcustomizations.search.open-cluster-management.io --all-namespaces  --dest-dir=must-gather

    oc adm inspect ns/openshift-monitoring  --dest-dir=must-gather
    oc adm inspect ns/open-cluster-management-issuer  --dest-dir=must-gather
    
    # gather hub imported as managed
    gather_spoke
    ;;

      
    #case 2 
    "SPOKE")
    gather_spoke
    ;; 
      
   
esac 



exit 0
