{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "papercrate.fullname" . }}
  labels:
    {{- include "papercrate.labels" . | nindent 4 }}
    app.kubernetes.io/component: ingress
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.ingress.className }}
  ingressClassName: {{ .Values.ingress.className }}
  {{- end }}
  rules:
    {{- range .Values.ingress.hosts }}
    - host: {{ .host }}
      http:
        paths:
          {{- $root := $ }}
          {{- $paths := .paths | default (list (dict "path" "/" "pathType" "Prefix" "service" (dict "name" "frontend" "port" "http")) (dict "path" "/api" "pathType" "Prefix" "service" (dict "name" "backend" "port" "http"))) }}
          {{- range $paths }}
          - path: {{ .path | default "/" }}
            pathType: {{ .pathType | default "Prefix" }}
            backend:
              service:
                name: {{ printf "%s-%s" (include "papercrate.fullname" $root) (.service.name | default "frontend") }}
                port:
                  {{- $port := .service.port | default "http" -}}
                  {{- if kindIs "string" $port }}
                  name: {{ $port }}
                  {{- else }}
                  number: {{ $port }}
                  {{- end }}
          {{- end }}
    {{- end }}
  {{- if .Values.ingress.tls }}
  tls:
    {{- toYaml .Values.ingress.tls | nindent 4 }}
  {{- end }}
{{- end }}
