{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "nextcloud.fullname" . }}
  namespace: {{ .Values.metrics.serviceMonitor.namespace | default .Release.Namespace | quote }}
  labels:
    {{- include "nextcloud.labels"  ( dict "component" "metrics"  "rootContext" $ ) | nindent 4 }}
    {{- with .Values.metrics.serviceMonitor.labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  jobLabel: {{ .Values.metrics.serviceMonitor.jobLabel | quote }}
  selector:
    matchLabels:
      {{- include "nextcloud.selectorLabels"  ( dict "component" "metrics"  "rootContext" $ ) | nindent 6 }}
  namespaceSelector:
    {{- with .Values.metrics.serviceMonitor.namespaceSelector }}
    {{- toYaml . | nindent 4 }}
    {{- else }}
    matchNames:
      - {{ .Release.Namespace | quote }}
    {{- end }}
  endpoints:
    - port: metrics
      path: "/"
      {{- with .Values.metrics.serviceMonitor.interval }}
      interval: {{ . }}
      {{- end }}
      {{- with .Values.metrics.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ . }}
      {{- end }}
{{- end }}
