---
- name: 1. helmchart install (imxc-ui-all)
  kubernetes.core.helm:
    name: "{{item}}"
    release_name: "{{item}}"
    release_namespace: "{{ cmoa_namespace }}"
    chart_ref: "{{ role_path }}/files/06-imxc-ui/{{ item }}"
    create_namespace: yes
    release_state: present
    values_files:
    - "{{ role_path }}/files/06-imxc-ui/{{ item }}/values.yaml"
  with_items:
    - imxc-ui-jaeger
    - imxc-ui-jspd
  when: imxc_ui == 'all'

- name: 1. helmchart install (imxc-ui-jaeger)
  kubernetes.core.helm:
    name: "{{item}}"
    release_name: "{{item}}"
    release_namespace: "{{ cmoa_namespace }}"
    chart_ref: "{{ role_path }}/files/06-imxc-ui/{{ item }}"
    create_namespace: yes
    release_state: present
    values_files:
    - "{{ role_path }}/files/06-imxc-ui/{{ item }}/values.yaml"
  with_items:
    - imxc-ui-jaeger
  when: imxc_ui == 'jaeger'

- name: 2. Change a imxc-ui Service (imxc-ui-jaeger)
  kubernetes.core.k8s:
    state: present
    definition:
      apiVersion: v1
      kind: Service
      metadata:
        name: "{{ jaeger_servicename }}"
        namespace: "{{ cmoa_namespace }}"
      spec:
        type: NodePort
        ports:
        - protocol: TCP
          port: "{{ jaeger_service_port }}"
          nodePort: "{{ jaeger_nodePort }}"
    apply: yes
  when: imxc_ui == 'jaeger'

- name: 2. Get a list of all pods from the namespace
  command: kubectl -n "{{ cmoa_namespace }}" get pods --no-headers -o custom-columns=":metadata.name" # Output is a column
  register: pod_list
  when: imxc_ui != 'all'

- name: 3. Copy psql file in psql (imxc-jaeger)
  kubernetes.core.k8s_cp:
    namespace: "{{ cmoa_namespace }}"
    pod: "{{ item }}"
    remote_path: /tmp/jaeger_menumeta.psql
    local_path: "{{ role_path }}/files/03-ddl-dml/postgres/jaeger_menumeta.psql"
  with_items: "{{ pod_list.stdout_lines }}"
  when: 
    - item is match('postgres')
    - imxc_ui == 'jaeger'
  ignore_errors: true

- name: 4. Execute a command in psql (imxc-jaeger)
  kubernetes.core.k8s_exec:
    namespace: "{{ cmoa_namespace }}"
    pod: "{{ item }}"
    command: bash -c "PGPASSWORD='eorbahrhkswp' && /usr/bin/psql -h 'localhost' -U 'admin' -d 'postgresdb' -f /tmp/jaeger_menumeta.psql"
  with_items: "{{ pod_list.stdout_lines }}"
  when: 
    - item is match('postgres')
    - imxc_ui == 'jaeger'
  ignore_errors: true

- name: 1. helmchart install (imxc-ui-jspd)
  kubernetes.core.helm:
    name: "{{item}}"
    release_name: "{{item}}"
    release_namespace: "{{ cmoa_namespace }}"
    chart_ref: "{{ role_path }}/files/06-imxc-ui/{{ item }}"
    create_namespace: yes
    release_state: present
    values_files:
    - "{{ role_path }}/files/06-imxc-ui/{{ item }}/values.yaml"
  with_items:
    - imxc-ui-jspd
  when: imxc_ui == 'jspd'
  ignore_errors: true

- name: 3. Copy psql file in postgres (imxc-ui-jspd) 
  kubernetes.core.k8s_cp:
    namespace: "{{ cmoa_namespace }}"
    pod: "{{ item }}"
    remote_path: /tmp/jspd_menumeta.psql
    local_path: "{{ role_path }}/files/03-ddl-dml/postgres/jspd_menumeta.psql"
  with_items: "{{ pod_list.stdout_lines }}"
  when: item is match('postgres') and imxc_ui == 'jspd'
  ignore_errors: true

- name: 4. Execute a command in postgres (imxc-ui-jspd)
  kubernetes.core.k8s_exec:
    namespace: "{{ cmoa_namespace }}"
    pod: "{{ item }}"
    command: bash -c "PGPASSWORD='eorbahrhkswp' && /usr/bin/psql -h 'localhost' -U 'admin' -d 'postgresdb' -f /tmp/jspd_menumeta.psql"
  with_items: "{{ pod_list.stdout_lines }}"
  when: item is match('postgres') and imxc_ui == 'jspd'
  ignore_errors: true

- name: 2. Check Kubernetes Pods (imxc ui)
  command: "{{ role_path }}/files/k8s_status {{ cmoa_namespace }}"
