---
- name: Kubernetes minion | Update etc\hosts file
  win_lineinfile:
    path: '{{ ansible_env.windir }}\System32\drivers\etc\hosts'
    line: '{{ host_public_ip }} {{ ansible_hostname | lower }}'
    state: present

- name: Kubernetes minion | Add "{{ install_path }}" to path
  win_path:
    elements: "{{ install_path }}"

- name: Kubernetes minion | Check if ovn-kubernetes-node is installed
  win_service:
    name: ovn-kubernetes-node
  register: ovnkube_service

- name: Kubernetes minion | Remove ovn-kubernetes-node service if it exists
  win_service:
    name: ovn-kubernetes-node
    state: absent
  when: ovnkube_service.exists

- name: Kubernetes minion | Set ovnkube service command
  set_fact:
    ovnkube_service_command: >-
      "{{ install_path }}\\ovnkube.exe"
      --k8s-kubeconfig={{ install_path }}\\kubeconfig.yaml
      --k8s-apiserver http://{{ kubernetes_info.MASTER_IP }}:8080
      --init-node {{ ansible_hostname|lower }}
      --k8s-token {{ TOKEN }}
      --nb-address "tcp://{{ kubernetes_info.MASTER_IP }}:6641"
      --sb-address "tcp://{{ kubernetes_info.MASTER_IP }}:6642"
      --cluster-subnets {{ kubernetes_info.CLUSTER_SUBNET }}
      --k8s-service-cidr {{ kubernetes_info.SERVICE_CLUSTER_IP_RANGE }}
      --cni-conf-dir="{{ install_path }}/cni"
      --cni-plugin "ovn-k8s-cni-overlay.exe"
      --encap-ip="{{ host_internal_ip }}"

- name: Kubernetes minion | Remove existing service wrapper config file
  win_file:
    state: absent
    path: '{{ install_path }}\ovnkube-servicewrapper-config.ini'

- name: Kubernetes minion | Create the new service wrapper config file
  win_lineinfile:
    path: '{{ install_path }}\ovnkube-servicewrapper-config.ini'
    create: yes
    line: |
      log-file={{ install_path }}/ovn-kubernetes-node.log
      service-name=ovn-kubernetes-node

- name: Kubernetes minion | Set default "init_gateway" fact if not already defined
  set_fact:
    init_gateway: true
  when: init_gateway is not defined

- name: Kubernetes minion | Set the ovnkube service-command with gateway args
  win_lineinfile:
    path: '{{ install_path }}\ovnkube-servicewrapper-config.ini'
    insertafter: EOF
    state: present
    line: >
      service-command={{ ovnkube_service_command }}
      --nodeport
      --gateway-mode=shared
      --gateway-interface="vEthernet ({{ interface_name }})"
      --gateway-nexthop="{{ interface_default_gateway }}"
  when: init_gateway

- name: Kubernetes minion | Set the ovnkube service-command without gateway args
  win_lineinfile:
    path: '{{ install_path }}\ovnkube-servicewrapper-config.ini'
    insertafter: EOF
    state: present
    line: >
      service-command={{ ovnkube_service_command }}
  when: not init_gateway

- name: Kubernetes minion | Create ovn-kubernetes-node service
  win_service:
    name: ovn-kubernetes-node
    display_name: OVN Kubernetes Node
    description: OVN Kubernetes Node CNI Server
    path: >-
      "{{ install_path }}\servicewrapper.exe" --config "{{ install_path }}\ovnkube-servicewrapper-config.ini"

- name: Kubernetes minion | Set Kubernetes minion service failure command
  win_shell: >-
    sc.exe failure ovn-kubernetes-node reset=40 actions=restart/0/restart/0/run/30000
    command="powershell.exe Move-Item
    \\\`"{{ install_path }}/ovn-kubernetes-node.log\\\`"
    \\\`"{{ install_path }}/ovn-kubernetes-node.log_`$(Get-Date -f yyyy-MM-dd-hh-mm-ss)\\\`";
    Restart-Service ovn-kubernetes-node"

- name: Kubernetes minion | Enable Kubernetes minion service failure flags
  win_shell: sc.exe failureflag ovn-kubernetes-node 1

- name: Kubernetes minion | Restart Kubelet
  win_service:
    name: kubelet
    state: restarted

- name: Kubernetes minion | Start ovn-kubernetes-node service
  win_service:
    name: ovn-kubernetes-node
    start_mode: auto
    state: started

- name: Kubernetes minion | Wait for ovn-kubernetes-node service to finish init
  win_shell: ping {{ ovn_gateway_ip }} -n 1
  register: ovngateway_ping
  until: ovngateway_ping.rc == 0
  retries: 30
  delay: 3
