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

{{- if and .Values.alertmanager.enabled .Values.alertmanager.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "kube-prometheus.alertmanager.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.alertmanager.labels" . | nindent 4 }}
    {{- if .Values.alertmanager.serviceMonitor.labels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.alertmanager.serviceMonitor.labels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if or .Values.commonAnnotations .Values.alertmanager.serviceMonitor.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.alertmanager.serviceMonitor.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.alertmanager.serviceMonitor.jobLabel | quote }}
  selector:
    matchLabels:
      app.kubernetes.io/component: alertmanager
      {{- if .Values.alertmanager.serviceMonitor.selector }}
      {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.serviceMonitor.selector "context" $) | nindent 6 }}
      {{- end }}
  namespaceSelector:
    matchNames:
      - {{ include "common.names.namespace" . | quote }}
  endpoints:
    - port: http
      path: {{ trimSuffix "/" .Values.alertmanager.routePrefix }}/metrics
      {{- if .Values.alertmanager.serviceMonitor.interval }}
      interval: {{ .Values.alertmanager.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.alertmanager.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.alertmanager.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.alertmanager.serviceMonitor.honorLabels }}
      honorLabels: {{ .Values.alertmanager.serviceMonitor.honorLabels }}
      {{- end }}
      {{- if .Values.alertmanager.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.alertmanager.serviceMonitor.metricRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.alertmanager.serviceMonitor.relabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.alertmanager.serviceMonitor.relabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.alertmanager.serviceMonitor.extraParameters }}
      {{- toYaml .Values.alertmanager.serviceMonitor.extraParameters | nindent 6 }}
      {{- end }}
{{- end }}
