From 3d96f72dc5c49cdb25095b77ae65ae72db1f5c4a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=B6ran=20P=C3=B6hner?=
 <10630407+groundhog2k@users.noreply.github.com>
Date: Mon, 27 Mar 2023 19:17:08 +0200
Subject: [PATCH] Fix redis volume dependency when deployment in Non-HA mode
 (#1218)

---
 charts/redis/Chart.yaml                 |  2 +-
 charts/redis/README.md                  |  3 ++-
 charts/redis/RELEASENOTES.md            |  1 +
 charts/redis/templates/pvc.yaml         | 27 +++++++++++++++++++++++++
 charts/redis/templates/statefulset.yaml | 20 ++++++++++++------
 charts/redis/values.yaml                |  3 +++
 6 files changed, 48 insertions(+), 8 deletions(-)
 create mode 100644 charts/redis/templates/pvc.yaml

diff --git a/charts/redis/Chart.yaml b/charts/redis/Chart.yaml
index c5ff56e6..e8871d3c 100644
--- a/charts/redis/Chart.yaml
+++ b/charts/redis/Chart.yaml
@@ -8,7 +8,7 @@ maintainers:
   - name: groundhog2k
 
 # This is the chart version
-version: "0.6.5"
+version: "0.6.6"
 
 # This is the version number of the application being deployed.
 appVersion: "7.0.10"
diff --git a/charts/redis/README.md b/charts/redis/README.md
index 1da1cdba..1c1cabfc 100644
--- a/charts/redis/README.md
+++ b/charts/redis/README.md
@@ -1,6 +1,6 @@
 # Redis
 
-![Version: 0.6.5](https://img.shields.io/badge/Version-0.6.5-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 7.0.10](https://img.shields.io/badge/AppVersion-7.0.10-informational?style=flat-square)
+![Version: 0.6.6](https://img.shields.io/badge/Version-0.6.6-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 7.0.10](https://img.shields.io/badge/AppVersion-7.0.10-informational?style=flat-square)
 
 ## Changelog
 
@@ -142,6 +142,7 @@ helm uninstall my-release
 | storage.persistentVolumeClaimName | string | `nil` | PVC name when existing storage volume should be used |
 | storage.requestedSize | string | `nil` | Size for new PVC, when no existing PVC is used |
 | storage.className | string | `nil` | Storage class name |
+| storage.keepPvc | bool | `false` | Keep a created Persistent volume claim when uninstalling the helm chart (only for `useDeploymentWhenNonHA`) |
 
 ## Redis parameters
 
diff --git a/charts/redis/RELEASENOTES.md b/charts/redis/RELEASENOTES.md
index 39b2ee4e..fabbff79 100644
--- a/charts/redis/RELEASENOTES.md
+++ b/charts/redis/RELEASENOTES.md
@@ -25,4 +25,5 @@
 | 0.6.3 | 7.0.9 | Upgraded to Redis 7.0.9 |
 | 0.6.4 | 7.0.9 | Added option to use Deployment instead of StatefulSet for Non-HA deployments - thx @tim-hanssen |
 | 0.6.5 | 7.0.10 | Upgraded to Redis 7.0.10 |
+| 0.6.6 | 7.0.10 | Fixed redis volume deployment in non-HA mode |
 | | | |
diff --git a/charts/redis/templates/pvc.yaml b/charts/redis/templates/pvc.yaml
new file mode 100644
index 00000000..c35a0d96
--- /dev/null
+++ b/charts/redis/templates/pvc.yaml
@@ -0,0 +1,27 @@
+{{- if and (not .Values.haMode.enabled) (.Values.useDeploymentWhenNonHA) }}
+{{- $createPvc := and (empty .Values.storage.persistentVolumeClaimName) (.Values.storage.requestedSize) }}
+{{- if $createPvc }}
+apiVersion: v1
+kind: PersistentVolumeClaim
+metadata:
+  name: {{ include "redis.fullname" . }}
+  labels:
+    {{- include "redis.labels" . | nindent 4 }}
+  {{- if .Values.storage.keepPvc }}
+  annotations:
+    "helm.sh/resource-policy": keep
+  {{- end }}
+spec:
+  {{- with .Values.storage }}
+  accessModes:
+    {{- toYaml .accessModes | nindent 4 }}
+  volumeMode: Filesystem
+  resources:
+    requests:
+      storage: {{ .requestedSize }}
+  {{- if .className }}
+  storageClassName: {{ .className }}
+  {{- end }}
+  {{- end }}  
+{{- end }}
+{{- end }}
diff --git a/charts/redis/templates/statefulset.yaml b/charts/redis/templates/statefulset.yaml
index b4cbbf25..47384849 100644
--- a/charts/redis/templates/statefulset.yaml
+++ b/charts/redis/templates/statefulset.yaml
@@ -1,11 +1,14 @@
+{{- $createPvc := and (empty .Values.storage.persistentVolumeClaimName) (.Values.storage.requestedSize) }}
+{{- $deployWithNonHA := and (not .Values.haMode.enabled) (.Values.useDeploymentWhenNonHA) }}
+{{- $fullname := include "redis.fullname" . }}
 apiVersion: apps/v1
-{{- if and (not .Values.haMode.enabled) (.Values.useDeploymentWhenNonHA) }}
+{{- if $deployWithNonHA }}
 kind: Deployment
 {{- else }}
 kind: StatefulSet
 {{- end }}
 metadata:
-  name: {{ include "redis.fullname" . }}
+  name: {{ $fullname }}
   labels:
     {{- include "redis.labels" . | nindent 4 }}
 spec:
@@ -18,7 +21,7 @@ spec:
   revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
   {{- end }}
   {{- if or (.Values.haMode.enabled) (not .Values.useDeploymentWhenNonHA) }}
-  serviceName: {{ include "redis.fullname" . }}-headless
+  serviceName: {{ $fullname }}-headless
   podManagementPolicy: {{ .Values.podManagementPolicy }}
   updateStrategy: 
     type: {{ .Values.updateStrategyType }}
@@ -282,12 +285,12 @@ spec:
       volumes:
         - name: scripts
           configMap:
-            name: {{ include "redis.fullname" . }}-scripts
+            name: {{ $fullname }}-scripts
             defaultMode: 0555
         {{- if or (.Values.redisConfig) (.Values.sentinelConfig) }}
         - name: config
           configMap:
-            name: {{ include "redis.fullname" . }}
+            name: {{ $fullname }}
         {{- end }}
         {{- range $secret := .Values.extraRedisSecrets }}
         - name: {{ $secret.name }}
@@ -312,7 +315,6 @@ spec:
             secretName: {{ .Values.extraSecretSentinelConfigs }}
         {{- end }}
 {{- with .Values.storage }}
-  {{- $createPvc := and (empty .persistentVolumeClaimName) .requestedSize }}
   {{- if not $createPvc }}
         - name: redis-data
         {{- if .persistentVolumeClaimName }}
@@ -322,6 +324,11 @@ spec:
           emptyDir: {}
         {{- end }}
   {{- else }}
+  {{- if $deployWithNonHA }}
+        - name: redis-data
+          persistentVolumeClaim:
+            claimName: {{ $fullname }}
+  {{- else }}
   volumeClaimTemplates:
     - metadata:
         name: redis-data
@@ -337,4 +344,5 @@ spec:
           requests:
             storage: {{ .requestedSize }}
   {{- end }}
+  {{- end }}
 {{- end }}
diff --git a/charts/redis/values.yaml b/charts/redis/values.yaml
index d7803cbb..48298063 100644
--- a/charts/redis/values.yaml
+++ b/charts/redis/values.yaml
@@ -236,3 +236,6 @@ storage:
   ## Default access mode (ReadWriteOnce)
   accessModes:
     - ReadWriteOnce
+
+  ## Keep a created Persistent volume claim when uninstalling the helm chart (only for non-HA mode with option useDeploymentWhenNonHA: true)
+  keepPvc: false
-- 
GitLab