---
- name: Gather OpenShift Logging Facts
  openshift_logging_facts:
    oc_bin: "{{openshift.common.client_binary}}"
    openshift_logging_namespace: "{{openshift_logging_namespace}}"

- name: Set logging project
  oc_project:
    state: present
    name: "{{ openshift_logging_namespace }}"
    node_selector: "{{ openshift_logging_nodeselector | default(null) }}"

- name: Labelling logging project
  oc_label:
    state: present
    kind: namespace
    name: "{{ openshift_logging_namespace }}"
    labels:
    - key: "{{ item.key }}"
      value: "{{ item.value }}"
  with_dict: "{{ openshift_logging_labels | default({}) }}"
  when:
  - openshift_logging_labels is defined
  - openshift_logging_labels is dict

- name: Labelling logging project
  oc_label:
    state: present
    kind: namespace
    name: "{{ openshift_logging_namespace }}"
    labels:
    - key: "{{ openshift_logging_label_key }}"
      value: "{{ openshift_logging_label_value }}"
  when:
  - openshift_logging_label_key is defined
  - openshift_logging_label_key != ""
  - openshift_logging_label_value is defined

- name: Create logging cert directory
  file:
    path: "{{ openshift.common.config_base }}/logging"
    state: directory
    mode: 0755
  changed_when: False
  check_mode: no

- include: generate_certs.yaml
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"

## Elasticsearch

- set_fact: es_indices={{ es_indices | default([]) + [item | int - 1] }}
  with_sequence: count={{ openshift_logging_facts.elasticsearch.deploymentconfigs.keys() | count }}
  when: openshift_logging_facts.elasticsearch.deploymentconfigs.keys() | count > 0

- set_fact: es_indices=[]
  when: openshift_logging_facts.elasticsearch.deploymentconfigs.keys() | count == 0

# We don't allow scaling down of ES nodes currently
- include_role:
    name: openshift_logging_elasticsearch
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_elasticsearch_deployment_name: "{{ item.0 }}"
    openshift_logging_elasticsearch_pvc_name: "{{ openshift_logging_es_pvc_prefix ~ '-' ~ item.2 if item.1 is none else item.1 }}"
    openshift_logging_elasticsearch_replica_count: "{{ openshift_logging_es_cluster_size | int }}"

    openshift_logging_elasticsearch_storage_type: "{{ 'pvc' if openshift_logging_es_pvc_dynamic | bool else 'emptydir' }}"
    openshift_logging_elasticsearch_pvc_size: "{{ openshift_logging_es_pvc_size }}"
    openshift_logging_elasticsearch_pvc_dynamic: "{{ openshift_logging_es_pvc_dynamic }}"
    openshift_logging_elasticsearch_pvc_pv_selector: "{{ openshift_logging_es_pv_selector }}"

  with_together:
  - "{{ openshift_logging_facts.elasticsearch.deploymentconfigs }}"
  - "{{ openshift_logging_facts.elasticsearch.pvcs }}"
  - "{{ es_indices }}"

# Create any new DC that may be required
- include_role:
    name: openshift_logging_elasticsearch
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_elasticsearch_pvc_name: "{{ openshift_logging_es_pvc_prefix }}-{{ item | int + openshift_logging_facts.elasticsearch.deploymentconfigs | count - 1 }}"
    openshift_logging_elasticsearch_replica_count: "{{ openshift_logging_es_cluster_size | int }}"

    openshift_logging_elasticsearch_storage_type: "{{ 'pvc' if openshift_logging_es_pvc_dynamic | bool else 'emptydir' }}"
    openshift_logging_elasticsearch_pvc_size: "{{ openshift_logging_es_pvc_size }}"
    openshift_logging_elasticsearch_pvc_dynamic: "{{ openshift_logging_es_pvc_dynamic }}"
    openshift_logging_elasticsearch_pvc_pv_selector: "{{ openshift_logging_es_pv_selector }}"

  with_sequence: count={{ openshift_logging_es_cluster_size | int - openshift_logging_facts.elasticsearch.deploymentconfigs.keys() | count }}

- set_fact: es_ops_indices={{ es_ops_indices | default([]) + [item | int - 1] }}
  with_sequence: count={{ openshift_logging_facts.elasticsearch_ops.deploymentconfigs.keys() | count }}
  when:
  - openshift_logging_use_ops | bool
  - openshift_logging_facts.elasticsearch_ops.deploymentconfigs.keys() | count > 0

