---
- fail:
    msg: Application logs destination is required
  when: not openshift_logging_mux_app_host or openshift_logging_mux_app_host == ''

- fail:
    msg: Operations logs destination is required
  when: not openshift_logging_mux_ops_host or openshift_logging_mux_ops_host == ''

- fail:
    msg: openshift_logging_mux_default_namespaces is not supported; use openshift_logging_mux_namespaces instead.
  when: openshift_logging_mux_default_namespaces is defined

- name: Ensure that Logging Mux has nodes to run on
  include_role:
    name: openshift_control_plane
    tasks_from: ensure_nodes_matching_selector.yml
  vars:
    openshift_master_ensure_nodes_selector: "{{ openshift_logging_mux_nodeselector | map_to_pairs }}"
    openshift_master_ensure_nodes_service: Logging Mux

# allow passing in a tempdir
- name: Create temp directory for doing work in
  command: mktemp -d /tmp/openshift-logging-ansible-XXXXXX
  register: mktemp
  changed_when: False

- set_fact:
    tempdir: "{{ mktemp.stdout }}"

- name: Create templates subdirectory
  file:
    state: directory
    path: "{{ tempdir }}/templates"
    mode: 0755
  changed_when: False

# we want to make sure we have all the necessary components here

# create service account
- name: Create Mux service account
  oc_serviceaccount:
    state: present
    name: "aggregated-logging-mux"
    namespace: "{{ openshift_logging_mux_namespace }}"
    image_pull_secrets: "{{ openshift_logging_mux_image_pull_secret }}"
    secrets:
      - logging-mux
  when: openshift_logging_mux_image_pull_secret != ''

- name: Create Mux service account
  oc_serviceaccount:
    state: present
    name: "aggregated-logging-mux"
    namespace: "{{ openshift_logging_mux_namespace }}"
    secrets:
      - logging-mux
  when:
    - openshift_logging_mux_image_pull_secret == ''

# set service account scc
- name: Set privileged permissions for Mux
  oc_adm_policy_user:
    namespace: "{{ openshift_logging_mux_namespace }}"
    resource_kind: scc
    resource_name: privileged
    state: present
    user: "system:serviceaccount:{{ openshift_logging_mux_namespace }}:aggregated-logging-mux"

# set service account permissions
- name: Set cluster-reader permissions for Mux
  oc_adm_policy_user:
    namespace: "{{ openshift_logging_mux_namespace }}"
    resource_kind: cluster-role
    resource_name: cluster-reader
    state: present
    user: "system:serviceaccount:{{ openshift_logging_mux_namespace }}:aggregated-logging-mux"

# set hostmount-anyuid permissions
- name: Set hostmount-anyuid permissions for Mux
  oc_adm_policy_user:
    namespace: "{{ openshift_logging_mux_namespace }}"
    resource_kind: scc
    resource_name: hostmount-anyuid
    state: present
    user: "system:serviceaccount:{{ openshift_logging_mux_namespace }}:aggregated-logging-mux"

# create Mux configmap
- copy:
    src: "fluent.conf"
    dest: "{{mktemp.stdout}}/fluent-mux.conf"
  changed_when: no

- copy:
    src: "secure-forward.conf"
    dest: "{{mktemp.stdout}}/secure-forward-mux.conf"
  changed_when: no

- include_role:
    name: openshift_logging
    tasks_from: patch_configmap_files.yaml
  vars:
    configmap_name: "logging-mux"
    configmap_namespace: "{{ openshift_logging_mux_namespace }}"
    configmap_file_names:
      - current_file: "fluent.conf"
        new_file: "{{ tempdir }}/fluent-mux.conf"
      - current_file: "secure-forward.conf"
        new_file: "{{ tempdir }}/secure-forward-mux.conf"

- name: Set Mux configmap
  oc_configmap:
    state: present
    name: "logging-mux"
    namespace: "{{ openshift_logging_mux_namespace }}"
    from_file:
      fluent.conf: "{{ tempdir }}/fluent-mux.conf"
      secure-forward.conf: "{{ tempdir }}/secure-forward-mux.conf"

# create Mux secret
- include_tasks: generate_secret.yaml

# services
- name: Set logging-mux service for external communication
  oc_service:
    state: present
    name: "logging-mux"
    namespace: "{{ openshift_logging_mux_namespace }}"
    selector:
      component: mux
      provider: openshift
    labels:
      logging-infra: 'support'
    ports:
      - name: mux-forward
        port: "{{ openshift_logging_mux_port }}"
        targetPort: "mux-forward"
    external_ips:
      - "{{ openshift_logging_mux_external_address }}"
  when: openshift_logging_mux_allow_external | bool

- name: Set logging-mux service for internal communication
  oc_service:
    state: present
    name: "logging-mux"
    namespace: "{{ openshift_logging_mux_namespace }}"
    selector:
      component: mux
      provider: openshift
    labels:
      logging-infra: 'support'
    ports:
      - name: mux-forward
        port: "{{ openshift_logging_mux_port }}"
        targetPort: "mux-forward"
  when: not openshift_logging_mux_allow_external | bool

# create Mux DC
- name: Generating mux deploymentconfig
  template:
    src: "mux.j2"
    dest: "{{mktemp.stdout}}/templates/logging-mux-dc.yaml"
  vars:
    component: mux
    logging_component: mux
    deploy_name: "logging-{{ component }}"
    image: "{{ openshift_logging_mux_image }}"
    es_host: "{{ openshift_logging_mux_app_host }}"
    es_port: "{{ openshift_logging_mux_app_port }}"
    ops_host: "{{ openshift_logging_mux_ops_host }}"
    ops_port: "{{ openshift_logging_mux_ops_port }}"
    mux_cpu_limit: "{{ openshift_logging_mux_cpu_limit }}"
    mux_cpu_request: "{{ openshift_logging_mux_cpu_request | min_cpu(openshift_logging_mux_cpu_limit | default(none)) }}"
    mux_memory_limit: "{{ openshift_logging_mux_memory_limit }}"
    mux_replicas: "{{ openshift_logging_mux_replicas | default(1) }}"
    mux_node_selector: "{{ openshift_logging_mux_nodeselector | default({}) }}"
  check_mode: no
  changed_when: no

- name: Create Mux PVC
  oc_pvc:
    state: present
    name: "{{ openshift_logging_mux_file_buffer_pvc_name }}"
    namespace: "{{ openshift_logging_mux_namespace }}"
    volume_capacity: "{{ openshift_logging_mux_file_buffer_pvc_size }}"
    access_modes: "{{ openshift_logging_mux_file_buffer_pvc_access_modes | list }}"
    selector: "{{ openshift_logging_mux_file_buffer_pvc_pv_selector }}"
    storage_class_name: "{{ openshift_logging_mux_file_buffer_pvc_storage_class_name | default('', true) }}"
  when:
    - openshift_logging_mux_file_buffer_storage_type == "pvc"

- name: Set logging-mux DC
  oc_obj:
    state: present
    name: logging-mux
    namespace: "{{ openshift_logging_mux_namespace }}"
    kind: dc
    files:
      - "{{ tempdir }}/templates/logging-mux-dc.yaml"
    delete_after: true

- name: Add mux namespaces
  oc_project:
    state: present
    name: "{{ item }}"
    node_selector: ""
  with_items: "{{ openshift_logging_mux_namespaces }}"

- name: Delete temp directory
  file:
    name: "{{ tempdir }}"
    state: absent
  changed_when: False
