daemon: translateContainerdStartErr(): extract detecting wrong cmd

To make the code slightly more readable, and slightly DRY.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-08-24 12:24:31 +02:00
parent a756fa60ef
commit 4e750caf96
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -154,10 +154,7 @@ func translateContainerdStartErr(setExitCode func(exitStatus), err error) error
// if we receive an internal error from the initial start of a container then lets
// return it instead of entering the restart loop
// set to 127 for container cmd not found/does not exist.
if contains(errDesc, "executable file not found") ||
contains(errDesc, "no such file or directory") ||
contains(errDesc, "system cannot find the file specified") ||
contains(errDesc, "failed to run runc create/exec call") {
if isInvalidCommand(errDesc) {
setExitCode(exitCmdNotFound)
retErr = startInvalidConfigError(errDesc)
}
@ -177,3 +174,23 @@ func translateContainerdStartErr(setExitCode func(exitStatus), err error) error
// TODO: it would be nice to get some better errors from containerd so we can return better errors here
return retErr
}
// isInvalidCommand tries to detect if the reason the container failed to start
// was due to an invalid command for the container (command not found, or not
// a valid executable).
func isInvalidCommand(errMessage string) bool {
errMessage = strings.ToLower(errMessage)
errMessages := []string{
"executable file not found",
"no such file or directory",
"system cannot find the file specified",
"failed to run runc create/exec call",
}
for _, msg := range errMessages {
if strings.Contains(errMessage, msg) {
return true
}
}
return false
}