Browse code

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>

Sebastiaan van Stijn authored on 2022/08/24 19:24:31
Showing 1 changed files
... ...
@@ -154,10 +154,7 @@ func translateContainerdStartErr(setExitCode func(exitStatus), err error) error
154 154
 	// if we receive an internal error from the initial start of a container then lets
155 155
 	// return it instead of entering the restart loop
156 156
 	// set to 127 for container cmd not found/does not exist.
157
-	if contains(errDesc, "executable file not found") ||
158
-		contains(errDesc, "no such file or directory") ||
159
-		contains(errDesc, "system cannot find the file specified") ||
160
-		contains(errDesc, "failed to run runc create/exec call") {
157
+	if isInvalidCommand(errDesc) {
161 158
 		setExitCode(exitCmdNotFound)
162 159
 		retErr = startInvalidConfigError(errDesc)
163 160
 	}
... ...
@@ -177,3 +174,23 @@ func translateContainerdStartErr(setExitCode func(exitStatus), err error) error
177 177
 	// TODO: it would be nice to get some better errors from containerd so we can return better errors here
178 178
 	return retErr
179 179
 }
180
+
181
+// isInvalidCommand tries to detect if the reason the container failed to start
182
+// was due to an invalid command for the container (command not found, or not
183
+// a valid executable).
184
+func isInvalidCommand(errMessage string) bool {
185
+	errMessage = strings.ToLower(errMessage)
186
+	errMessages := []string{
187
+		"executable file not found",
188
+		"no such file or directory",
189
+		"system cannot find the file specified",
190
+		"failed to run runc create/exec call",
191
+	}
192
+
193
+	for _, msg := range errMessages {
194
+		if strings.Contains(errMessage, msg) {
195
+			return true
196
+		}
197
+	}
198
+	return false
199
+}