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

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