{{- if and (.Values.metrics.enabled) (.Values.metrics.serviceMonitor.enabled) }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "redis.fullname" . }}
  {{- if .Values.metrics.serviceMonitor.namespace }}
  namespace: {{ .Values.metrics.serviceMonitor.namespace }}
  {{- else }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
    app: {{ template "redis.name" . }}
    chart: {{ template "redis.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    {{- range $key, $value := .Values.metrics.serviceMonitor.selector }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
spec:
  endpoints:
  - port: metrics
    {{- if .Values.metrics.serviceMonitor.interval }}
    interval: {{ .Values.metrics.serviceMonitor.interval }}
    {{- end }}
  selector:
    matchLabels:
      app: {{ template "redis.name" . }}
      release: {{ .Release.Name }}
      app.kubernetes.io/component: "metrics"
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
{{- end -}}
