diff --git a/pkg/kubelet/kuberuntime/helpers.go b/pkg/kubelet/kuberuntime/helpers.go index a3deec2a793..9887fd68ce6 100644 --- a/pkg/kubelet/kuberuntime/helpers.go +++ b/pkg/kubelet/kuberuntime/helpers.go @@ -139,8 +139,10 @@ 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 in unknown state. +// 3. container occurs OOMKilled. func isInitContainerFailed(status *kubecontainer.Status) bool { // When oomkilled occurs, init container should be considered as a failure. if status.Reason == "OOMKilled" { diff --git a/pkg/kubelet/kuberuntime/helpers_test.go b/pkg/kubelet/kuberuntime/helpers_test.go index 55cd2eb96c4..9f6d379f6a0 100644 --- a/pkg/kubelet/kuberuntime/helpers_test.go +++ b/pkg/kubelet/kuberuntime/helpers_test.go @@ -41,51 +41,59 @@ func seccompLocalhostPath(profileName string) string { func TestIsInitContainerFailed(t *testing.T) { tests := []struct { - status *kubecontainer.Status - isFailed bool + status *kubecontainer.Status + isFailed bool + description string }{ { status: &kubecontainer.Status{ State: kubecontainer.ContainerStateExited, ExitCode: 1, }, - isFailed: true, + isFailed: true, + description: "Init container which state is exited and exitcode is non-zero shoud return true", }, { status: &kubecontainer.Status{ State: kubecontainer.ContainerStateUnknown, }, - isFailed: true, + isFailed: true, + description: "Init container which state is unknown should return true", }, { status: &kubecontainer.Status{ - Reason: "OOMKilled", + Reason: "OOMKilled", + ExitCode: 0, }, - isFailed: true, + isFailed: true, + description: "Init container which state is exited and exitcode is zero shoud return true", }, { status: &kubecontainer.Status{ State: kubecontainer.ContainerStateExited, ExitCode: 0, }, - isFailed: false, + isFailed: false, + description: "Init container which state is exited and exitcode is zero shoud return false", }, { status: &kubecontainer.Status{ State: kubecontainer.ContainerStateRunning, }, - isFailed: false, + isFailed: false, + description: "Init container which state is running should return false", }, { status: &kubecontainer.Status{ State: kubecontainer.ContainerStateCreated, }, - isFailed: false, + isFailed: false, + description: "Init container which state is created should return false", }, } for i, test := range tests { isFailed := isInitContainerFailed(test.status) - assert.Equal(t, test.isFailed, isFailed, "TestCase[%d]", i) + assert.Equal(t, test.isFailed, isFailed, "TestCase[%d]: %s", i, test.description) } }