{{- if .Values.monitoring.serviceMonitor.enabled }}
{{- if not .Values.config.metrics.enabled }}
{{- fail "ServiceMonitor is enabled but metrics are disabled. Please enable config.metrics.enabled or disable monitoring.serviceMonitor.enabled" }}
{{- end }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "marmot.fullname" . }}
  labels:
    {{- include "marmot.labels" . | nindent 4 }}
    {{- with .Values.monitoring.serviceMonitor.labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- with .Values.monitoring.serviceMonitor.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  selector:
    matchLabels:
      {{- include "marmot.selectorLabels" . | nindent 6 }}
  endpoints:
    - port: metrics
      path: {{ .Values.monitoring.serviceMonitor.path }}
      interval: {{ .Values.monitoring.serviceMonitor.interval }}
      scrapeTimeout: {{ .Values.monitoring.serviceMonitor.scrapeTimeout }}
{{- end }}
