Is it possible to define restartPolicy based on container exit code?

2/14/2018

Is it possible to apply restartPolicy 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)?

I have a program in the container which have 2 exit codes:

  • exit 1 - where a failure is recoverable (e.g. disconnection from a server as server can be down/restarted),
  • exit 2 - where a failure is not recoverable (passed wrong argument).

    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.

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 restartPolicy to onFailure but I don't like this solution.

-- blekione
kubernetes

1 Answer

2/14/2018

No. It's not clear what "passed wrong argument" means, but from a lifecycle perspective it sounds like what one wants is to prevent pods where that is the case from being admitted to the cluster altogether. If it is possible to detect the "passed wrong argument" condition without running the container, then one can use an AdmissionController for this purpose.

-- Jonah Benton
Source: StackOverflow