---
- name: Kubernetes | Include global vars for minions
  include_vars: "{{ansible_tmp_dir}}/generated_global_vars.yml"
  when: not master

- name: Kubernetes | Include vars
  include_vars: "{{ ansible_os_family|lower }}.yml"

- name: Kubernetes | Fetch network details
  include_tasks: set_ip_facts.yml

# We need to start kubelet in order to be able to retrieve the ovn_host_subnet
# from kubernetes node annotations
- name: Kubernetes | Register and start kubelet service
  include_tasks: ./start_kubelet.yml

- name: Kubernetes | Fetch OVN subnet for the node
  include_tasks: ./get_ovn_subnet.yml

# This will create the HNS network if it doesn't exist, or get its HNS id if
# it was already previously created. The variable "hns_net_id" is set at the
# end of the Setup SDN task.
- name: Kubernetes | Setup SDN
  include_tasks: ./setup_sdn.yml

- name: Kubernetes | OVS HNS network setup
  include_tasks: ./setup_ovs_hns.yml

- name: Kubernetes | Configure the Windows firewall
  block:
    - name: Disable firewall & realtime monitoring (only for testing environments)
      block:
        - name: Disable firewall
          win_command: NetSh Advfirewall set allprofiles state off

        - name: Disable realtime monitoring
          win_shell: Set-MpPreference -DisableRealtimeMonitoring $true
      when: firewall.disable_completely == True

    - name: Enable firewall (for production environments)
      win_command: NetSh Advfirewall set allprofiles state on
      when: firewall.disable_completely == False

    - name: "Enable firewall rules"
      win_shell: |
        $ErrorActionPreference = "Stop"
        $rule = Get-NetFirewallRule -Name "{{ item['name'] }}" -ErrorAction SilentlyContinue
        if($rule) {
            # Delete existing rule to re-create it with latest config
            Remove-NetFirewallRule -InputObject $rule
        }
        New-NetFirewallRule -Enabled True -Action Allow `
                            -Name "{{ item['name'] }}" -DisplayName "{{ item['name'] }}" `
                            -Protocol "{{ item['protocol'] }}" -LocalPort "{{ item['port'] }}" `
                            -Direction "{{ item['direction'] }}" 
      with_items: "{{ firewall['rules'] }}"
      when: firewall.disable_completely == False

- name: Kubernetes | Check ping to OVN gateway
  win_shell: ping {{ ovn_gateway_ip }} -n 1
  register: ping_ovn_gateway
  ignore_errors: true

- name: Kubernetes | Check if minion-init has executed already
  block:
    - debug:
        msg: Could not reach OVN gateway, running minion init
    # This will create another powershell script for minion init. OVS on Windows
    # does not allow any commands to be ran through network. Ansible uses WinRM
    # for the connection.
    - name: Kubernetes | Run minion-init
      include_tasks: ./run_minion_init.yml
  when: ping_ovn_gateway.rc != 0

# There is no infra container for Windows Server, this creates a custom
# infra container
- name: Kubernetes | Check if infra container exists
  win_shell: "docker image inspect {{ kubernetes_info.infracontainername }}"
  register: infra_inspect
  failed_when: infra_inspect.rc != 0 and infra_inspect.rc != 1
  when: ansible_kernel in supported_versions_by_the_playbook

- include_tasks: ./create_infracontainer.yml
  when: infra_inspect.rc != 0 and ansible_kernel in supported_versions_by_the_playbook

- name: Create test yaml for this host
  become: true
  become_method: sudo
  blockinfile:
    path: /root/nano-pod-{{windows_container_tag}}-{{ansible_hostname|lower}}.yaml
    create: yes
    marker: "# {mark} Ansible automatic example generation"
    block: |
      apiVersion: v1
      kind: Pod
      metadata:
        name: nano-{{windows_container_tag}}-{{ansible_hostname|lower}}
        labels:
          name: webserver
      spec:
        containers:
        - name: nano
          image: ovnkubernetes/pause
          imagePullPolicy: IfNotPresent
        # This test yaml uses a custom nanoserver container that starts a simple
        # http server that can be used for tests. It's much faster compared to the
        # IIS container.
        - name: nano2
          image: alinbalutoiu/nanoserver-web:{{windows_container_tag}}
          imagePullPolicy: IfNotPresent
        nodeSelector:
          kubernetes.io/os: windows
          kubernetes.io/hostname: {{ansible_hostname|lower}}
  delegate_to: "{{ item }}"
  with_items: "{{ groups['kube-master'] }}"
