{{- if .Values.serviceMonitor.enabled  }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "stolon.fullname" . }}
  labels:
  {{- if .Values.serviceMonitor.labels }}
    {{ toYaml .Values.serviceMonitor.labels | nindent 4 }}
  {{- else }}
    app: {{ template "stolon.name" . }}
    chart: {{ template "stolon.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  {{- end }}
  {{- if .Values.serviceMonitor.namespace }}
  namespace: {{ .Values.serviceMonitor.namespace }}
  {{- end }}
spec:
  endpoints:
    - targetPort: "metrics"
      {{- if .Values.serviceMonitor.interval }}
      interval: {{ .Values.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
      {{- end }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app: {{ template "stolon.name" . }}
      release: {{ .Release.Name }}
      component: "all"
{{- end }}
