---
- name: Kubelet | Delete kubeconfig
  win_file:
    path: "{{ install_path }}/kubeconfig.yaml"
    state: absent

- name: Kubelet | Create kubeconfig
  win_lineinfile:
    path: "{{ install_path }}/kubeconfig.yaml"
    create: yes
    line: |
      apiVersion: v1
      kind: Config
      clusters:
      - name: local
        cluster:
          server: http://{{ kubernetes_info.MASTER_IP }}:8080
      users:
      - name: kubelet
      contexts:
      - context:
          cluster: local
          user: kubelet
        name: kubelet-context
      current-context: kubelet-context
    newline: unix

- name: Kubernetes | check if Kubelet is installed
  win_service:
    name: kubelet
  register: kubelet_service

- name: Kubelet | Remove kubelet service if it exists
  win_service:
    name: kubelet
    state: absent
  when: kubelet_service.exists

- name: Kubelet | Create the service wrapper config file
  block:
    - name: Kubelet | Remove existing service wrapper config file
      win_file:
        state: absent
        path: '{{ install_path }}\kubelet-servicewrapper-config.ini'

    - name: Kubelet | Create the new service wrapper config file
      win_lineinfile:
        path: '{{ install_path }}\kubelet-servicewrapper-config.ini'
        create: yes
        line: >
          log-file={{ install_path }}/kubelet.log

          service-name=kubelet

          service-command="{{ install_path }}\\kubelet.exe"
          --hostname-override="{{ ansible_hostname }}"
          --cluster-dns="{{ kubernetes_info.K8S_DNS_SERVICE_IP }}"
          --cluster-domain="{{ kubernetes_info.K8S_DNS_DOMAIN }}"
          --pod-infra-container-image="{{kubernetes_info.infracontainername}}"
          --kubeconfig="{{ install_path }}\\kubeconfig.yaml"
          --network-plugin=cni --cni-bin-dir="{{ install_path }}\\cni"
          --cni-conf-dir="{{ install_path }}\\cni"
          --node-ip="{{ host_public_ip }}"
          --enforce-node-allocatable ""
          --cgroups-per-qos=false
          --resolv-conf ""

- name: Kubelet | Create Kublet Windows service
  win_service:
    name: kubelet
    display_name: Kubernetes Kubelet
    description: Kubernetes Kubelet service
    path: >-
      "{{ install_path }}\servicewrapper.exe" --config "{{ install_path }}\kubelet-servicewrapper-config.ini"

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

- name: Kublet | Enable Kublet service failure flags
  win_shell: sc.exe failureflag kubelet 1

- name: Kubelet | Set kubectl context
  win_shell: |
    $ErrorActionPreference = "Stop"
    {{ install_path }}\\kubectl.exe config set-cluster default-cluster --server={{ kubernetes_info.MASTER_IP }}:8080
    if($LASTEXITCODE) {
        Throw "Failed to run kubectl.exe config set-cluster"
    }
    {{ install_path }}\\kubectl.exe config set-context local --cluster=default-cluster --user=default-admin
    if($LASTEXITCODE) {
        Throw "Failed to run kubectl.exe config set-context"
    }
    {{ install_path }}\\kubectl.exe config use-context local
    if($LASTEXITCODE) {
        Throw "Failed to run kubectl.exe config use-context"
    }

# Start the kubelet to ensure OVN gives subnet to this minion
- name: Kubelet | Start service kubelet
  win_service:
    name: kubelet
    start_mode: auto
    state: started
