how to make helm hooks retry only one time?

Viewed 1444

I have seen that if my post-upgrade helm hook fails, it retries 5 times before giving up. How do I make sure that the hook only tries once to succeed and give up if failed? Or can I make the helm hook retry only on specific conditions if failed, rather than always? I could not find any documentation/parameters for this use-case here.

2 Answers

You can set Pod backoff failure policy.

As far the k8s doc: Pod backoff failure policy: There are situations where you want to fail a Job after some amount of retries due to a logical error in configuration etc. To do so, set .spec.backoffLimit to specify the number of retries before considering a Job as failed. The back-off limit is set by default to 6.

Add backoffLimit: 1 in the pod spec, Ex:

spec:
      containers:
        - name:
          image:
      backoffLimit: 1
      restartPolicy: Never

Full examle:

apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Release.Name | quote }}
  labels:
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/instance: {{ .Release.Name | quote }}
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "-5"
    "helm.sh/hook-delete-policy": hook-succeeded  
spec:
  template:
    metadata:
      name: {{ .Release.Name | quote }}
      labels:
        app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
        app.kubernetes.io/instance: {{ .Release.Name | quote }}
    spec:
      backoffLimit: 1
      restartPolicy: Never
      containers:
      - name: post-install-job
        image: "alpine:3.3"
        command: ["/bin/sleep","{{ default "10" .Values.hook.job.sleepyTime }}"]

If you want the Helm hook to run just once, you have to set the backoffLimit to 0. That means, it will run and it won't back off, at all.

If you want the hook to retry once after running (and failing), then you have to set backoffLimit to 1 indeed, but that's not the same thing.

Related