{{- if and .Values.exporters.jmx.enabled .Values.prometheus.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "zookeeper.fullname" . }}
  {{- if .Values.prometheus.serviceMonitor.namespace }}
  namespace: {{ .Values.prometheus.serviceMonitor.namespace }}
  {{- else }}
  namespace: {{ .Release.Namespace  }}
  {{- end }}
  labels:
{{ toYaml .Values.prometheus.serviceMonitor.selector | indent 4 }}
spec:
  endpoints:
  {{- range $key, $port := .Values.exporters.jmx.ports }}
    - port: {{ $key }}
      path: {{ $.Values.exporters.jmx.path }}
      interval: {{ $.Values.exporters.jmx.serviceMonitor.interval }}
      scrapeTimeout: {{ $.Values.exporters.jmx.serviceMonitor.scrapeTimeout }}
      scheme: {{ $.Values.exporters.jmx.serviceMonitor.scheme }}
  {{- end }}
  selector:
    matchLabels:
      app: {{ include "zookeeper.name" . }}
      release: {{ .Release.Name }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
{{- end }}
---

{{- if and .Values.exporters.zookeeper.enabled .Values.prometheus.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "zookeeper.fullname" . }}-exporter
  {{- if .Values.prometheus.serviceMonitor.namespace }}
  namespace: {{ .Values.prometheus.serviceMonitor.namespace }}
  {{- else }}
  namespace: {{ .Release.Namespace  }}
  {{- end }}
  labels:
{{ toYaml .Values.prometheus.serviceMonitor.selector | indent 4 }}
spec:
  endpoints:
  {{- range $key, $port := .Values.exporters.zookeeper.ports }}
    - port: {{ $key }}
      path: {{ $.Values.exporters.zookeeper.path }}
      interval: {{ $.Values.exporters.zookeeper.serviceMonitor.interval }}
      scrapeTimeout: {{ $.Values.exporters.zookeeper.serviceMonitor.scrapeTimeout }}
      scheme: {{ $.Values.exporters.zookeeper.serviceMonitor.scheme }}
  {{- end }}
  selector:
    matchLabels:
      app: {{ include "zookeeper.name" . }}
      release: {{ .Release.Name }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
{{- end }}