#  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 CoreDNS to restart
  command: kubectl rollout status deployment/coredns -n kube-system
  changed_when: false
  failed_when: false

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

- name: Deploy MetalLB
  command: "kubectl apply -f '{{ metallb_yaml_url }}'"
  changed_when: true
  when: "'metallb' not in k8s_pods.stdout"

- name: Create MetalLB Setup Config Files
  copy:
    src: metal-config.yaml
    dest: "{{ metallb_config_file_dest }}"
    owner: root
    group: root
    mode: "{{ metallb_config_file_mode }}"

- name: Replace metallb_addresses
  replace:
    path: "{{ metallb_config_file_dest }}"
    regexp: 'addresses:'
    replace: "{{ metallb_addresses }}"

- name: Remove ^M characters from metal-config file
  shell: 'sed -e "s/\r//g" {{ metallb_config_file_dest }} > {{ metallb_config_updated_file_dest }}'
  args:
    warn: false
  changed_when: true

- name: Update metal-config file permissions
  file:
    path: "{{ metallb_config_updated_file_dest }}"
    owner: root
    group: root
    mode: "{{ metallb_config_file_mode }}"

- name: Remove old metallb-config file
  file:
    path: "{{ metallb_config_file_dest }}"
    state: absent

- name: Create MetalLB Setup Deployment Files
  copy:
    src: metallb.yaml
    dest: "{{ metallb_deployment_file_dest }}"
    owner: root
    group: root
    mode: "{{ metallb_deployment_file_mode }}"

- name: Replace metallb_container_port
  replace:
    path: "{{ metallb_deployment_file_dest }}"
    regexp: 'metallb_container_port'
    replace: "{{ metallb_container_port }}"

- name: Replace metallb_container_port
  replace:
    path: "{{ metallb_deployment_file_dest }}"
    regexp: 'metallb_run_as_user_port'
    replace: "{{ metallb_run_as_user_port }}"

- name: Deploy MetalLB
  command: "kubectl apply -f '{{ metallb_deployment_file_dest }}'"
  changed_when: true
  when: "'metallb' not in k8s_pods.stdout"

- name: Create default setup for MetalLB
  command: "kubectl apply -f '{{ metallb_config_updated_file_dest }}'"
  changed_when: true
  when: "'metallb' not in k8s_pods.stdout"

- name: Deploy K8s dashboard
  command: "kubectl apply -f {{ k8s_dashboard_yaml_url }}"
  changed_when: true
  when: "'kubernetes-dashboard' not in k8s_pods.stdout"

- name: Copy k8s_dashboard_admin.yml file
  copy:
    src: k8s_dashboard_admin.yaml
    dest: "{{ k8s_dashboard_admin_file_dest }}"
    owner: root
    group: root
    mode: "{{ k8s_dashboard_admin_file_mode }}"

- name: Create admin user for K8s dashboard
  command: "kubectl apply -f {{ k8s_dashboard_admin_file_dest }}"
  changed_when: true

- name: Start NFS Client Provisioner
  command: "helm install stable/nfs-client-provisioner --set nfs.server='{{ ansible_default_ipv4.address }}' --set nfs.path='{{ nfs_path }}' --generate-name"
  changed_when: true
  when: "'nfs-client-provisioner' not in k8s_pods.stdout"

- name: Set NFS-Client Provisioner as DEFAULT StorageClass
  shell: >
    kubectl patch storageclasses.storage.k8s.io nfs-client \
    -p '{ "metadata": { "annotations":{ "storageclass.kubernetes.io/is-default-class":"true" }}}'
  changed_when: true

- name: Get K8s namespaces
  command: kubectl get namespaces
  changed_when: false
  register: k8s_namespaces

- name: Create namespace network-config
  command: kubectl create namespace network-config
  changed_when: true
  when: "'network-config' not in k8s_namespaces.stdout"