diff --git a/pkg/kubelet/kuberuntime/helpers.go b/pkg/kubelet/kuberuntime/helpers.go index 8974888a15f..da24072e354 100644 --- a/pkg/kubelet/kuberuntime/helpers.go +++ b/pkg/kubelet/kuberuntime/helpers.go @@ -139,9 +139,16 @@ func (m *kubeGenericRuntimeManager) getImageUser(image string) (*int64, string, return new(int64), "", nil } -// isInitContainerFailed returns true if container has exited and exitcode is not zero -// or is in unknown state. +// isInitContainerFailed returns true under the following conditions: +// 1. container has exited and exitcode is not zero. +// 2. container is in unknown state. +// 3. container gets OOMKilled. func isInitContainerFailed(status *kubecontainer.Status) bool { + // When oomkilled occurs, init container should be considered as a failure. + if status.Reason == "OOMKilled" { + return true + } + if status.State == kubecontainer.ContainerStateExited && status.ExitCode != 0 { return true } diff --git a/pkg/kubelet/kuberuntime/helpers_test.go b/pkg/kubelet/kuberuntime/helpers_test.go index 7cc6298e636..ec67ca36b10 100644 --- a/pkg/kubelet/kuberuntime/helpers_test.go +++ b/pkg/kubelet/kuberuntime/helpers_test.go @@ -39,6 +39,64 @@ func seccompLocalhostPath(profileName string) string { return "localhost/" + seccompLocalhostRef(profileName) } +func TestIsInitContainerFailed(t *testing.T) { + tests := []struct { + status *kubecontainer.Status + isFailed bool + description string + }{ + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateExited, + ExitCode: 1, + }, + isFailed: true, + description: "Init container in exited state and non-zero exit code should return true", + }, + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateUnknown, + }, + isFailed: true, + description: "Init container in unknown state should return true", + }, + { + status: &kubecontainer.Status{ + Reason: "OOMKilled", + ExitCode: 0, + }, + isFailed: true, + description: "Init container which reason is OOMKilled should return true", + }, + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateExited, + ExitCode: 0, + }, + isFailed: false, + description: "Init container in exited state and zero exit code should return false", + }, + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateRunning, + }, + isFailed: false, + description: "Init container in running state should return false", + }, + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateCreated, + }, + isFailed: false, + description: "Init container in created state should return false", + }, + } + for i, test := range tests { + isFailed := isInitContainerFailed(test.status) + assert.Equal(t, test.isFailed, isFailed, "TestCase[%d]: %s", i, test.description) + } +} + func TestStableKey(t *testing.T) { container := &v1.Container{ Name: "test_container",