---
- name: Check that hawkular_metrics_hostname is set
  fail: msg='the openshift_metrics_hawkular_hostname variable is required'
  when: openshift_metrics_hawkular_hostname is not defined

- name: Check the value of openshift_metrics_cassandra_storage_type
  fail:
    msg: >
      openshift_metrics_cassandra_storage_type ({{ openshift_metrics_cassandra_storage_type }})
      is invalid, must be one of: emptydir, pv, dynamic
  when:
  - openshift_metrics_cassandra_storage_type not in openshift_metrics_cassandra_storage_types
  - not (openshift_metrics_heapster_standalone | bool)

- name: list existing secrets
  command: >
    {{ openshift_client_binary }} -n {{ openshift_metrics_project }}
    --config={{ mktemp.stdout }}/admin.kubeconfig
    get secrets -o name
  register: metrics_secrets
  changed_when: false

- name: Get namespace object
  oc_obj:
    state: list
    kind: namespace
    name: "{{ openshift_metrics_project }}"
    namespace: "{{ openshift_metrics_project }}"
  register: openshift_metrics_namespace_object

- name: Get namespace annotations
  set_fact:
    openshift_metrics_namespace_annotations: >
      {{ openshift_metrics_namespace_object.results.results[0].metadata.annotations }}
  when:
  - openshift_metrics_namespace_object is defined
  - openshift_metrics_namespace_object.results is defined
  - openshift_metrics_namespace_object.results.results is defined
  - openshift_metrics_namespace_object.results.results[0] is defined
  - openshift_metrics_namespace_object.results.results[0].metadata is defined

- name: Get namespace fsGroup
  set_fact:
    openshift_metrics_namespace_fsgroup: >
      {{ openshift_metrics_namespace_annotations['openshift.io/sa.scc.supplemental-groups'].split("/")[0] | trim }}
  when:
  - openshift_metrics_namespace_annotations is defined
  - openshift_metrics_namespace_annotations['openshift.io/sa.scc.supplemental-groups'] is defined

- name: Get namespace userID
  set_fact:
    openshift_metrics_namespace_uid: >
      {{ openshift_metrics_namespace_annotations['openshift.io/sa.scc.uid-range'].split("/")[0] | trim }}
  when:
  - openshift_metrics_namespace_annotations is defined
  - openshift_metrics_namespace_annotations['openshift.io/sa.scc.uid-range'] is defined

- name: Get namespace seLinuxOptions
  set_fact:
    openshift_metrics_namespace_selinux: >
      {{ openshift_metrics_namespace_annotations['openshift.io/sa.scc.mcs'] | trim }}
  when:
  - openshift_metrics_namespace_annotations is defined
  - openshift_metrics_namespace_annotations['openshift.io/sa.scc.mcs'] is defined

- fail:
    msg: Cannot get annotations on {{ openshift_metrics_project }} namespace
  when: >
    openshift_metrics_namespace_uid is not defined or
    openshift_metrics_namespace_selinux is not defined or
    openshift_metrics_namespace_fsgroup is not defined
