---
- name: Generate initial heketi resource files
  copy:
    src: "deploy-heketi-template.yml"
    dest: "{{ mktemp.stdout }}/deploy-heketi-template.yml"

- name: deploy-heketi template
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    kind: template
    name: "deploy-heketi"
    state: present
    files:
    - "{{ mktemp.stdout }}/deploy-heketi-template.yml"

- name: Deploy deploy-heketi pod
  oc_process:
    namespace: "{{ glusterfs_namespace }}"
    template_name: "deploy-heketi"
    create: True
    params:
      IMAGE_NAME: "{{ glusterfs_heketi_image }}"
      HEKETI_ROUTE: "{{ glusterfs_heketi_url | default(['heketi-',glusterfs_name]|join) }}"
      HEKETI_USER_KEY: "{{ glusterfs_heketi_user_key }}"
      HEKETI_ADMIN_KEY: "{{ glusterfs_heketi_admin_key }}"
      HEKETI_EXECUTOR: "{{ glusterfs_heketi_executor }}"
      HEKETI_FSTAB: "{{ glusterfs_heketi_fstab }}"
      CLUSTER_NAME: "{{ glusterfs_name }}"
      HEKETI_LVM_WRAPPER: "{% if glusterfs_is_native | bool %}{{ glusterfs_heketi_lvm_wrapper }}{% endif %}"

- name: Wait for deploy-heketi pod
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    kind: pod
    state: list
    selector: "glusterfs=deploy-heketi-{{ glusterfs_name }}-pod"
  register: deploy_heketi_pod_wait
  until:
  - "deploy_heketi_pod_wait.module_results.results[0]['items'] | count > 0"
  # Pod's 'Ready' status must be True
  - "deploy_heketi_pod_wait.module_results.results[0]['items'] | lib_utils_oo_collect(attribute='status.conditions') | lib_utils_oo_collect(attribute='status', filters={'type': 'Ready'}) | map('bool') | select | list | count == 1"
  delay: 10
  retries: "{{ (glusterfs_timeout | int / 10) | int }}"

- import_tasks: heketi_set_cli.yml
  vars:
    heketi_pod: "{{ deploy_heketi_pod_wait.module_results.results[0]['items'][0] }}"

- name: Set deploy-heketi deployed fact
  set_fact:
    glusterfs_heketi_deploy_is_missing: False
