---
# The following makes sure that the second time we enter init_gateway we actually
# fetch the physical interface name and not the bridge name
# TODO: interfaces that start with 'br' are not supported
- name: OVN Gateway | Physical interface
  set_fact:
    physical_interface_name: "{{ interface_name }}"
  when: interface_name[0:2] != "br"

- name: OVN Gateway | Physical interface
  set_fact:
    physical_interface_name: "{{ interface_name[2:] }}"
  when: interface_name[0:2] == "br"

- name: OVN Gateway | Create service OVN Kube Gateway
  blockinfile:
    path: /etc/systemd/system/ovn-kubernetes-node.service
    create: yes
    block: |
      [Unit]
      Description=OVN Gateway Helper
      Documentation=https://github.com/openvswitch/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 }}" \
        -nodeport \
        -init-gateways \
        -gateway-interface={{physical_interface_name}} \
        -gateway-nexthop="{{gateway_next_hop}}" \
        -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: OVN Gateway | start OVN Kube Gateway Helper
  service:
    name: "ovn-kubernetes-node"
    enabled: yes
    state: restarted
    daemon_reload: yes
  changed_when: false
