---
# set facts
- include_tasks: facts.yaml

# namespace
- name: Add prometheus project
  oc_project:
    state: present
    name: "{{ openshift_prometheus_namespace }}"
    node_selector: ""
    description: Prometheus

- name: Make temp directory for node exporter template
  command: mktemp -d /tmp/prometheus-ansible-XXXXXX
  register: mktemp
  changed_when: False

- name: Copy admin client config
  command: >
    cp {{ openshift.common.config_base }}/master//admin.kubeconfig {{ mktemp.stdout }}/admin.kubeconfig
  changed_when: false

# create clusterrolebinding for prometheus-node-exporter serviceaccount
- name: Set hostaccess SCC for prometheus-node-exporter
  oc_adm_policy_user:
    state: present
    namespace: "{{ openshift_prometheus_namespace }}"
    resource_kind: scc
    resource_name: hostaccess
    user: "system:serviceaccount:{{ openshift_prometheus_namespace }}:prometheus-node-exporter"

- name: Copy node exporter templates to temp directory
  copy:
    src: "{{ item }}"
    dest: "{{ mktemp.stdout }}/{{ item }}"
  with_items:
    - "{{ __node_exporter_template_file }}"

- name: Apply the node exporter template file
  shell: >
    {{ openshift_client_binary }} process -f "{{ mktemp.stdout }}/{{ __node_exporter_template_file }}"
    --param IMAGE="{{ openshift_prometheus_node_exporter_image }}"
    --param MEMORY_REQUESTS="{{ openshift_prometheus_node_exporter_memory_requests }}"
    --param CPU_REQUESTS="{{ openshift_prometheus_node_exporter_cpu_requests }}"
    --param MEMORY_LIMITS="{{ openshift_prometheus_node_exporter_memory_limit }}"
    --param CPU_LIMITS="{{ openshift_prometheus_node_exporter_cpu_limit }}"
    --config={{ mktemp.stdout }}/admin.kubeconfig
    -n "{{ openshift_prometheus_namespace }}"
    | {{ openshift_client_binary }} apply --config={{ mktemp.stdout }}/admin.kubeconfig -f - -n "{{ openshift_prometheus_namespace }}"

- name: Remove temp directory
  file:
    state: absent
    name: "{{ mktemp.stdout }}"
  changed_when: False
