From 23809a1e8449354972cfdc7127fa671d4e15269d Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Wed, 7 Aug 2024 20:23:30 +0200 Subject: [PATCH] Add volume-resize-hook (#5868) --- .../templates/volume-resize-hook.yaml | 114 ++++++++++++++++++ k8s/charts/seaweedfs/values.yaml | 5 + 2 files changed, 119 insertions(+) create mode 100644 k8s/charts/seaweedfs/templates/volume-resize-hook.yaml diff --git a/k8s/charts/seaweedfs/templates/volume-resize-hook.yaml b/k8s/charts/seaweedfs/templates/volume-resize-hook.yaml new file mode 100644 index 000000000..436a785e1 --- /dev/null +++ b/k8s/charts/seaweedfs/templates/volume-resize-hook.yaml @@ -0,0 +1,114 @@ +{{- if and .Values.volume.enabled .Values.volume.resizeHook.enabled }} +{{- $seaweedfsName := include "seaweedfs.name" $ }} +{{- $replicas := int .Values.volume.replicas -}} +{{- $statefulsetName := printf "%s-volume" $seaweedfsName -}} +{{- $statefulset := (lookup "apps/v1" "StatefulSet" .Release.Namespace $statefulsetName) -}} + +{{/* Check for changes in volumeClaimTemplates */}} +{{- $templateChangesRequired := false -}} +{{- if $statefulset -}} + {{- range $dir := .Values.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) -}} + {{- $templateChangesRequired = true -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end -}} +{{- end -}} + +{{/* Check for the need for patching existing PVCs */}} +{{- $pvcChangesRequired := false -}} +{{- range $dir := .Values.volume.dataDirs -}} + {{- if eq .type "persistentVolumeClaim" -}} + {{- $desiredSize := .size -}} + {{- range $i, $e := until $replicas }} + {{- $pvcName := printf "%s-%s-volume-%d" $dir.name $seaweedfsName $e -}} + {{- $currentPVC := (lookup "v1" "PersistentVolumeClaim" $.Release.Namespace $pvcName) -}} + {{- if and $currentPVC (ne ($currentPVC.spec.resources.requests.storage | toString) $desiredSize) -}} + {{- $pvcChangesRequired = true -}} + {{- end -}} + {{- end -}} + {{- end -}} +{{- end -}} + +{{- if or $templateChangesRequired $pvcChangesRequired }} +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: + - | + {{- if $pvcChangesRequired -}} + {{- range $dir := .Values.volume.dataDirs -}} + {{- if eq .type "persistentVolumeClaim" -}} + {{- $desiredSize := .size -}} + {{- range $i, $e := until $replicas }} + kubectl patch pvc {{ printf "%s-%s-volume-%d" $dir.name $seaweedfsName $e }} -p '{"spec":{"resources":{"requests":{"storage":"{{ $desiredSize }}"}}}}' + {{- end }} + {{- end }} + {{- end }} + {{- end -}} + + {{- if $templateChangesRequired }} + kubectl delete statefulset {{ $statefulsetName }} --cascade=orphan + {{- end }} +{{- 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 }} diff --git a/k8s/charts/seaweedfs/values.yaml b/k8s/charts/seaweedfs/values.yaml index e42ad069a..e53fa5969 100644 --- a/k8s/charts/seaweedfs/values.yaml +++ b/k8s/charts/seaweedfs/values.yaml @@ -305,6 +305,11 @@ volume: # size: "800Gi" # maxVolumes: 0 + # This will automatically create a job for patching Kubernetes resources if the dataDirs type is 'persistentVolumeClaim' and the size has changed. + resizeHook: + enabled: true + image: bitnami/kubectl + # idx can be defined by: # # idx: