Allow the use of an external PVC for git repositories

The persistent volume used to store git repositories was managed by helm.
That meant that the volume was deleted when uninstalling the chart. It
would be safer to manage the volume separately, in case an update of a
chart fails.

This change adds the option to name an external persistent volume claim
to be used for storing the repositories. That way the volume still exists,
after the chart is uninstalled and can be rebound from a new installation.

Change-Id: I3e52aa21629600771168e71c63d6e88f7a2709e8
diff --git a/helm-charts/gerrit-replica/README.md b/helm-charts/gerrit-replica/README.md
index 32a2e13..e720056 100644
--- a/helm-charts/gerrit-replica/README.md
+++ b/helm-charts/gerrit-replica/README.md
@@ -127,10 +127,17 @@
 
 ### Storage for Git repositories
 
-| Parameter                   | Description                                     | Default |
-|-----------------------------|-------------------------------------------------|---------|
-| `gitRepositoryStorage.size` | Size of the volume storing the Git repositories | `5Gi`   |
+| Parameter                               | Description                                     | Default              |
+|-----------------------------------------|-------------------------------------------------|----------------------|
+| `gitRepositoryStorage.externalPVC.use`  | Whether to use a PVC deployed outside the chart | `false`              |
+| `gitRepositoryStorage.externalPVC.name` | Name of the external PVC                        | `git-filesystem-pvc` |
+| `gitRepositoryStorage.size`             | Size of the volume storing the Git repositories | `5Gi`                |
 
+If the git repositories should be persisted even if the chart is deleted and in
+a way that the volume containing them can be mounted by the reinstalled chart,
+the PVC claiming the volume has to be created independently of the chart. To use
+the external PVC, set `gitRepositoryStorage.externalPVC.enabled` to `true` and
+give the name of the PVC under `gitRepositoryStorage.externalPVC.name`.
 
 ### Istio
 
diff --git a/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml b/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml
index 075307f..fe3b28c 100644
--- a/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml
+++ b/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml
@@ -160,7 +160,11 @@
         {{- end }}
       - name: git-filesystem
         persistentVolumeClaim:
+          {{- if .Values.gitRepositoryStorage.externalPVC.use }}
+          claimName: {{ .Values.gitRepositoryStorage.externalPVC.name }}
+          {{- else }}
           claimName: {{ .Release.Name }}-git-filesystem-pvc
+          {{- end }}
       {{- if and .Values.gerritReplica.plugins.cache.enabled .Values.gerritReplica.plugins.downloaded }}
       - name: gerrit-plugin-cache
         persistentVolumeClaim:
diff --git a/helm-charts/gerrit-replica/templates/git-backend.deployment.yaml b/helm-charts/gerrit-replica/templates/git-backend.deployment.yaml
index fb4d010..867d029 100644
--- a/helm-charts/gerrit-replica/templates/git-backend.deployment.yaml
+++ b/helm-charts/gerrit-replica/templates/git-backend.deployment.yaml
@@ -72,7 +72,11 @@
       volumes:
       - name: git-filesystem
         persistentVolumeClaim:
+          {{- if .Values.gitRepositoryStorage.externalPVC.use }}
+          claimName: {{ .Values.gitRepositoryStorage.externalPVC.name }}
+          {{- else }}
           claimName: {{ .Release.Name }}-git-filesystem-pvc
+          {{- end }}
       - name: git-backend-secret
         secret:
           secretName: {{ .Release.Name }}-git-backend-secret
diff --git a/helm-charts/gerrit-replica/templates/git-gc.cronjob.yaml b/helm-charts/gerrit-replica/templates/git-gc.cronjob.yaml
index f14add6..4cb8e88 100644
--- a/helm-charts/gerrit-replica/templates/git-gc.cronjob.yaml
+++ b/helm-charts/gerrit-replica/templates/git-gc.cronjob.yaml
@@ -40,7 +40,11 @@
           volumes:
           - name: git-filesystem
             persistentVolumeClaim:
+              {{- if .Values.gitRepositoryStorage.externalPVC.use }}
+              claimName: {{ .Values.gitRepositoryStorage.externalPVC.name }}
+              {{- else }}
               claimName: {{ .Release.Name }}-git-filesystem-pvc
+              {{- end }}
           - name: git-gc-logs
             {{ if .Values.gitGC.logging.persistence.enabled -}}
             persistentVolumeClaim:
diff --git a/helm-charts/gerrit-replica/templates/git-repositories-init.job.yaml b/helm-charts/gerrit-replica/templates/git-repositories-init.job.yaml
index 34ff66d..1ea6981 100644
--- a/helm-charts/gerrit-replica/templates/git-repositories-init.job.yaml
+++ b/helm-charts/gerrit-replica/templates/git-repositories-init.job.yaml
@@ -59,7 +59,11 @@
         emptyDir: {}
       - name: git-filesystem
         persistentVolumeClaim:
+          {{- if .Values.gitRepositoryStorage.externalPVC.use }}
+          claimName: {{ .Values.gitRepositoryStorage.externalPVC.name }}
+          {{- else }}
           claimName: {{ .Release.Name }}-git-filesystem-pvc
+          {{- end }}
       - name: gerrit-init-config
         configMap:
           name: {{ .Release.Name }}-gerrit-init-configmap
diff --git a/helm-charts/gerrit-replica/templates/git-repositories.storage.yaml b/helm-charts/gerrit-replica/templates/git-repositories.storage.yaml
index bb786b2..543654e 100644
--- a/helm-charts/gerrit-replica/templates/git-repositories.storage.yaml
+++ b/helm-charts/gerrit-replica/templates/git-repositories.storage.yaml
@@ -1,3 +1,4 @@
+{{- if not .Values.gitRepositoryStorage.externalPVC.use }}
 kind: PersistentVolumeClaim
 apiVersion: v1
 metadata:
@@ -9,3 +10,4 @@
     requests:
       storage: {{ .Values.gitRepositoryStorage.size }}
   storageClassName: {{ .Values.storageClasses.shared.name }}
+{{- end }}
diff --git a/helm-charts/gerrit-replica/values.yaml b/helm-charts/gerrit-replica/values.yaml
index 3a91396..bc17e35 100644
--- a/helm-charts/gerrit-replica/values.yaml
+++ b/helm-charts/gerrit-replica/values.yaml
@@ -43,6 +43,9 @@
 
 
 gitRepositoryStorage:
+  externalPVC:
+    use: false
+    name: git-filesystem-pvc
   size: 5Gi