{%- if routes|length >= 1  %}
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: {{ sanitized_name }}
  namespace: {{ namespace }}
  labels:
    ownerId: {{ owner_id }}
    routerName: {{ sanitized_name }}
    routerId: {{ id }}
    envId: {{ environment_id }}
    routerLongId: {{ long_id }}
    envLongId: {{ environment_long_id }}
    projectLongId: {{ project_long_id }}
    fqdn: "{{ router_default_domain }}"
  annotations:
    external-dns.alpha.kubernetes.io/hostname: {{ router_default_domain }}
    external-dns.alpha.kubernetes.io/ttl: "300"
    kubernetes.io/tls-acme: "true"
    {%- if custom_domains|length > 0 %}
    cert-manager.io/issuer: {{ id }}
    {%- else %}
    cert-manager.io/cluster-issuer: {{ metadata_annotations_cert_manager_cluster_issuer }}
    {%- endif %}
    kubernetes.io/ingress.class: "nginx-qovery"
    ingress.kubernetes.io/ssl-redirect: "true"
    #nginx.ingress.kubernetes.io/enable-cors: "true"
    #nginx.ingress.kubernetes.io/cors-allow-headers: "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,x-csrftoken"
    {%- if sticky_sessions_enabled == true %}
    # https://kubernetes.github.io/ingress-nginx/examples/affinity/cookie/
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/affinity-mode: "persistent"
    nginx.ingress.kubernetes.io/session-cookie-secure: "true"
    nginx.ingress.kubernetes.io/session-cookie-name: "INGRESSCOOKIE_QOVERY"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "85400" # 1 day
    nginx.ingress.kubernetes.io/session-cookie-expires: "85400" # 1 day
    nginx.ingress.kubernetes.io/session-cookie-samesite: "Lax"
    {%- endif %}
spec:
  tls:
    {%- if custom_domains|length > 0 %}
    - secretName: "router-tls-{{ id }}"
      hosts:
        {%- for domain in custom_domains %}
        - "{{ domain.domain }}"
        {%- endfor %}
    {%- endif %}
  # We dont use secret name as we want to rely on default tls certificate from ingress controller
  # which has our wildcard certificate https://cert-manager.io/next-docs/faq/kubed/
  rules:
    - host: "{{ router_default_domain }}"
      http:
        paths:
        {%- for route in routes %}
        - path: "{{ route.path }}"
          backend:
            serviceName: "{{ route.application_name }}"
            servicePort: {{ route.application_port }}
        {%- endfor %}
    {%- for domain in custom_domains %}
    - host: "{{ domain.domain }}"
      http:
        paths:
        {%- for route in routes %}
        - path: "{{ route.path }}"
          backend:
            serviceName: "{{ route.application_name }}"
            servicePort: {{ route.application_port }}
        {%- endfor %}
    {%- endfor %}
{%- endif %}
