helm Ignore pre-install hook failure

8/19/2020

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?

-- subrat
helmfile
kubernetes
kubernetes-helm
kubernetes-pod

2 Answers

8/31/2020

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
Source: StackOverflow

8/19/2020

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
Source: StackOverflow