{{- /*
Copyright VMware, Inc.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.server.ingress.enabled }}
apiVersion: {{ include "common.capabilities.ingress.apiVersion" . }}
kind: Ingress
metadata:
  name: {{ template "vault.server.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: vault
    app.kubernetes.io/component: server
  {{- if or .Values.server.ingress.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.server.ingress.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  {{- if and .Values.server.ingress.ingressClassName (eq "true" (include "common.ingress.supportsIngressClassname" .)) }}
  ingressClassName: {{ .Values.server.ingress.ingressClassName | quote }}
  {{- end }}
  rules:
    {{- if .Values.server.ingress.hostname }}
    - host: {{ .Values.server.ingress.hostname }}
      http:
        paths:
          {{- if .Values.server.ingress.extraPaths }}
          {{- toYaml .Values.server.ingress.extraPaths | nindent 10 }}
          {{- end }}
          - path: {{ .Values.server.ingress.path }}
            {{- if eq "true" (include "common.ingress.supportsPathType" .) }}
            pathType: {{ .Values.server.ingress.pathType }}
            {{- end }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (printf "%s-active" (include "vault.server.fullname" .) | trunc 63 | trimSuffix "-") "servicePort" "http" "context" $)  | nindent 14 }}
    {{- end }}
    {{- range .Values.server.ingress.extraHosts }}
    - host: {{ .name | quote }}
      http:
        paths:
          - path: {{ default "/" .path }}
            {{- if eq "true" (include "common.ingress.supportsPathType" $) }}
            pathType: {{ default "ImplementationSpecific" .pathType }}
            {{- end }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (printf "%s-active" (include "vault.server.fullname" $) | trunc 63 | trimSuffix "-") "servicePort" "http" "context" $) | nindent 14 }}
    {{- end }}
    {{- if .Values.server.ingress.extraRules }}
    {{- include "common.tplvalues.render" (dict "value" .Values.server.ingress.extraRules "context" $) | nindent 4 }}
    {{- end }}
  {{- if or (and .Values.server.ingress.tls (or (include "common.ingress.certManagerRequest" ( dict "annotations" .Values.server.ingress.annotations )) .Values.server.ingress.selfSigned)) .Values.server.ingress.extraTls }}
  tls:
    {{- if and .Values.server.ingress.tls (or (include "common.ingress.certManagerRequest" ( dict "annotations" .Values.server.ingress.annotations )) .Values.server.ingress.selfSigned) }}
    - hosts:
        - {{ .Values.server.ingress.hostname | quote }}
      secretName: {{ printf "%s-tls" .Values.server.ingress.hostname }}
    {{- end }}
    {{- if .Values.server.ingress.extraTls }}
    {{- include "common.tplvalues.render" (dict "value" .Values.server.ingress.extraTls "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end }}
