---
- shell: >
    {{ openshift.common.client_binary }} -n {{ openshift_metrics_project | quote }}
    --config={{ mktemp.stdout }}/admin.kubeconfig
    get rc hawkular-cassandra-{{node}} -o jsonpath='{.spec.replicas}' || echo 0
  vars:
    node: "{{ item }}"
  register: cassandra_replica_count
  with_sequence: count={{ openshift_metrics_cassandra_replicas }}
  changed_when: false
  failed_when: false

- name: generate hawkular-cassandra replication controllers
  template:
    src: hawkular_cassandra_rc.j2
    dest: "{{ mktemp.stdout }}/templates/hawkular-cassandra-rc{{ item }}.yaml"
  vars:
    node: "{{ item }}"
    master: "{{ (item == '1')|string|lower }}"
    replica_count: "{{cassandra_replica_count.results[item|int - 1].stdout}}"
    node_selector: "{{openshift_metrics_cassandra_nodeselector | default('') }}"
  with_sequence: count={{ openshift_metrics_cassandra_replicas }}
  changed_when: false

- set_fact: openshift_metrics_cassandra_pvc_prefix="hawkular-metrics"
  when: "not openshift_metrics_cassandra_pvc_prefix or openshift_metrics_cassandra_pvc_prefix == ''"

- name: generate hawkular-cassandra persistent volume claims
  template:
    src: pvc.j2
    dest: "{{ mktemp.stdout }}/templates/hawkular-cassandra-pvc{{ item }}.yaml"
  vars:
    obj_name: "{{ openshift_metrics_cassandra_pvc_prefix }}-{{ item }}"
    labels:
      metrics-infra: hawkular-cassandra
    access_modes: "{{ openshift_metrics_cassandra_pvc_access | list }}"
    size: "{{ openshift_metrics_cassandra_pvc_size }}"
    pv_selector: "{{ openshift_metrics_cassandra_pv_selector }}"
    storage_class_name: "{{ openshift_metrics_cassanda_pvc_storage_class_name | default('', true) }}"
  with_sequence: count={{ openshift_metrics_cassandra_replicas }}
  when:
  - openshift_metrics_cassandra_storage_type != 'emptydir'
  - openshift_metrics_cassandra_storage_type != 'dynamic'
  changed_when: false

- name: generate hawkular-cassandra persistent volume claims (dynamic)
  template:
    src: pvc.j2
    dest: "{{ mktemp.stdout }}/templates/hawkular-cassandra-pvc{{ item }}.yaml"
  vars:
    obj_name: "{{ openshift_metrics_cassandra_pvc_prefix }}-{{ item }}"
    labels:
      metrics-infra: hawkular-cassandra
    access_modes: "{{ openshift_metrics_cassandra_pvc_access | list }}"
    size: "{{ openshift_metrics_cassandra_pvc_size }}"
    pv_selector: "{{ openshift_metrics_cassandra_pv_selector }}"
    storage_class_name: "{{ openshift_metrics_cassanda_pvc_storage_class_name | default('', true) }}"
  with_sequence: count={{ openshift_metrics_cassandra_replicas }}
  when: openshift_metrics_cassandra_storage_type == 'dynamic'
  changed_when: false
