{{- if .Values.serviceMonitor.enabled }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "elasticsearch-exporter.fullname" . }}
  {{- if .Values.serviceMonitor.namespace }}
  namespace: {{ .Values.serviceMonitor.namespace }}
  {{- end }}
  labels:
    chart: {{ template "elasticsearch-exporter.chart" . }}
    app: {{ template "elasticsearch-exporter.name" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
    {{- if .Values.serviceMonitor.labels }}
    {{- toYaml .Values.serviceMonitor.labels | nindent 4 }}
    {{- end }}
spec:
  endpoints:
  - interval: {{ .Values.serviceMonitor.interval }}
    {{- if .Values.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
    {{- end }}
    honorLabels: true
    port: {{ .Values.service.metricsPort.name }}
    path: {{ .Values.web.path }}
    scheme: {{ .Values.serviceMonitor.scheme }}
    {{- if .Values.serviceMonitor.relabelings }}
    relabelings:
    {{- toYaml .Values.serviceMonitor.relabelings | nindent 4 }}
    {{- end }}
    {{- if .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings:
    {{ toYaml .Values.serviceMonitor.metricRelabelings | nindent 4 }}
    {{- end }}
  jobLabel: "{{ .Release.Name }}"
  selector:
    matchLabels:
      app: {{ template "elasticsearch-exporter.name" . }}
      release: "{{ .Release.Name }}"
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
{{- if .Values.serviceMonitor.targetLabels }}
  targetLabels:
{{- range .Values.serviceMonitor.targetLabels }}
    - {{ . }}
{{- end }}
{{- end }}
  sampleLimit: {{ .Values.serviceMonitor.sampleLimit }}
{{- end }}
