{{- $cozyConfig := lookup "v1" "ConfigMap" "cozy-system" "cozystack" }}
{{- $issuerType := (index $cozyConfig.data "clusterissuer") | default "http01" }}

{{- $myNS := lookup "v1" "Namespace" "" .Release.Namespace }}
{{- $ingress := index $myNS.metadata.annotations "namespace.cozystack.io/ingress" }}
{{- $host := index $myNS.metadata.annotations "namespace.cozystack.io/host" }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: bootbox
  labels:
    app: bootbox
  annotations:
    {{- if ne $issuerType "cloudflare" }}
    acme.cert-manager.io/http01-ingress-class: {{ $ingress }}
    {{- end }}
    cert-manager.io/cluster-issuer: letsencrypt-prod
    {{- if .Values.whitelistHTTP }}
    nginx.ingress.kubernetes.io/whitelist-source-range: "{{ join "," (.Values.whitelist | default "0.0.0.0/32") }}"
    {{- end }}
spec:
  ingressClassName: {{ $ingress }}
  tls:
    - hosts:
        - "{{ printf "bootbox.%s" (.Values.host | default $host) }}"
      secretName: bootbox-tls
  rules:
    - host: "{{ printf "bootbox.%s" (.Values.host | default $host) }}"
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: bootbox
                port:
                  name: http
