#!/usr/bin/env bash

function namespace() {
  # we control the subs, so this is the most reliable way to get the namespace
  ns=$( oc get subs -A --field-selector metadata.name='performance-addon-operator-subscription' -o=jsonpath='{.items[0].metadata.namespace}{"\n"}' 2> /dev/null )
  # trying again with the pods, which are _usually_ reliable - but users can change them
  [ -z "${ns}" ] && ns=$( oc get pods -A -l name='performance-operator' -o=jsonpath='{.items[0].metadata.namespace}{"\n"}' 2> /dev/null )
  # namespace suggested by the documentation. This is a fancier way to check for its existence
  [ -z "${ns}" ] && ns=$( oc get ns openshift-performance-addon-operator -o jsonpath='{.metadata.name}{"\n"}' 2> /dev/null )
  # we should never get there. This is the last resort.
  [ -z "${ns}" ] && ns="openshift-operators"
  echo ${ns}
}
