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

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