{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.alertmanager.enabled .Values.alertmanager.ingress.enabled }}
apiVersion: {{ include "common.capabilities.ingress.apiVersion" . }}
kind: Ingress
metadata:
  name: {{ template "kube-prometheus.alertmanager.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.alertmanager.labels" . | nindent 4 }}
  {{- if or .Values.alertmanager.ingress.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.alertmanager.ingress.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.alertmanager.ingress.ingressClassName }}
  ingressClassName: {{ .Values.alertmanager.ingress.ingressClassName | quote }}
  {{- end }}
  rules:
    {{- if .Values.alertmanager.ingress.hostname }}
    - http:
        paths:
          {{- if .Values.alertmanager.ingress.extraPaths }}
          {{- toYaml .Values.alertmanager.ingress.extraPaths | nindent 10 }}
          {{- end }}
          - path: {{ .Values.alertmanager.ingress.path }}
            pathType: {{ .Values.alertmanager.ingress.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "kube-prometheus.alertmanager.fullname" $) "servicePort" "http" "context" $)  | nindent 14 }}
      {{- if ne .Values.alertmanager.ingress.hostname "*" }}
      host: {{ .Values.alertmanager.ingress.hostname }}
      {{- end}}
    {{- end }}
    {{- range .Values.alertmanager.ingress.extraHosts }}
    - host: {{ .name | quote }}
      http:
        paths:
          - path: {{ default "/" .path }}
            pathType: {{ default "ImplementationSpecific" .pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "kube-prometheus.alertmanager.fullname" $) "servicePort" "http" "context" $) | nindent 14 }}
    {{- end }}
    {{- if .Values.alertmanager.ingress.extraRules }}
    {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.ingress.extraRules "context" $) | nindent 4 }}
    {{- end }}
  {{- if or .Values.alertmanager.ingress.tls .Values.alertmanager.ingress.extraTls }}
  tls:
    {{- if .Values.alertmanager.ingress.tls }}
    - hosts:
        - {{ .Values.alertmanager.ingress.hostname }}
      secretName: {{ printf "%s-tls" .Values.alertmanager.ingress.hostname }}
    {{- end }}
    {{- if .Values.alertmanager.ingress.extraTls }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.alertmanager.ingress.extraTls "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end }}
