{{- if and .Values.prometheus.enabled .Values.prometheus.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "chart.fullname" . }}
  {{- $commonLabels := (include "chart.commonLabels" .) }}
  {{- if or $commonLabels .Values.prometheus.serviceMonitor.additionalLabels }}
  labels:
    {{- with .Values.prometheus.serviceMonitor.additionalLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- with $commonLabels }}
    {{- . | nindent 4 }}
    {{- end }}
  {{- end }}
  {{- $commonAnnotations := (include "chart.commonAnnotations" .) }}
  {{- if or $commonAnnotations .Values.prometheus.serviceMonitor.additionalAnnotations }}
  annotations:
    {{- with .Values.prometheus.serviceMonitor.additionalAnnotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- with $commonAnnotations }}
    {{- . | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  selector:
    matchLabels: {{- include "chart.selectorLabels" . | nindent 6 }}
  endpoints:
    - port: metrics
      {{- if .Values.prometheus.serviceMonitor.interval }}
      interval: {{ .Values.prometheus.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.prometheus.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.prometheus.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.prometheus.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{ toYaml .Values.prometheus.serviceMonitor.metricRelabelings | nindent 8 }}
      {{- end }}
      {{- if .Values.prometheus.serviceMonitor.relabelings }}
      relabelings: {{ toYaml .Values.prometheus.serviceMonitor.relabelings | nindent 8 }}
      {{- end }}
{{- end }}
