{{- if .Values.ingress.enabled -}}
{{- $fullName := include "vaultwarden.fullname" . -}}
{{- if .Capabilities.APIVersions.Has "networking.k8s.io/v1/Ingress" -}}
apiVersion: networking.k8s.io/v1
{{- else if .Capabilities.APIVersions.Has "networking.k8s.io/v1beta1/Ingress" -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    {{- include "vaultwarden.labels" . | nindent 4 }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.ingress.className }}
  ingressClassName: {{ .Values.ingress.className }}
  {{- end }}
  {{- if .Values.ingress.tls }}
  tls:
    {{- range .Values.ingress.tls }}
    - hosts:
        {{- range .hosts }}
        - {{ . | quote }}
        {{- end }}
      secretName: {{ .secretName }}
    {{- end }}
  {{- end }}
  rules:
    - host: {{ required "Host required for ingress" .Values.ingress.host | quote }}
      http:
        paths:
          {{- if .Values.vaultwarden.enableWebsockets }}
          - path: "/notifications/hub"
            {{- if .Capabilities.APIVersions.Has "networking.k8s.io/v1/Ingress" }}
            pathType: Prefix
            {{- end }}
            backend:
              {{- if $.Capabilities.APIVersions.Has "networking.k8s.io/v1/Ingress" }}
              service:
                name: {{ $fullName }}
                port:
                  number: {{ .Values.service.websocketPort }}
              {{- else }}
              serviceName: {{ $fullName }}
              servicePort: {{ .Values.service.websocketPort }}
              {{- end }}
          {{- end }}
          - path: "/"
            {{- if .Capabilities.APIVersions.Has "networking.k8s.io/v1/Ingress" }}
            pathType: Prefix
            {{- end }}
            backend:
              {{- if $.Capabilities.APIVersions.Has "networking.k8s.io/v1/Ingress" }}
              service:
                name: {{ $fullName }}
                port:
                  number: {{ .Values.service.httpPort }}
              {{- else }}
              serviceName: {{ $fullName }}
              servicePort: {{ .Values.service.httpPort }}
              {{- end }}
{{- end }}
