---
- name: Kube-router | Create annotations
  import_tasks: annotate.yml
  tags: annotate

- name: Kube-router | Create config directory
  file:
    path: /var/lib/kube-router
    state: directory
    owner: "{{ kube_owner }}"
    recurse: true
    mode: "0755"

- name: Kube-router | Create kubeconfig
  template:
    src: kubeconfig.yml.j2
    dest: /var/lib/kube-router/kubeconfig
    mode: "0644"
    owner: "{{ kube_owner }}"
  notify:
    - Reset_kube_router

- name: Kube-router | Slurp cni config
  slurp:
    src: /etc/cni/net.d/10-kuberouter.conflist
  register: cni_config_slurp
  ignore_errors: true  # noqa ignore-errors

- name: Kube-router | Set cni_config variable
  set_fact:
    cni_config: "{{ cni_config_slurp.content | b64decode | from_json }}"
  when:
    - not cni_config_slurp.failed

- name: Kube-router | Set host_subnet variable
  when:
    - cni_config is defined
    - cni_config | json_query('plugins[?bridge==`kube-bridge`].ipam.subnet') | length > 0
  set_fact:
    host_subnet: "{{ cni_config | json_query('plugins[?bridge==`kube-bridge`].ipam.subnet') | first }}"

- name: Kube-router | Create cni config
  template:
    src: cni-conf.json.j2
    dest: /etc/cni/net.d/10-kuberouter.conflist
    mode: "0644"
    owner: "{{ kube_owner }}"
  notify:
    - Reset_kube_router

- name: Kube-router | Delete old configuration
  file:
    path: /etc/cni/net.d/10-kuberouter.conf
    state: absent

- name: Kube-router | Create manifest
  template:
    src: kube-router.yml.j2
    dest: "{{ kube_config_dir }}/kube-router.yml"
    mode: "0644"
  delegate_to: "{{ groups['kube_control_plane'] | first }}"
  run_once: true

- name: Kube-router | Start Resources
  kube:
    name: "kube-router"
    kubectl: "{{ bin_dir }}/kubectl"
    filename: "{{ kube_config_dir }}/kube-router.yml"
    resource: "ds"
    namespace: "kube-system"
    state: "latest"
  delegate_to: "{{ groups['kube_control_plane'] | first }}"
  run_once: true

- name: Kube-router | Wait for kube-router pods to be ready
  command: "{{ kubectl }} -n kube-system get pods -l k8s-app=kube-router -o jsonpath='{.items[?(@.status.containerStatuses[0].ready==false)].metadata.name}'"   # noqa ignore-errors
  register: pods_not_ready
  until: pods_not_ready.stdout.find("kube-router")==-1
  retries: 30
  delay: 10
  ignore_errors: true
  delegate_to: "{{ groups['kube_control_plane'] | first }}"
  run_once: true
  changed_when: false
