<p>I have a cronjob that keeps restarting, despite its <code>RestartPolicy</code> set to <code>Never</code>:</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cron-zombie-pod-killer
spec:
  schedule: &quot;*/9 * * * *&quot;
  successfulJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        metadata:
          name: cron-zombie-pod-killer
        spec:
          containers:
            - name: cron-zombie-pod-killer
              image: bitnami/kubectl
              command:
                - &quot;/bin/sh&quot;
              args:
                - &quot;-c&quot;
                - &quot;kubectl get pods --all-namespaces --field-selector=status.phase=Failed | awk '{print $2 \&quot; --namespace=\&quot; $1}' | xargs kubectl delete pod &gt; /dev/null&quot;
          serviceAccountName: pod-read-and-delete
          restartPolicy: Never
</code></pre>
<p>I would expect it to run every 9th minute, but that's not the case.
What happens is that when there are pods to clean up (so, when there's smth to do for the pod) it would run normally. Once everything is cleared up, it keeps restarting -&gt; failing -&gt; starting, etc. in a loop every second.</p>
<p>Is there something I need to do to tell k8s that the job has been successful, even if there's nothing to do (no pods to clean up)? What makes the job loop in restarts and failures?</p>

<p>That is by design. <code>restartPolicy</code> is not applied to a CronJob, but a Pod it creates.</p>
<p>If <code>restartPolicy</code> is set to <code>Never</code>, it will ust create new pods, if the previous failed. Setting it to <code>OnFailure</code> causes the Pod to be restarted, and prevents the stream of new Pods.</p>
<p>This was discussed in this GitHub issue: <a href="https://github.com/kubernetes/kubernetes/issues/20255" rel="nofollow noreferrer">Job being constanly recreated despite RestartPolicy: Never #20255</a></p>
<hr />
<p>Your <code>kubectl</code> command results in exit code <em>123</em> (any invocation exited with a non-zero status) if there are no Pods in <em>Failed</em> state. This causes the Job to fail, and constant restarts.</p>
<p>You can fix that by forcing <code>kubectl</code> command to exit with exit code <em>0</em>. Add <code>|| exit 0</code> to the end of it:</p>
<pre class="lang-text prettyprint-override"><code>kubectl get pods --all-namespaces --field-selector=status.phase=Failed | awk '{print $2 \&quot; --namespace=\&quot; $1}' | xargs kubectl delete pod &gt; /dev/null || exit 0
</code></pre>
