#  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: Disable SWAP (1/2)
  command: /usr/sbin/swapoff -a
  changed_when: true
  tags: init

- name: Disable SWAP in fstab (2/2)
  replace:
    path: /etc/fstab
    regexp: '^([^#].*?\sswap\s+.*)$'
    replace: '# \1'

- name: Get K8s nodes status
  command: kubectl get nodes
  changed_when: false
  failed_when: false
  register: k8s_nodes

- name: Get K8s pods status
  command: kubectl get pods --all-namespaces
  changed_when: false
  failed_when: false
  register: k8s_pods

- name: Docker login
  command: docker login -u {{ docker_username }} -p {{ docker_password }}
  changed_when: true
  register: docker_login_output
  failed_when: false
  when: docker_username or docker_password
  no_log: true

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

- name: Initialize kubeadm (This process may take 5-10min)
  block:
    - name: Initialize kubeadm (This process may take 5-10min)
      command: "kubeadm init --pod-network-cidr='{{ appliance_k8s_pod_net_cidr }}' \
        --apiserver-advertise-address='{{ ansible_default_ipv4.address }}'"
      changed_when: true
      register: init_output
  rescue:
    - name: Reset kubeadm
      command: "kubeadm reset -f"
      changed_when: true

    - name: Initialize kubeadm (This process may take 5-10min)
      command: "kubeadm init --pod-network-cidr='{{ appliance_k8s_pod_net_cidr }}' \
          --apiserver-advertise-address='{{ ansible_default_ipv4.address }}'"
      changed_when: true
      register: init_output

    - name: Get K8s pods status
      command: kubectl get pods --all-namespaces
      changed_when: false
      failed_when: false
      register: k8s_pods
  when: "'master' not in k8s_nodes.stdout"

- name: Setup directory for Kubernetes environment for root
  file:
    path: "{{ k8s_root_directory }}"
    state: directory
    mode: "{{ k8s_root_directory_mode }}"

- name: Copy Kubernetes config for root
  copy:
    src: "{{ k8s_config_src }}"
    dest: "{{ k8s_config_dest }}"
    owner: root
    group: root
    mode: "{{ k8s_config_file_mode }}"
    remote_src: yes

- name: Update the kubernetes config file permissions
  shell: "chown $(id -u):$(id -g) '{{ k8s_config_dest }}'"
  args:
    warn: false
  changed_when: true

- name: Cluster token
  shell: >
    set -o pipefail && \
      kubeadm token list | cut -d ' ' -f1 | sed -n '2p'
  changed_when: false
  register: k8s_token

- name: CA Hash
  shell: >
    set -o pipefail && \
      openssl x509 -pubkey -in {{ k8s_cert_path }} | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
  changed_when: false
  register: k8s_manager_ca_hash

- name: Add K8S Manager IP, Token, and Hash to dummy host
  add_host:
    name:   "K8S_TOKEN_HOLDER"
    token:  "{{ k8s_token.stdout }}"
    hash:   "{{ k8s_manager_ca_hash.stdout }}"
    ip:     "{{ ansible_default_ipv4.address }}"

- name: Create yaml repo for setup
  file:
    path: "{{ yaml_repo_dir_path }}"
    state: directory
    mode: "{{ yaml_repo_dir_mode }}"

- name: Setup Calico SDN network - tigera-operator
  command: "kubectl create -f {{ tigera_operator_url }}"
  changed_when: true
  when: "'tigera-operator' not in k8s_pods.stdout"

- name: Setup Calico SDN network - custom-resources
  command: "kubectl create -f {{ calico_yml_url }}"
  changed_when: true
  failed_when: false
  when: "'calico-system' not in k8s_pods.stdout"

- name: Edge / Workstation Install allows pods to schedule on manager
  command: kubectl taint nodes --all node-role.kubernetes.io/master-
  changed_when: true
  failed_when: false