0

I have one pre-install hook which creates a dynamic PVC and looks like this

kind: PersistentVolumeClaim
metadata:
  name: my-dynamic-pv
  annotations:
    "helm.sh/resource-policy": keep
    "helm.sh/hook": "pre-install"
spec:
  storageClassName: {{ .Values.persistence.storageClass }}
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

I want to preserve the same PVC across restarts that's why I have provided "helm.sh/resource-policy": keep. I am able to create the PVC with the pre-install hook the very first time I start my service. But the subsequent installs/restarts are failing with error Error: persistentvolumeclaims "my-dynamic-pv" already exists.

Is there a way to ignore this failure and continue with the helm installation?

David Maze
  • 130,717
  • 29
  • 175
  • 215
subrat
  • 73
  • 2
  • 7
  • You shouldn't need to tag this as a hook; `helm upgrade` won't delete objects that haven't changed. Is there a more specific sequence of `helm` commands that's causing problems, especially without the hook annotations? – David Maze Aug 19 '20 at 14:20
  • I am actually doing a `helm del` and `helm install` here. and this is causing a problem. As per my use case, I should always purge and start my services. – subrat Aug 20 '20 at 05:55
  • Please try to use as mentioned - https://stackoverflow.com/questions/49344501/how-can-you-reuse-dynamically-provisioned-persistentvolumes-with-helm-on-gke – Anurag Jain Aug 31 '20 at 19:28

2 Answers2

6

You can use helm lookup function to check the existence of the pvc before creating it.

{{- $mypvc := (lookup "v1" "PersistentVolumeClaim" .Release.Namespace (printf "my- 
dynamic-pv")) }}
{{- if not $mypvc }}
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: my-dynamic-pv
  annotations:
    "helm.sh/resource-policy": keep
    "helm.sh/hook": "pre-install"
spec:
  storageClassName: {{ .Values.persistence.storageClass }}
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
{{- end -}}

More on helm lookup funtion - Helm template functions

I'm using helm 3.2.1. You will probably need a near version to use the lookup function.

Dhanuj Dharmarajan
  • 436
  • 1
  • 3
  • 10
5

Use --no-hooks flag to the helm command to ignore hooks.

$ helm install --help | grep "no-hooks"
     
 --no-hooks                     prevent hooks from running during install

$ helm install <NAME> <CHART> --no-hooks
Kamol Hasan
  • 12,218
  • 1
  • 37
  • 46
  • I have explored this option. The problem here is it takes an extra option `--no-hooks` with the `helm install` which is not my intention. I want to use the `helm install` without any additional options to it. – subrat Aug 20 '20 at 05:59
  • 1
    If you want to ignore something that is supposed to happen, you will definitely need to provide something extra. Period. – Kamol Hasan Aug 20 '20 at 06:06