#!/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-operators-redhat

CLO_COLLECTION_PATH="$BASE_COLLECTION_PATH/cluster-logging"
eo_folder="$CLO_COLLECTION_PATH/eo"

echo "Gathering data for elasticsearch-operator"
mkdir -p "$eo_folder"

pods=$(oc -n $NAMESPACE get pods -l name=elasticsearch-operator -o jsonpath='{.items[*].metadata.name}')
for pod in $pods
do
    get_env $pod $eo_folder $NAMESPACE "Dockerfile-*operator*"
done

oc -n $NAMESPACE get deployment elasticsearch-operator -o yaml > $eo_folder/deployment

csv_name="$(oc -n $NAMESPACE get csv -o name | grep 'elasticsearch-operator')"
oc -n $NAMESPACE get "${csv_name}" -o yaml > "${eo_folder}/csv"
oc -n openshift-logging exec -c elasticsearch \
    $(oc -n openshift-logging get pods -l component=elasticsearch -o jsonpath='{.items[0].metadata.name}') \
    -- indices > ${eo_folder}/indices.txt

oc -n $NAMESPACE get deployment elasticsearch-operator -o wide > ${eo_folder}/eo-deployment.txt 2>&1
oc -n $NAMESPACE describe deployment elasticsearch-operator > ${eo_folder}/eo-deployment.describe 2>&1
oc -n $NAMESPACE logs deployment/elasticsearch-operator > ${eo_folder}/elasticsearch-operator.logs 2>&1
