{{- $seaweedfsName := include "seaweedfs.name" $ }}
{{- $volumes := deepCopy .Values.volumes | mergeOverwrite (dict "" .Values.volume)  }}


{{- if .Values.volume.resizeHook.enabled }}
{{-   $commands := list }}
{{-   range $vname, $volume := $volumes }}
{{-     $volumeName := trimSuffix "-" (printf "volume-%s" $vname) }}
{{-     $volume := mergeOverwrite (deepCopy $.Values.volume) (dict "enabled" true) $volume }}

{{-     if $volume.enabled }}
{{-       $replicas := int $volume.replicas -}}
{{-       $statefulsetName := printf "%s-%s" $seaweedfsName $volumeName -}}
{{-       $statefulset := (lookup "apps/v1" "StatefulSet" $.Release.Namespace $statefulsetName) -}}

{{/*      Check for changes in volumeClaimTemplates */}}
{{-       if $statefulset }}
{{-         range $dir := $volume.dataDirs }}
{{-           if eq .type "persistentVolumeClaim" }}
{{-             $desiredSize := .size }}
{{-             range $statefulset.spec.volumeClaimTemplates }}
{{-               if and (eq .metadata.name $dir.name) (ne .spec.resources.requests.storage $desiredSize) }}
{{-                 $commands = append $commands (printf "kubectl delete statefulset %s --cascade=orphan" $statefulsetName) }}
{{-               end }}
{{-             end }}
{{-           end }}
{{-         end }}
{{-       end }}

{{/*      Check for the need for patching existing PVCs */}}
{{-       range $dir := $volume.dataDirs }}
{{-         if eq .type "persistentVolumeClaim" }}
{{-           $desiredSize := .size }}
{{-           range $i, $e := until $replicas }}
{{-             $pvcName := printf "%s-%s-%s-%d" $dir.name $seaweedfsName $volumeName $e }}
{{-             $currentPVC := (lookup "v1" "PersistentVolumeClaim" $.Release.Namespace $pvcName) }}
{{-             if and $currentPVC }}
{{-             $oldSize := include "common.resource-quantity" $currentPVC.spec.resources.requests.storage }}
{{-             $newSize := include "common.resource-quantity" $desiredSize }}
{{-               if gt $newSize $oldSize }}
{{-               $commands = append $commands (printf "kubectl patch pvc %s-%s-%s-%d -p '{\"spec\":{\"resources\":{\"requests\":{\"storage\":\"%s\"}}}}'" $dir.name $seaweedfsName $volumeName $e $desiredSize) }}
{{-               end }}
{{-             end }}
{{-           end }}
{{-         end }}
{{-       end }}

{{-     end }}
{{-   end }}

{{-   if $commands }}
apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ $seaweedfsName }}-volume-resize-hook"
  annotations:
    helm.sh/hook: pre-install,pre-upgrade
    helm.sh/hook-weight: "0"
    helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
spec:
  template:
    spec:
      serviceAccountName: {{ $seaweedfsName }}-volume-resize-hook
      restartPolicy: Never
      backoffLimit: 1
      containers:
        - name: resize
          image: {{ .Values.volume.resizeHook.image }}
          command: ["sh", "-xec"]
          args:
            - |
              {{- range $commands }}
              {{ . }}
              {{- end }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ $seaweedfsName }}-volume-resize-hook
  annotations:
    helm.sh/hook: pre-install,pre-upgrade
    helm.sh/hook-weight: "-5"
    helm.sh/hook-delete-policy: before-hook-creation
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ $seaweedfsName }}-volume-resize-hook
  annotations:
    helm.sh/hook: pre-install,pre-upgrade
    helm.sh/hook-weight: "-5"
    helm.sh/hook-delete-policy: before-hook-creation
rules:
  - apiGroups: ["apps"]
    resources: ["statefulsets"]
    verbs: ["delete", "get", "list", "watch"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["patch", "get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ $seaweedfsName }}-volume-resize-hook
  annotations:
    helm.sh/hook: pre-install,pre-upgrade
    helm.sh/hook-weight: "-5"
    helm.sh/hook-delete-policy: before-hook-creation
subjects:
  - kind: ServiceAccount
    name: {{ $seaweedfsName }}-volume-resize-hook
roleRef:
  kind: Role
  name: {{ $seaweedfsName }}-volume-resize-hook
  apiGroup: rbac.authorization.k8s.io
{{-   end }}
{{- end }}