- set_fact: es_ops_indices=[]
  when: openshift_logging_facts.elasticsearch_ops.deploymentconfigs.keys() | count == 0


- include_role:
    name: openshift_logging_elasticsearch
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_elasticsearch_deployment_name: "{{ item.0 }}"
    openshift_logging_elasticsearch_pvc_name: "{{ openshift_logging_es_pvc_prefix ~ '-' ~ item.2 if item.1 is none else item.1 }}"
    openshift_logging_elasticsearch_ops_deployment: true
    openshift_logging_elasticsearch_replica_count: "{{ openshift_logging_es_ops_cluster_size | int }}"

    #openshift_logging_elasticsearch_storage_type: "{{ }}"
    openshift_logging_elasticsearch_pvc_size: "{{ openshift_logging_es_pvc_size }}"
    openshift_logging_elasticsearch_pvc_dynamic: "{{ openshift_logging_es_pvc_dynamic }}"
    openshift_logging_elasticsearch_pvc_pv_selector: "{{ openshift_logging_es_pv_selector }}"

  with_together:
  - "{{ openshift_logging_facts.elasticsearch_ops.deploymentconfigs }}"
  - "{{ openshift_logging_facts.elasticsearch_ops.pvcs }}"
  - "{{ es_ops_indices }}"
  when:
  - openshift_logging_use_ops | bool

# Create any new DC that may be required
- include_role:
    name: openshift_logging_elasticsearch
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_elasticsearch_pvc_name: "{{ openshift_logging_es_pvc_prefix }}-{{ item | int + openshift_logging_facts.elasticsearch_ops.deploymentconfigs | count - 1 }}"
    openshift_logging_elasticsearch_ops_deployment: true
    openshift_logging_elasticsearch_replica_count: "{{ openshift_logging_es_ops_cluster_size | int }}"

    openshift_logging_elasticsearch_storage_type: "{{ 'pvc' if openshift_logging_es_pvc_dynamic | bool else 'emptydir' }}"
    openshift_logging_elasticsearch_pvc_size: "{{ openshift_logging_es_pvc_size }}"
    openshift_logging_elasticsearch_pvc_dynamic: "{{ openshift_logging_es_pvc_dynamic }}"
    openshift_logging_elasticsearch_pvc_pv_selector: "{{ openshift_logging_es_pv_selector }}"

  with_sequence: count={{ openshift_logging_es_ops_cluster_size | int - openshift_logging_facts.elasticsearch_ops.deploymentconfigs.keys() | count }}
  when:
  - openshift_logging_use_ops | bool


## Kibana
- include_role:
    name: openshift_logging_kibana
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_kibana_namespace: "{{ openshift_logging_namespace }}"
    openshift_logging_kibana_master_url: "{{ openshift_logging_master_url }}"
    openshift_logging_kibana_master_public_url: "{{ openshift_logging_master_public_url }}"
    openshift_logging_kibana_image_prefix: "{{ openshift_logging_image_prefix }}"
    openshift_logging_kibana_image_version: "{{ openshift_logging_image_version }}"
    openshift_logging_kibana_replicas: "{{ openshift_logging_kibana_replica_count }}"
    openshift_logging_kibana_es_host: "{{ openshift_logging_es_host }}"
    openshift_logging_kibana_es_port: "{{ openshift_logging_es_port }}"
    openshift_logging_kibana_image_pull_secret: "{{ openshift_logging_image_pull_secret }}"


