#  Copyright 2020 Dell Inc. or its subsidiaries. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
---

# OMNIA_UKP_US_VFKP_TC_009, OMNIA_UKP_US_VFKP_TC_010
# Execute k8s_start_manager role in manager nodes with os installed centos 7.9 and swap enabled
- name: OMNIA_UKP_US_VFKP_TC_009, OMNIA_UKP_US_VFKP_TC_010
  hosts: manager
  gather_facts: false
  vars_files:
    - test_vars/test_k8s_start_manager_workers_vars.yml
  tasks:
    - name: Enable Swap
      command: /usr/sbin/swapon -a
      changed_when: true
      tags: TC_009, TC_010

    - block:
        - name: Call k8s_start_manager role
          include_role:
            name: ../roles/k8s_start_manager
      tags: TC_009, TC_010

    - name: Waiting for the pods deployment
      pause:
        minutes: 10
      tags: TC_009, TC_010

    - name: Checking master node
      command: kubectl get nodes
      register: master_node_status
      changed_when: false
      ignore_errors: True
      tags: TC_009, TC_010, VERIFY_009

    - name: Checking kube-system pods
      command: kubectl get pods --namespace kube-system --field-selector=status.phase=Running
      register: kube_system_pods
      changed_when: false
      ignore_errors: True
      tags: TC_009, TC_010, VERIFY_009

    - name: Checking calico/flannel SDN network status
      command: ip address
      register: calico_flannel_status
      changed_when: false
      tags: TC_009, TC_010, VERIFY_009

    - name: Checking K8s service account and token
      command: kubectl get secrets
      register: service_account_status
      changed_when: false
      ignore_errors: True
      tags: TC_009, TC_010, VERIFY_009

    - name: Validating master node status
      assert:
        that:
          - "'master' in master_node_status.stdout"
        fail_msg: "{{ master_node_status_fail_msg }}"
        success_msg: "{{ master_node_status_success_msg }}"
      tags: TC_009, TC_010, VERIFY_009

    - name: Validating controller-manager and scheduler and coreDNS pods status
      assert:
        that:
          - "'kube-scheduler' in kube_system_pods.stdout"
          - "'kube-controller' in kube_system_pods.stdout"
        fail_msg: "{{ controller_scheduler_status_fail_msg }}"
        success_msg: "{{ controller_scheduler_status_success_msg }}"
      tags: TC_009, TC_010, VERIFY_009

    - name: Validating coreDNS pods status
      assert:
        that:
          - "'coredns' in kube_system_pods.stdout"
        fail_msg: "{{ coredns_status_fail_msg }}"
        success_msg: "{{ coredns_status_success_msg }}"
      tags: TC_009, TC_010, VERIFY_009

    - name: Validating calico/flannel SDN network status
      assert:
        that:
          - "'calico' in kube_system_pods.stdout or 'flannel' in kube_system_pods.stdout"
        fail_msg: "{{ calico_flannel_status_fail_msg }}"
        success_msg: "{{ calico_flannel_status_success_msg }}"
      tags: TC_009, TC_010, VERIFY_009

    - name: Validating K8s service account and token status
      assert:
        that:
          - "'kubernetes.io/service-account-token' in service_account_status.stdout"
        fail_msg: "{{ k8s_service_account_status_fail_msg }}"
        success_msg: "{{ k8s_service_account_status_success_msg }}"
      tags: TC_009, TC_010, VERIFY_009

# OMNIA_UKP_US_VFKP_TC_011, OMNIA_UKP_US_VFKP_TC_012
# Execute k8s_start_workers role in compute nodes with os installed centos 7.9 and swap enabled
- name: OMNIA_UKP_US_VFKP_TC_011, OMNIA_UKP_US_VFKP_TC_012
  hosts: compute
  gather_facts: false
  tasks:
    - name: Enable Swap
      command: /usr/sbin/swapon -a
      changed_when: true
      tags: TC_011, TC_012

    - block:
        - name: Call k8s_start_workers role
          include_role:
            name: ../roles/k8s_start_workers.yml
      tags: TC_011, TC_012

- name: OMNIA_UKP_US_VFKP_TC_011, OMNIA_UKP_US_VFKP_TC_012
  hosts: manager
  gather_facts: false
  vars_files:
    - test_vars/test_k8s_start_manager_workers_vars.yml
  tasks:
    - name: Check worker nodes status
      command: kubectl get node --selector='!node-role.kubernetes.io/master'
      register: worker_nodes_status
      changed_when: false
      ignore_errors: True
      tags: TC_011, TC_012, VERIFY_011

    - name: Validating worker nodes status
      assert:
        that:
          - "'Ready' in worker_nodes_status.stdout"
        fail_msg: "{{ worker_nodes_status_fail_msg }}"
        success_msg: "{{ worker_nodes_status_success_msg }}"
      tags: TC_011, TC_012, VERIFY_011