---
- name: Setup ingress for KIND for use with Fabric Operator/Console
  hosts: localhost
  tasks:
    - name: Create kubernetes resources for the ingress
      k8s:
        definition: "{{ lookup('kubernetes.core.kustomize', dir='templates/ingress') }}"
      register: resultingress

    - name: Wait for the ingress
      command: kubectl wait --namespace ingress-nginx --for=condition=ready pod --selector=app.kubernetes.io/component=controller --timeout=2m
      changed_when: false

    # Override the cluster DNS with a local override to refer pods to the HOST interface
    # when connecting to ingress.
    - name: Need the cluster ip address
      k8s_info:
        api_version: v1
        kind: service
        namespace: ingress-nginx
        name: "ingress-nginx-controller"
      register: ingress_info

    - name: Applying CoreDNS overrides for ingress domain
      vars:
        clusterip: "{{ ingress_info.resources[0].spec.clusterIP }}"
      k8s:
        state: present
        namespace: kube-system
        resource_definition: "{{ lookup('template','templates/coredns/coredns.yaml.j2') }}"
        apply: yes

    - name: Rollout the CoreDNS
      shell: |
        kubectl -n kube-system rollout restart deployment/coredns
        kubectl wait --namespace ingress-nginx --for=condition=ready pod --selector=app.kubernetes.io/component=controller --timeout=2m
      changed_when: false
