{% set dns_names = kubify_config | json_query('service.names') | default([app_name], true) %}

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: "{{ app_name }}"
  labels:
    app: "{{ app_name }}"
    kubify_version: "{{ kubify_version }}"
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"

{% if cert_issuer == 'ca-issuer' %}
    certmanager.k8s.io/issuer: "{{ cert_issuer }}"
{% else %}
    certmanager.k8s.io/cluster-issuer: "{{ cert_issuer }}"
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/tls-acme: "true"
    external-dns.alpha.kubernetes.io/alias: "true"
{% endif %}
spec:
  tls:
    - hosts:
{% for dns_name in dns_names %}
{% set host_name = dns_name + '.' + env_domain %}
      - "{{ host_name }}"
      secretName: "{{ host_name | replace('.', '-') }}-v2"
{% endfor %}
  rules:
{% for dns_name in dns_names %}
{% set host_name = dns_name + '.' + env_domain %}
  - host: "{{ host_name }}"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: "{{ app_name }}"
            port:
              number: 80
{% if (kubify_config | json_query('release.type')) == 'static_assets' or (kubify_config | json_query('configPath') is defined) %}
      - pathType: Prefix
        path: "{{ kubify_config | json_query('configPath') | default('/config.json', true) }}"
        backend:
          service:
            name: "{{ app_name }}-config"
            port:
              number: 80
{% endif %}
{% endfor %}