How to delete job pods once it will completed after retrying using backofflimit

9/29/2021

In my case I have not added any parameter for backofflimit in kind:job so job will retry for 6 times and if it is completed it will remove all pods Error and Completed as used hook delete policy.

apiVersion: batch/v1
kind: Job
metadata:
  name: create-job
  annotations:
    "helm.sh/hook": "post-install"
    "helm.sh/hook-delete-policy": "hook-succeeded,before-hook-creation"
    "helm.sh/hook-weight": "1"
spec:
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: create-job

Problem Statement: This job is dependent on another Pod, So now that pod is taking time to come in running state. So job runs for 6 times and not able to succeed, I have added backofflimit: 10, now the job reties 10 times and if it success in between then does not removed Error and Completed pods by default.

Thanks

-- kodu_
hook
kubernetes
kubernetes-helm

1 Answer

12/30/2021

Your failed pod should be automatically deleted once hitting backofflimit (default to 6) if your restartPolicy is defined as onFailure.

In my case - cronjob defined and should run every 15mins: enter image description here

All my job histories kept: enter image description here

Describe the job - which tells it hit backoffLimit enter image description here

The failed pod got deleted. enter image description here

enter image description here

enter image description here

Check it out https://github.com/kubernetes/kubernetes/issues/74848

-- Patrick Ding
Source: StackOverflow