{{- include "seaweedfs.compat" . -}}
{{ $volumes := deepCopy .Values.volumes | mergeOverwrite (dict "" .Values.volume)  }}
{{- range $vname, $volume := $volumes }}
{{- $volumeName := trimSuffix "-" (printf "volume-%s" $vname) }}
{{- $volume := mergeOverwrite (deepCopy $.Values.volume) (dict "enabled" true) $volume }}

{{- if $volume.enabled }}
{{- if $volume.metricsPort }}
{{- if $.Values.global.seaweedfs.monitoring.enabled }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "seaweedfs.fullname" $ }}-{{ $volumeName }}
  namespace: {{ $.Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "seaweedfs.name" $ }}
    helm.sh/chart: {{ $.Chart.Name }}-{{ $.Chart.Version | replace "+" "_" }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
    app.kubernetes.io/instance: {{ $.Release.Name }}
    app.kubernetes.io/component: {{ $volumeName }}
    {{- with $.Values.global.seaweedfs.monitoring.additionalLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
{{- with $volume.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
{{- end }}
spec:
  endpoints:
    - interval: 30s
      port: metrics
      scrapeTimeout: 5s
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "seaweedfs.name" $ }}
      app.kubernetes.io/instance: {{ $.Release.Name }}
      app.kubernetes.io/component: {{ $volumeName }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
