AnjK
AnjK

Reputation: 3603

How default hook-delete-policy works in Helm 3?

I have a helm chart myapp with a job as pre-install and pre-upgrade hook. That has hook-deletion-policies mentioned as before-hook-creation.

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

As per the documentation, the job job-myapp-1 should be removed during the second helm upgrade:

before-hook-creation: Delete the previous resource before a new hook is launched (default)

But it is not being deleted. Whereas, the value hook-succeeded works as expected (deletes the job just after its successful completion).

Is there something wrong with the yaml or my understanding of how before-hook-creation works?

Update:

Keeping the annotation:

"helm.sh/hook-delete-policy": before-hook-creation

Changed job name to avoid appending the release revision number:

name: "job-{{ .Release.Name }}"

This will generate the same job name on most of the releases. Then the job gets replaced on the next helm upgrade and the pod associated with that previous job is also deleted.

However, the job will not be deleted on helm uninstall as the helm is not tracking those hook resources.

Upvotes: 1

Views: 1905

Answers (1)

Paolo Sini
Paolo Sini

Reputation: 231

If it fits your usecase, you can set the time to live field (.spec.ttlSecondsAfterFinished) on your job, so that it gets deleted automatically after a set time if it is successful:

Upvotes: 3

Related Questions