volume-resize-hook.yaml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. {{- $seaweedfsName := include "seaweedfs.name" $ }}
  2. {{- $volumes := deepCopy .Values.volumes | mergeOverwrite (dict "" .Values.volume) }}
  3. {{- if .Values.volume.resizeHook.enabled }}
  4. {{- $commands := list }}
  5. {{- range $vname, $volume := $volumes }}
  6. {{- $volumeName := trimSuffix "-" (printf "volume-%s" $vname) }}
  7. {{- $volume := mergeOverwrite (deepCopy $.Values.volume) (dict "enabled" true) $volume }}
  8. {{- if $volume.enabled }}
  9. {{- $replicas := int $volume.replicas -}}
  10. {{- $statefulsetName := printf "%s-%s" $seaweedfsName $volumeName -}}
  11. {{- $statefulset := (lookup "apps/v1" "StatefulSet" $.Release.Namespace $statefulsetName) -}}
  12. {{/* Check for changes in volumeClaimTemplates */}}
  13. {{- if $statefulset }}
  14. {{- range $dir := $volume.dataDirs }}
  15. {{- if eq .type "persistentVolumeClaim" }}
  16. {{- $desiredSize := .size }}
  17. {{- range $statefulset.spec.volumeClaimTemplates }}
  18. {{- if and (eq .metadata.name $dir.name) (ne .spec.resources.requests.storage $desiredSize) }}
  19. {{- $commands = append $commands (printf "kubectl delete statefulset %s --cascade=orphan" $statefulsetName) }}
  20. {{- end }}
  21. {{- end }}
  22. {{- end }}
  23. {{- end }}
  24. {{- end }}
  25. {{/* Check for the need for patching existing PVCs */}}
  26. {{- range $dir := $volume.dataDirs }}
  27. {{- if eq .type "persistentVolumeClaim" }}
  28. {{- $desiredSize := .size }}
  29. {{- range $i, $e := until $replicas }}
  30. {{- $pvcName := printf "%s-%s-%s-%d" $dir.name $seaweedfsName $volumeName $e }}
  31. {{- $currentPVC := (lookup "v1" "PersistentVolumeClaim" $.Release.Namespace $pvcName) }}
  32. {{- if and $currentPVC }}
  33. {{- $oldSize := include "common.resource-quantity" $currentPVC.spec.resources.requests.storage }}
  34. {{- $newSize := include "common.resource-quantity" $desiredSize }}
  35. {{- if gt $newSize $oldSize }}
  36. {{- $commands = append $commands (printf "kubectl patch pvc %s-%s-%s-%d -p '{\"spec\":{\"resources\":{\"requests\":{\"storage\":\"%s\"}}}}'" $dir.name $seaweedfsName $volumeName $e $desiredSize) }}
  37. {{- end }}
  38. {{- end }}
  39. {{- end }}
  40. {{- end }}
  41. {{- end }}
  42. {{- end }}
  43. {{- end }}
  44. {{- if $commands }}
  45. apiVersion: batch/v1
  46. kind: Job
  47. metadata:
  48. name: "{{ $seaweedfsName }}-volume-resize-hook"
  49. annotations:
  50. helm.sh/hook: pre-install,pre-upgrade
  51. helm.sh/hook-weight: "0"
  52. helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
  53. spec:
  54. template:
  55. spec:
  56. serviceAccountName: {{ $seaweedfsName }}-volume-resize-hook
  57. restartPolicy: Never
  58. backoffLimit: 1
  59. containers:
  60. - name: resize
  61. image: {{ .Values.volume.resizeHook.image }}
  62. command: ["sh", "-xec"]
  63. args:
  64. - |
  65. {{- range $commands }}
  66. {{ . }}
  67. {{- end }}
  68. ---
  69. apiVersion: v1
  70. kind: ServiceAccount
  71. metadata:
  72. name: {{ $seaweedfsName }}-volume-resize-hook
  73. annotations:
  74. helm.sh/hook: pre-install,pre-upgrade
  75. helm.sh/hook-weight: "-5"
  76. helm.sh/hook-delete-policy: before-hook-creation
  77. ---
  78. apiVersion: rbac.authorization.k8s.io/v1
  79. kind: Role
  80. metadata:
  81. name: {{ $seaweedfsName }}-volume-resize-hook
  82. annotations:
  83. helm.sh/hook: pre-install,pre-upgrade
  84. helm.sh/hook-weight: "-5"
  85. helm.sh/hook-delete-policy: before-hook-creation
  86. rules:
  87. - apiGroups: ["apps"]
  88. resources: ["statefulsets"]
  89. verbs: ["delete", "get", "list", "watch"]
  90. - apiGroups: [""]
  91. resources: ["persistentvolumeclaims"]
  92. verbs: ["patch", "get", "list", "watch"]
  93. ---
  94. apiVersion: rbac.authorization.k8s.io/v1
  95. kind: RoleBinding
  96. metadata:
  97. name: {{ $seaweedfsName }}-volume-resize-hook
  98. annotations:
  99. helm.sh/hook: pre-install,pre-upgrade
  100. helm.sh/hook-weight: "-5"
  101. helm.sh/hook-delete-policy: before-hook-creation
  102. subjects:
  103. - kind: ServiceAccount
  104. name: {{ $seaweedfsName }}-volume-resize-hook
  105. roleRef:
  106. kind: Role
  107. name: {{ $seaweedfsName }}-volume-resize-hook
  108. apiGroup: rbac.authorization.k8s.io
  109. {{- end }}
  110. {{- end }}