---
- name: Linux validation | include vars
  include_vars: "{{ ansible_distribution | lower }}.yml"

- name: Linux validation | Set the service_names fact
  block:
    - name: Linux validation | Set the Linux master service names
      set_fact:
        service_names: "{{ master_service_names }}"
      when: master

    - name: Linux validation | Set the Linux minion service names
      set_fact:
        service_names: "{{ minion_service_names }}"
      when: minion

- name: Linux validation | Validate the services success state
  include: "./validate_service.yml service_name={{ item }}"
  with_items: "{{ service_names }}"

- name: Linux validation | Validate if the pods from namespace kube-system (CoreDNS) are running
  run_once: true
  retries: 10
  delay: 5
  register: result
  shell: |
    set -o errexit
    for UNHEALTHY_POD in $(kubectl get pods --namespace kube-system --output jsonpath='{@.items[?(@.status.phase!="Running")].metadata.name}'); do
        echo "ERROR: Pod $UNHEALTHY_POD from namespace kube-system is not running"
        exit 1
    done
    echo "All the pods from namespace kube-system are running"
  args:
    executable: /bin/bash
  until: result.rc == 0
  when: master

- name: Linux validation | Confirm K8s minions health
  retries: 10
  delay: 5
  register: result
  shell: |
    set -o errexit
    READY_STATUS=$(kubectl get node {{ hostvars[item]['ansible_facts']['hostname'] | lower }} --output jsonpath='{@.status.conditions[?(@.type=="Ready")].status}')
    if [[ "$READY_STATUS" != "True" ]]; then
        echo "ERROR: Node {{ hostvars[item]['ansible_facts']['hostname'] | lower }} is not ready"
        exit 1
    fi
    echo "Node {{ hostvars[item]['ansible_facts']['hostname'] | lower }} is ready"
  args:
    executable: /bin/bash
  until: result.rc == 0
  with_items:
    - "{{ groups['kube-minions-linux'] }}"
    - "{{ groups['kube-minions-windows'] }}"
  when: master
