---
- name: Check for existing deploy-heketi pod
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    state: list
    kind: pod
    selector: "glusterfs=deploy-heketi-{{ glusterfs_name }}-pod"
  register: deploy_heketi_pod_check

# deploy-heketi pod is not missing when there are one or more pods with matching labels whose 'Ready' status is True
- name: Check if need to deploy deploy-heketi
  set_fact:
    glusterfs_heketi_deploy_is_missing: "{{ heketi_pod_results_count | int == 0 or heketi_pod_results_ready_count | int == 0 }}"
  vars:
    heketi_pod_results: "{{ deploy_heketi_pod_check.module_results.results[0]['items'] }}"
    heketi_pod_results_count: "{{ heketi_pod_results | count }}"
    heketi_pod_results_ready_count: "{{ heketi_pod_results | lib_utils_oo_collect(attribute='status.conditions') | lib_utils_oo_collect(attribute='status', filters={'type': 'Ready'}) | map('bool') | select | list | count }}"
  when: glusterfs_heketi_deploy_is_missing

- name: Check for existing heketi pod
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    state: list
    kind: pod
    selector: "glusterfs=heketi-{{ glusterfs_name }}-pod"
  register: heketi_pod_check

# heketi pod is not missing when there are one or more pods with matching labels whose 'Ready' status is True
- name: Check if need to deploy heketi
  set_fact:
    glusterfs_heketi_is_missing: "{{ heketi_pod_results_count | int == 0 or heketi_pod_results_ready_count | int == 0 }}"
  vars:
    heketi_pod_results: "{{ heketi_pod_check.module_results.results[0]['items'] }}"
    heketi_pod_results_count: "{{ heketi_pod_results | count }}"
    heketi_pod_results_ready_count: "{{ heketi_pod_results | lib_utils_oo_collect(attribute='status.conditions') | lib_utils_oo_collect(attribute='status', filters={'type': 'Ready'}) | map('bool') | select | list | count }}"
  when: glusterfs_heketi_is_missing

- import_tasks: heketi_set_cli.yml
  vars:
    deploy_heketi_pod_results: "{{ deploy_heketi_pod_check.module_results.results[0]['items'] }}"
    heketi_pod_results: "{{ heketi_pod_check.module_results.results[0]['items'] }}"
    heketi_pod: "{{ heketi_pod_results | default(deploy_heketi_pod_results, true) | default([''], true) | first }}"
