---
- name: Kubernetes Minion | Setting OVN external_ids
  shell: |
    ovs-vsctl --timeout={{TIMEOUT}} set Open_vSwitch . external_ids:ovn-remote="tcp:{{ kubernetes_cluster_info.MASTER_IP }}:6642" \
      external_ids:ovn-nb="tcp:{{ kubernetes_cluster_info.MASTER_IP }}:6641" \
      external_ids:ovn-encap-ip="{{ host_public_ip }}" \
      external_ids:ovn-encap-type="geneve"
  changed_when: false

- name: Kubernetes Minion | Generate the client certificates for the current minion
  import_role:
    name: linux/common
    tasks_from: k8s_client_certs

- name: Kubernetes Minion | Creating kubeconfig
  blockinfile:
    path: /etc/kubernetes/kubeconfig.yaml
    create: yes
    block: |
      apiVersion: v1
      kind: Config
      clusters:
      - cluster:
          certificate-authority: {{ kubernetes_certificates.directory }}/ca.pem
          server: https://{{ kubernetes_cluster_info.MASTER_IP }}
        name: local
      users:
      - name: kubelet
        user:
          client-certificate: {{ kubernetes_certificates.directory }}/node.pem
          client-key: {{ kubernetes_certificates.directory }}/node-key.pem
      contexts:
      - context:
          cluster: local
          user: kubelet
        name: kubelet-context
      current-context: kubelet-context

- name: Kubernetes Minion | Create service kubelet
  blockinfile:
    path: /etc/systemd/system/kubelet.service
    create: yes
    block: |
      [Unit]
      Description=Kubelet Server
      Documentation=https://github.com/GoogleCloudPlatform/kubernetes
      [Service]
      ExecStart=/usr/bin/kubelet \
        --network-plugin=cni \
        --allow-privileged=true \
        --hostname-override={{ ansible_hostname }} \
        --cluster-dns={{ kubernetes_cluster_info.K8S_DNS_SERVICE_IP }} \
        --cluster-domain={{ kubernetes_cluster_info.K8S_DNS_DOMAIN }} \
        --cni-bin-dir=/opt/cni/bin \
        --cni-conf-dir=/etc/cni/net.d \
        --kubeconfig=/etc/kubernetes/kubeconfig.yaml \
        --tls-cert-file={{ kubernetes_certificates.directory }}/node.pem \
        --tls-private-key-file={{ kubernetes_certificates.directory }}/node-key.pem \
        --fail-swap-on=false
      Restart=on-failure
      RestartSec=10
      WorkingDirectory=/root/
      [Install]
      WantedBy=multi-user.target

- name: Kubernetes Minion | start kubelet
  service:
    name: "kubelet"
    enabled: yes
    state: restarted
    daemon_reload: yes
  changed_when: false

- name: Kubernetes Minion | Setup kubectl
  import_role:
    name: linux/common
    tasks_from: kubectl_client

- name: Kubernetes Minion | Prepare OVN certs
  shell: |
    set -o errexit
    ovs-vsctl --timeout={{TIMEOUT}} set Open_vSwitch . \
      external_ids:k8s-api-server="http://{{ host_public_ip }}:8080" \
      external_ids:k8s-api-token="{{TOKEN}}"
    ln -fs {{ kubernetes_certificates.directory }}/ca.pem /etc/openvswitch/k8s-ca.crt
  changed_when: false

- name: Kubernetes Minion | Ensure /etc/hosts is updated
  lineinfile:
    path: /etc/hosts
    regexp: ' {{ ansible_hostname | lower }}$'
    line: '{{ host_public_ip }} {{ ansible_hostname | lower }}'

- name: Kubernetes Minion | Create CNI binaries folder
  file:
    path: "/opt/cni/bin"
    state: directory
    mode: 0755

- name: Kubernetes Minion | get cni archive
  get_url:
    url: "{{kubernetes_binaries_info.cni_linux_download_link}}"
    dest: "/opt/cni/bin/cni.tgz"
    force_basic_auth: yes
    timeout: 30
  retries: 3

- name: Kubernetes Minion | unarchive tar.gz
  unarchive:
    src: "/opt/cni/bin/cni.tgz"
    dest: "/opt/cni/bin/"
    remote_src: yes

- name: Kubernetes Minion | Set default "init_gateway" fact if not already defined
  block:
  - set_fact:
      init_gateway: true
    when: ansible_distribution_version == '16.04'
  - set_fact:
      init_gateway: false
    when: ansible_distribution_version == '18.04'
  when: init_gateway is not defined

- fail:
    msg: "Gateway not supported yet on Ubuntu 18.04"
  when: init_gateway == true and ansible_distribution_version == '18.04'

- name: Kubernetes Minion | Setup OVN Kube service
  block:
    - name: Kubernetes Minion | Create OVN Kube systemd service file
      blockinfile:
        path: /etc/systemd/system/ovn-kubernetes-node.service
        create: yes
        block: |
          [Unit]
          Description=OVN Kube Systemd Daemon
          Documentation=https://github.com/ovn-org/ovn-kubernetes
          [Service]
          ExecStart=/usr/bin/ovnkube \
            --init-node "{{ ansible_hostname }}" \
            --cluster-subnets "{{ kubernetes_cluster_info.CLUSTER_SUBNET }}" \
            --k8s-token {{TOKEN}} \
            --k8s-cacert /etc/openvswitch/k8s-ca.crt \
            --k8s-apiserver "http://{{ kubernetes_cluster_info.MASTER_IP }}:8080" \
            --k8s-service-cidr "{{ kubernetes_cluster_info.SERVICE_CLUSTER_IP_RANGE }}" \
            --nb-address tcp://{{ kubernetes_cluster_info.MASTER_IP }}:6641 \
            --sb-address tcp://{{ kubernetes_cluster_info.MASTER_IP }}:6642
          Restart=on-failure
          RestartSec=10
          WorkingDirectory=/root/
          [Install]
          WantedBy=multi-user.target

    - name: Kubernetes Minion | Start OVN Kube service
      service:
        name: "ovn-kubernetes-node"
        enabled: yes
        state: restarted
        daemon_reload: yes
  when: not init_gateway

- name: Kubernetes Minion | Minion init with gateway
  include_tasks: ./init_gateway.yml
  when: init_gateway
