diff --git a/pkg/kubelet/kuberuntime/helpers.go b/pkg/kubelet/kuberuntime/helpers.go index 8974888a15f..a3deec2a793 100644 --- a/pkg/kubelet/kuberuntime/helpers.go +++ b/pkg/kubelet/kuberuntime/helpers.go @@ -142,6 +142,11 @@ func (m *kubeGenericRuntimeManager) getImageUser(image string) (*int64, string, // isInitContainerFailed returns true if container has exited and exitcode is not zero // or is in unknown state. 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..55cd2eb96c4 100644 --- a/pkg/kubelet/kuberuntime/helpers_test.go +++ b/pkg/kubelet/kuberuntime/helpers_test.go @@ -39,6 +39,56 @@ func seccompLocalhostPath(profileName string) string { return "localhost/" + seccompLocalhostRef(profileName) } +func TestIsInitContainerFailed(t *testing.T) { + tests := []struct { + status *kubecontainer.Status + isFailed bool + }{ + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateExited, + ExitCode: 1, + }, + isFailed: true, + }, + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateUnknown, + }, + isFailed: true, + }, + { + status: &kubecontainer.Status{ + Reason: "OOMKilled", + }, + isFailed: true, + }, + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateExited, + ExitCode: 0, + }, + isFailed: false, + }, + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateRunning, + }, + isFailed: false, + }, + { + status: &kubecontainer.Status{ + State: kubecontainer.ContainerStateCreated, + }, + isFailed: false, + }, + } + for i, test := range tests { + isFailed := isInitContainerFailed(test.status) + assert.Equal(t, test.isFailed, isFailed, "TestCase[%d]", i) + } +} + func TestStableKey(t *testing.T) { container := &v1.Container{ Name: "test_container",