{{- $ingress := .Values.ingress -}}
{{- if $ingress.enabled -}}
apiVersion: {{ include "keycloak.ingressAPIVersion" . }}
kind: Ingress
metadata:
  name: {{ include "keycloak.fullname" . }}
  {{- with $ingress.annotations }}
  annotations:
    {{- range $key, $value := . }}
    {{- printf "%s: %s" $key (tpl $value $ | quote) | nindent 4 }}
    {{- end }}
  {{- end }}
  labels:
    {{- include "keycloak.labels" . | nindent 4 }}
    {{- range $key, $value := $ingress.labels }}
    {{- printf "%s: %s" $key (tpl $value $ | quote) | nindent 4 }}
    {{- end }}
spec:
{{- if $ingress.tls }}
  tls:
    {{- range $ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ tpl . $ | quote }}
      {{- end }}
      {{- with .secretName }}
      secretName: {{ tpl . $ }}
      {{- end }}
    {{- end }}
{{- end }}
  rules:
    {{- range .Values.ingress.rules }}
    - host: {{ tpl .host $ | quote }}
      http:
        paths:
          {{- range .paths }}
          - path: {{ . }}
            {{- if $.Capabilities.APIVersions.Has "networking.k8s.io/v1/Ingress" }}
            pathType: Prefix
            backend:
              service:
                name: {{ include "keycloak.fullname" $ }}-http
                port:
                  name: {{ $ingress.servicePort }}
            {{- else }}
            backend:
              serviceName: {{ include "keycloak.fullname" $ }}-http
              servicePort: {{ $ingress.servicePort }}
            {{- end }}
          {{- end }}
    {{- end }}
{{- if $ingress.console.enabled }}
---
apiVersion: {{ include "keycloak.ingressAPIVersion" . }}
kind: Ingress
metadata:
  name: {{ include "keycloak.fullname" . }}-console
  {{- with $ingress.console.annotations }}
  annotations:
    {{- range $key, $value := . }}
    {{- printf "%s: %s" $key (tpl $value $ | quote) | nindent 4 }}
    {{- end }}
  {{- end }}
  labels:
    {{- include "keycloak.labels" . | nindent 4 }}
    {{- range $key, $value := $ingress.labels }}
    {{- printf "%s: %s" $key (tpl $value $ | quote) | nindent 4 }}
    {{- end }}
spec:
{{- if $ingress.tls }}
  tls:
    {{- range $ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ tpl . $ | quote }}
      {{- end }}
      {{- with .secretName }}
      secretName: {{ tpl . $ }}
      {{- end }}
    {{- end }}
{{- end }}
  rules:
    {{- range .Values.ingress.console.rules }}
    - host: {{ tpl .host $ | quote }}
      http:
        paths:
          {{- range .paths }}
          - path: {{ . }}
            {{- if $.Capabilities.APIVersions.Has "networking.k8s.io/v1/Ingress" }}
            pathType: Prefix
            backend:
              service:
                name: {{ include "keycloak.fullname" $ }}-http
                port:
                  name: {{ $ingress.servicePort }}
            {{- else }}
            backend:
              serviceName: {{ include "keycloak.fullname" $ }}-http
              servicePort: {{ $ingress.servicePort }}
            {{- end }}
          {{- end }}
    {{- end }}
{{- end -}}
{{- end -}}
