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

- name: Download kfctl release from the Kubeflow releases page
  unarchive:
    src: "{{ kfctl_download_url }}"
    dest: "{{ kfctl_download_dest_path }}"
    mode: "{{ kfctl_download_file_mode }}"
    remote_src: yes

- name: Delete omnia kubeflow directory if exists
  file:
    path: "{{ omnia_kubeflow_dir_path }}"
    state: absent

- name: Create omnia kubeflow directory
  file:
    path: "{{ omnia_kubeflow_dir_path }}"
    state: directory
    mode: "{{ omnia_kubeflow_dir_mode }}"
    recurse: yes

- name: Build kubeflow configuration
  command:
    cmd: /usr/bin/kfctl build -V -f "{{ kubeflow_config_yaml_url }}"
    chdir: "{{ omnia_kubeflow_dir_path }}"
  changed_when: true

- name: Modify CPU limit for istio-ingressgateway-service-account
  replace:
    path: "{{ istio_noauth_yaml_file_path }}"
    after: 'serviceAccountName: istio-ingressgateway-service-account'
    before: '---'
    regexp: 'cpu: 100m'
    replace: 'cpu: 2'

- name: Modify memory limit for istio-ingressgateway-service-account
  replace:
    path: "{{ istio_noauth_yaml_file_path }}"
    after: 'serviceAccountName: istio-ingressgateway-service-account'
    before: '---'
    regexp: 'memory: 128Mi'
    replace: 'memory: 512Mi'

- name: Modify CPU request for istio-ingressgateway-service-account
  replace:
    path: "{{ istio_noauth_yaml_file_path }}"
    after: 'serviceAccountName: istio-ingressgateway-service-account'
    before: '---'
    regexp: 'cpu: 10m'
    replace: 'cpu: 1'

- name: Modify memory request for istio-ingressgateway-service-account
  replace:
    path: "{{ istio_noauth_yaml_file_path }}"
    after: 'serviceAccountName: istio-ingressgateway-service-account'
    before: '---'
    regexp: 'memory: 40Mi'
    replace: 'memory: 256Mi'

- name: Modify memory request for istio-engressgateway-service-account
  replace:
    path: "{{ istio_noauth_yaml_file_path }}"
    after: 'serviceAccountName: istio-egressgateway-service-account'
    before: '---'
    regexp: 'memory: 128Mi'
    replace: 'memory: 256Mi'

- name: Modify memory request for istio-engressgateway-service-account
  replace:
    path: "{{ istio_noauth_yaml_file_path }}"
    after: 'serviceAccountName: istio-egressgateway-service-account'
    before: '---'
    regexp: 'memory: 40Mi'
    replace: 'memory: 128Mi'

- name: Modify CPU limit for kfserving-gateway
  replace:
    path: "{{ kfserving_gateway_yaml_file_path }}"
    after: 'serviceAccountName: istio-ingressgateway-service-account'
    before: 'env:'
    regexp: 'cpu: 100m'
    replace: 'cpu: 2'

- name: Modify memory limit for kfserving-gateway
  replace:
    path: "{{ kfserving_gateway_yaml_file_path }}"
    after: 'serviceAccountName: istio-ingressgateway-service-account'
    before: 'env:'
    regexp: 'memory: 128Mi'
    replace: 'memory: 512Mi'

- name: Modify CPU request for kfserving-gateway
  replace:
    path: "{{ kfserving_gateway_yaml_file_path }}"
    after: 'serviceAccountName: istio-ingressgateway-service-account'
    before: 'env:'
    regexp: 'cpu: 10m'
    replace: 'cpu: 1'

- name: Modify memory request for kfserving-gateway
  replace:
    path: "{{ kfserving_gateway_yaml_file_path }}"
    after: 'serviceAccountName: istio-ingressgateway-service-account'
    before: 'env:'
    regexp: 'memory: 40Mi'
    replace: 'memory: 256Mi'

- name: Change argo base service from NodePort to LoadBalancer
  replace:
    path: "{{ argo_yaml_file_path }}"
    regexp: 'NodePort'
    replace: 'LoadBalancer'

- name: Change istio-install base istio-noauth service from NodePort to LoadBalancer
  replace:
    path: "{{ istio_noauth_yaml_file_path }}"
    regexp: 'NodePort'
    replace: 'LoadBalancer'

- name: Apply kubeflow configuration
  command:
    cmd: "/usr/bin/kfctl apply -V -f '{{ kubeflow_config_file }}'"
    chdir: "{{ omnia_kubeflow_dir_path }}"
  changed_when: true
  register: apply_kubeflow_config
  until: apply_kubeflow_config is not failed
  retries: 20
  delay: 10