#  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: Permanently Disable swap
  mount:
    name: "swap"
    fstype: swap
    state: absent

- name: Disable selinux
  selinux:
    state: disabled

- name: Copy k8s.conf file
  copy:
    src: k8s.conf
    dest: "{{ k8s_conf_dest }}"
    owner: root
    group: root
    mode: "{{ conf_file_mode }}"

- name: Copy crio.conf file
  copy:
    src: crio.conf
    dest: "{{ crio_conf_dest }}"
    owner: root
    group: root
    mode: "{{ conf_file_mode }}"

- name: Enable the kernel modules overlay and br_netfilter
  modprobe:
    name: "{{ item }}"
    state: present
  with_items:
    - overlay
    - br_netfilter

- name: Update sysctl to handle incorrectly routed traffic when iptables is bypassed
  copy:
    src: k8s-crio.conf
    dest: "{{ k8s_crio_conf_dest }}"
    owner: root
    group: root
    mode: "{{ conf_file_mode }}"

- name: Update sysctl
  command: /sbin/sysctl --system
  changed_when: true

- name: Add CRI-O repo (1/2)
  get_url:
    url: "{{ crio_repo1_url }}"
    dest: "{{ crio_repo1_dest }}"
  register: crio_repo1_result
  until: crio_repo1_result is not failed
  retries: 20

- name: Add CRI-O repo (2/2)
  get_url:
    url: "{{ crio_repo2_url }}"
    dest: "{{ crio_repo2_dest }}"
  register: crio_repo2_result
  until: crio_repo2_result is not failed
  retries: 20

- name: Add kubernetes repo
  yum_repository:
    name: kubernetes
    description: kubernetes
    baseurl: https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
    enabled: yes
    gpgcheck: no
    repo_gpgcheck: no
    gpgkey:
      - https://packages.cloud.google.com/yum/doc/yum-key.gpg
      - https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg

- name: Install common packages
  package:
    name: "{{ common_packages }}"
    state: present

- name: Install k8s packages
  package:
    name: "{{ k8s_packages }}"
    state: present

- name: Versionlock kubernetes
  command: "yum versionlock '{{ item }}'"
  args:
    warn: false
  with_items:
    - "{{ k8s_packages }}"
  changed_when: true

- name: Add docker community edition repository for docker-ce-cli
  get_url:
    url: "{{ docker_repo_url }}"
    dest: "{{ docker_repo_dest }}"
  when: docker_username and docker_password
  register: docker_repo
  until: docker_repo is not failed
  retries: 20
  delay: 10
  tags: install

- name: Install docker-ce-cli
  package:
    name: docker-ce-cli
    state: present
  when: docker_username and docker_password

- name: Start and enable crio
  service:
    name: crio
    state: restarted
    daemon_reload: yes
    enabled: yes

- name: Start and enable kubernetes - kubelet
  service:
    name: kubelet
    state: restarted
    enabled: yes

- name: Wait for 30sec for kubelet to get things ready
  pause:
    seconds: 30