{{- if not $.Values.crds.exclusive }}
  {{- with (mergeOverwrite .Values.monitoring.serviceMonitor (default dict .Values.serviceMonitor)) -}}
    {{- if .enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "capsule.fullname" $ }}
  namespace: {{ .namespace | default $.Release.Namespace }}
  labels:
    {{- include "capsule.labels" $ | nindent 4 }}
    {{- with .labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- with .annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  endpoints:
  {{- with .endpoint }}
  - interval: {{ .interval }}
    port: metrics
    path: /metrics
    {{- with .scrapeTimeout }}
    scrapeTimeout: {{ . }}
    {{- end }}
    {{- with .metricRelabelings }}
    metricRelabelings: {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with .relabelings }}
    relabelings: {{- toYaml . | nindent 6 }}
    {{- end }}
  {{- end }}
  jobLabel: app.kubernetes.io/name
  {{- with .targetLabels }}
  targetLabels: {{- toYaml . | nindent 4 }}
  {{- end }}
  selector:
    matchLabels:
    {{- if .matchLabels }}
      {{- toYaml .matchLabels | nindent 6 }}
    {{- else }}
      {{- include "capsule.selectorLabels" $ | nindent 6 }}
    {{- end }}
  namespaceSelector:
    matchNames:
      - {{ $.Release.Namespace }}
    {{- end }}
  {{- end }}
{{- end }}
