#  Copyright 2021 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.
---

- name: Wait for calico pod to come to ready state
  command: "kubectl wait --for=condition=ready -n kube-system pod -l k8s-app=calico-node --timeout=10m"
  changed_when: false
  register: calico_pod_status
  failed_when: false
  when:
    - "'manager' in group_names"
    - hostvars['127.0.0.1']['k8s_cni'] == "calico"
  tags: install

- name: Wait for flannel pod to come to ready state
  command: "kubectl wait --for=condition=ready -n kube-system pod -l app=flannel --timeout=10m"
  changed_when: false
  register: flannel_pod_status
  failed_when: false
  when:
    - "'manager' in group_names"
    - hostvars['127.0.0.1']['k8s_cni'] == "flannel"
  tags: install

- name: Wait for nfs-client-provisioner pod to come to ready state
  command: "kubectl wait --for=condition=ready -n default pod -l app=nfs-client-provisioner --timeout=10m"
  changed_when: false
  register: nfs_pod_status
  failed_when: false
  when:
    - "'manager' in group_names"
    - calico_pod_status is not failed or flannel_pod_status is not failed
  tags: install

- name: Wait for volcano-scheduler pod to come to ready state
  command: "kubectl wait --for=condition=ready -n volcano-system pod -l app=volcano-scheduler --timeout=5m"
  changed_when: false
  register: volcano_pod_status
  failed_when: false
  when:
    - "'manager' in group_names"
    - nfs_pod_status is not failed
  tags: install

- name: Get K8s pods
  command: kubectl get pods --all-namespaces
  changed_when: false
  register: k8s_pods
  when: "'manager' in group_names"
  tags: install

- name: Add k8s_pods_status to dummy host
  add_host:
    name: "check_k8s_pods"
    k8s_pods_status: "{{ k8s_pods.stdout }}"
  tags: install

- name: Fail message
  fail:
    msg: "{{ docker_pull_limit_msg }}"
  when:
    - "'ImagePullBackOff' in hostvars['check_k8s_pods']['k8s_pods_status'] or 'ErrImagePull' in hostvars['check_k8s_pods']['k8s_pods_status']"
    - not hostvars['127.0.0.1']['docker_username'] and not hostvars['127.0.0.1']['docker_password']

- name: Docker login
  command: docker login -u {{ hostvars['127.0.0.1']['docker_username'] }} -p {{ hostvars['127.0.0.1']['docker_password'] }}
  changed_when: true
  register: docker_login_output
  failed_when: false
  when:
    - "'ImagePullBackOff' in hostvars['check_k8s_pods']['k8s_pods_status'] or 'ErrImagePull' in hostvars['check_k8s_pods']['k8s_pods_status']"
    - hostvars['127.0.0.1']['docker_username'] or hostvars['127.0.0.1']['docker_password']
    - "'compute' in group_names"
  no_log: true

- name: Docker login check
  fail:
    msg: "{{ docker_login_fail_msg }}"
  when:
    - docker_login_output is failed
    - "'compute' in group_names"

- name: Pull K8s services docker images
  command: docker pull {{ item }}
  with_items: "{{ k8s_docker_images }}"
  when:
    - "'ImagePullBackOff' in hostvars['check_k8s_pods']['k8s_pods_status'] or 'ErrImagePull' in hostvars['check_k8s_pods']['k8s_pods_status']"
    - hostvars['127.0.0.1']['docker_username'] and hostvars['127.0.0.1']['docker_password']
  register: docker_image_pull_result
  until: docker_image_pull_result is not failed
  retries: 5