<p>Is it possible to apply <code>restartPolicy</code> for the pod based on docker exit code? What I mean is that if a container will exit with code 1 then the pod should be restarted (restartPolicy = Always), but when container will exit with code 2 then pod shouldn't be restarted (restartPolicy = Never)?</p>
<p>I have a program in the container which have 2 exit codes:</p>
<ul>
<li><p>exit 1 - where a failure is recoverable (e.g. disconnection from a server as server can be down/restarted),</p>
</li>
<li><p>exit 2 - where a failure is not recoverable (passed wrong argument).</p>
<p>I want kubernetes to restart the pod when exit code is 1 but not when exit code is 2 as there will be no chance to recovery.</p>
</li>
</ul>
<p>I think I can trick kubernetes by setting exit code for non-recoverable failure to 0 (normal termination) and exit code for recoverable failure to 1. Then set <code>restartPolicy</code> to <code>onFailure</code> but I don't like this solution.</p>

<p>It is possible from Kubernetes 1.25: <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/#pod-failure-policy" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/job/#pod-failure-policy</a></p>
<p>It only exists for Kubernetes jobs.</p>
<p>In the case of your initial question, the syntax would be:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
spec:
  podFailurePolicy:
    rules:
    - action: Count
      onExitCodes:
        operator: In
        values: [1]
    - action: FailJob
      onExitCodes:
        operator: In
        values: [2]
</code></pre>
