{{ $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 }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ template "seaweedfs.name" $ }}-{{ $volumeName }}
  namespace: {{ $.Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "seaweedfs.name" $ }}
    app.kubernetes.io/component: {{ $volumeName }}
    helm.sh/chart: {{ $.Chart.Name }}-{{ $.Chart.Version | replace "+" "_" }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
{{- if $volume.annotations }}
  annotations:
    {{- toYaml $volume.annotations | nindent 4 }}
{{- end }}
spec:
  clusterIP: None
  internalTrafficPolicy: {{ $volume.internalTrafficPolicy | default "Cluster" }}
  ports:
  - name: "swfs-volume"
    port: {{ $volume.port }}
    targetPort: {{ $volume.port }}
    protocol: TCP
  - name: "swfs-volume-18080"
    port: {{ $volume.grpcPort }}
    targetPort: {{ $volume.grpcPort }}
    protocol: TCP
{{- if $volume.metricsPort }}
  - name: "metrics"
    port: {{ $volume.metricsPort }}
    targetPort: {{ $volume.metricsPort }}
    protocol: TCP
{{- end }}
  selector:
    app.kubernetes.io/name: {{ template "seaweedfs.name" $ }}
    app.kubernetes.io/component: {{ $volumeName }}
{{- end }}
{{- end }}
