#!/bin/bash

source ./common

# Expect base collection path as an argument
BASE_COLLECTION_PATH=$1

# Use PWD as base path if no argument is passed
if [ "${BASE_COLLECTION_PATH}" = "" ]; then
    BASE_COLLECTION_PATH=$(pwd)
fi

NAMESPACE="openshift-logging"

CLO_COLLECTION_PATH="$BASE_COLLECTION_PATH/cluster-logging"
kibana_folder="$CLO_COLLECTION_PATH/kibana"

check_kibana_connectivity() {
  pod=$1

  echo "---- Connectivity between $pod and elasticsearch" >> $kibana_folder/$pod
  es_url=$(oc -n $NAMESPACE get pod $pod  -o jsonpath='{.spec.containers[?(@.name=="kibana")].env[?(@.name=="ELASTICSEARCH_URL")].value}')

  echo "  with ca" >> $kibana_folder/$pod
  oc -n $NAMESPACE exec $pod -c kibana -- curl -ILvs --key /etc/kibana/keys/key --cert /etc/kibana/keys/cert --cacert /etc/kibana/keys/ca -XGET $es_url &>> $kibana_folder/$pod

  echo "  without ca" >> $kibana_folder/$pod
  oc -n $NAMESPACE exec $pod -c kibana -- curl -ILkvs --key /etc/kibana/keys/key --cert /etc/kibana/keys/cert -XGET $es_url &>> $kibana_folder/$pod
}

get_kibana_cr() {
  oc get -n ${NAMESPACE} kibana.logging.openshift.io kibana -o yaml > $kibana_folder/cr
}

echo "Gathering data for visualization component"
echo "-- Checking Kibana health"
mkdir -p $kibana_folder

kibana_pods=$(oc -n $NAMESPACE get pods -l logging-infra=kibana -o jsonpath='{.items[*].metadata.name}')
for pod in $kibana_pods
do
    echo "---- Kibana pod: $pod"
    get_env $pod $kibana_folder "$NAMESPACE"
    check_kibana_connectivity $pod
done

echo "-- Gather Kibana CR"
get_kibana_cr
