---
- name: Ensure that hawkular-metrics-schema job has nodes to run on
  import_role:
    name: openshift_control_plane
    tasks_from: ensure_nodes_matching_selector.yml
  vars:
    openshift_master_ensure_nodes_selector: "{{ openshift_metrics_schema_job_nodeselector | map_to_pairs }}"
    openshift_master_ensure_nodes_service: Hawkular Metrics schema job

- name: list installed jobs
  command: >
    {{ openshift_client_binary }} -n {{ openshift_metrics_project }} --config={{ mktemp.stdout }}/admin.kubeconfig
    get jobs
  register: jobs

# We cannot use oc apply here because the Job template has immutable fields
# on which oc apply will fail.
- name: remove hawkular-metrics-schema job
  command: >
    {{ openshift_client_binary }} -n {{ openshift_metrics_project }} --config={{ mktemp.stdout }}/admin.kubeconfig
    delete job hawkular-metrics-schema
  register: delete_schema_job
  when: "'hawkular-metrics-schema' in jobs.stdout"

- name: generate hawkular-metrics schema job
  template:
    src: hawkular_metrics_schema_job.j2
    dest: "{{ mktemp.stdout }}/templates/hawkular_metrics_schema_job.yaml"
  vars:
    node_selector: "{{openshift_metrics_schema_job_nodeselector | default('') }}"
  changed_when: false