- include_role:
    name: openshift_logging_kibana
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_kibana_ops_deployment: true
    openshift_logging_kibana_namespace: "{{ openshift_logging_namespace }}"
    openshift_logging_kibana_master_url: "{{ openshift_logging_master_url }}"
    openshift_logging_kibana_master_public_url: "{{ openshift_logging_master_public_url }}"
    openshift_logging_kibana_image_prefix: "{{ openshift_logging_image_prefix }}"
    openshift_logging_kibana_image_version: "{{ openshift_logging_image_version }}"
    openshift_logging_kibana_image_pull_secret: "{{ openshift_logging_image_pull_secret }}"
    openshift_logging_kibana_es_host: "{{ openshift_logging_es_ops_host }}"
    openshift_logging_kibana_es_port: "{{ openshift_logging_es_ops_port }}"
    openshift_logging_kibana_nodeselector: "{{ openshift_logging_kibana_ops_nodeselector }}"
    openshift_logging_kibana_cpu_limit: "{{ openshift_logging_kibana_ops_cpu_limit }}"
    openshift_logging_kibana_memory_limit: "{{ openshift_logging_kibana_ops_memory_limit }}"
    openshift_logging_kibana_hostname: "{{ openshift_logging_kibana_ops_hostname }}"
    openshift_logging_kibana_replicas: "{{ openshift_logging_kibana_ops_replica_count }}"
    openshift_logging_kibana_proxy_debug: "{{ openshift_logging_kibana_ops_proxy_debug }}"
    openshift_logging_kibana_proxy_cpu_limit: "{{ openshift_logging_kibana_ops_proxy_cpu_limit }}"
    openshift_logging_kibana_proxy_memory_limit: "{{ openshift_logging_kibana_ops_proxy_memory_limit }}"
    openshift_logging_kibana_cert: "{{ openshift_logging_kibana_ops_cert }}"
    openshift_logging_kibana_key: "{{ openshift_logging_kibana_ops_key }}"
    openshift_logging_kibana_ca: "{{ openshift_logging_kibana_ops_ca}}"
  when:
  - openshift_logging_use_ops | bool


## Curator
- include_role:
    name: openshift_logging_curator
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_curator_namespace: "{{ openshift_logging_namespace }}"
    openshift_logging_curator_master_url: "{{ openshift_logging_master_url }}"
    openshift_logging_curator_image_prefix: "{{ openshift_logging_image_prefix }}"
    openshift_logging_curator_image_version: "{{ openshift_logging_image_version }}"
    openshift_logging_curator_image_pull_secret: "{{ openshift_logging_image_pull_secret }}"

- include_role:
    name: openshift_logging_curator
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_curator_ops_deployment: true
    openshift_logging_curator_namespace: "{{ openshift_logging_namespace }}"
    openshift_logging_curator_master_url: "{{ openshift_logging_master_url }}"
    openshift_logging_curator_image_prefix: "{{ openshift_logging_image_prefix }}"
    openshift_logging_curator_image_version: "{{ openshift_logging_image_version }}"
    openshift_logging_curator_image_pull_secret: "{{ openshift_logging_image_pull_secret }}"
    openshift_logging_curator_cpu_limit: "{{ openshift_logging_curator_ops_cpu_limit }}"
    openshift_logging_curator_memory_limit: "{{ openshift_logging_curator_ops_memory_limit }}"
    openshift_logging_curator_nodeselector: "{{ openshift_logging_curator_ops_nodeselector }}"
  when:
  - openshift_logging_use_ops | bool

## Mux
- include_role:
    name: openshift_logging_mux
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_mux_ops_host: "{{ ( openshift_logging_use_ops | bool ) | ternary('logging-es-ops', 'logging-es') }}"
    openshift_logging_mux_namespace: "{{ openshift_logging_namespace }}"
    openshift_logging_mux_master_url: "{{ openshift_logging_master_url }}"
    openshift_logging_mux_image_prefix: "{{ openshift_logging_image_prefix }}"
    openshift_logging_mux_image_version: "{{ openshift_logging_image_version }}"
    openshift_logging_mux_image_pull_secret: "{{ openshift_logging_image_pull_secret }}"
  when:
  - openshift_logging_use_mux | bool


## Fluentd
- include_role:
    name: openshift_logging_fluentd
  vars:
    generated_certs_dir: "{{openshift.common.config_base}}/logging"
    openshift_logging_fluentd_ops_host: "{{ ( openshift_logging_use_ops | bool ) | ternary('logging-es-ops', 'logging-es') }}"
    openshift_logging_fluentd_use_journal: "{{ openshift.docker.options | search('journald') }}"
    openshift_logging_fluentd_image_prefix: "{{ openshift_logging_image_prefix }}"
    openshift_logging_fluentd_image_version: "{{ openshift_logging_image_version }}"
    openshift_logging_fluentd_image_pull_secret: "{{ openshift_logging_image_pull_secret }}"
    openshift_logging_fluentd_master_url: "{{ openshift_logging_master_url }}"
    openshift_logging_fluentd_namespace: "{{ openshift_logging_namespace }}"

- include: update_master_config.yaml
