From afde4c8bc4297a3573cf9186e5e4d7b7170dabb7 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Sun, 29 Aug 2021 12:43:33 +0800 Subject: [PATCH 1/6] fix init container oomkilled as a failure Signed-off-by: yxxhero --- pkg/kubelet/kuberuntime/helpers.go | 5 +++ pkg/kubelet/kuberuntime/helpers_test.go | 50 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+) 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", From 71a91d55cbd0b787a4865fc3a3e3c02fc3b4ff02 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Fri, 3 Sep 2021 07:20:28 +0800 Subject: [PATCH 2/6] update func description --- pkg/kubelet/kuberuntime/helpers.go | 6 ++++-- pkg/kubelet/kuberuntime/helpers_test.go | 28 ++++++++++++++++--------- 2 files changed, 22 insertions(+), 12 deletions(-) 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) } } From 2f448a078973df7adb4e7fe8ffd60a0ec2eabc35 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Fri, 3 Sep 2021 22:07:46 +0800 Subject: [PATCH 3/6] fix oomkilled description Signed-off-by: yxxhero --- pkg/kubelet/kuberuntime/helpers_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kubelet/kuberuntime/helpers_test.go b/pkg/kubelet/kuberuntime/helpers_test.go index 9f6d379f6a0..7344460640c 100644 --- a/pkg/kubelet/kuberuntime/helpers_test.go +++ b/pkg/kubelet/kuberuntime/helpers_test.go @@ -66,7 +66,7 @@ func TestIsInitContainerFailed(t *testing.T) { ExitCode: 0, }, isFailed: true, - description: "Init container which state is exited and exitcode is zero shoud return true", + description: "Init container which reason is OOMKilled shoud return true", }, { status: &kubecontainer.Status{ From 5ba76eb91115e146b74eaa5b9b9fed5fcc3bdf7f Mon Sep 17 00:00:00 2001 From: yxxhero Date: Tue, 14 Sep 2021 09:03:29 +0800 Subject: [PATCH 4/6] fix typo Signed-off-by: yxxhero --- pkg/kubelet/kuberuntime/helpers.go | 2 +- pkg/kubelet/kuberuntime/helpers_test.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/kubelet/kuberuntime/helpers.go b/pkg/kubelet/kuberuntime/helpers.go index 9887fd68ce6..d31d0cf479e 100644 --- a/pkg/kubelet/kuberuntime/helpers.go +++ b/pkg/kubelet/kuberuntime/helpers.go @@ -142,7 +142,7 @@ func (m *kubeGenericRuntimeManager) getImageUser(image string) (*int64, string, // 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. +// 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" { diff --git a/pkg/kubelet/kuberuntime/helpers_test.go b/pkg/kubelet/kuberuntime/helpers_test.go index 7344460640c..596a258d362 100644 --- a/pkg/kubelet/kuberuntime/helpers_test.go +++ b/pkg/kubelet/kuberuntime/helpers_test.go @@ -51,14 +51,14 @@ func TestIsInitContainerFailed(t *testing.T) { ExitCode: 1, }, isFailed: true, - description: "Init container which state is exited and exitcode is non-zero shoud return true", + description: "Init container which in exited state and non-zero exit code should return true", }, { status: &kubecontainer.Status{ State: kubecontainer.ContainerStateUnknown, }, isFailed: true, - description: "Init container which state is unknown should return true", + description: "Init container which in unknown state should return true", }, { status: &kubecontainer.Status{ @@ -66,7 +66,7 @@ func TestIsInitContainerFailed(t *testing.T) { ExitCode: 0, }, isFailed: true, - description: "Init container which reason is OOMKilled shoud return true", + description: "Init container which reason is OOMKilled should return true", }, { status: &kubecontainer.Status{ @@ -74,21 +74,21 @@ func TestIsInitContainerFailed(t *testing.T) { ExitCode: 0, }, isFailed: false, - description: "Init container which state is exited and exitcode is zero shoud return false", + description: "Init container which in exited state and zero exit code should return false", }, { status: &kubecontainer.Status{ State: kubecontainer.ContainerStateRunning, }, isFailed: false, - description: "Init container which state is running should return false", + description: "Init container which in running state should return false", }, { status: &kubecontainer.Status{ State: kubecontainer.ContainerStateCreated, }, isFailed: false, - description: "Init container which state is created should return false", + description: "Init container which in created state should return false", }, } for i, test := range tests { From 20b3cd519826fe866f8d64970dce2c9a9fb6a21b Mon Sep 17 00:00:00 2001 From: yxxhero Date: Tue, 14 Sep 2021 09:04:59 +0800 Subject: [PATCH 5/6] fix typo Signed-off-by: yxxhero --- pkg/kubelet/kuberuntime/helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kubelet/kuberuntime/helpers.go b/pkg/kubelet/kuberuntime/helpers.go index d31d0cf479e..da24072e354 100644 --- a/pkg/kubelet/kuberuntime/helpers.go +++ b/pkg/kubelet/kuberuntime/helpers.go @@ -141,7 +141,7 @@ func (m *kubeGenericRuntimeManager) getImageUser(image string) (*int64, string, // isInitContainerFailed returns true under the following conditions: // 1. container has exited and exitcode is not zero. -// 2. container in unknown state. +// 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. From c1b94d27d923ce28f707710471b20cab7a9f8600 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Tue, 14 Sep 2021 23:24:14 +0800 Subject: [PATCH 6/6] fix typo Signed-off-by: yxxhero --- pkg/kubelet/kuberuntime/helpers_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/kubelet/kuberuntime/helpers_test.go b/pkg/kubelet/kuberuntime/helpers_test.go index 596a258d362..ec67ca36b10 100644 --- a/pkg/kubelet/kuberuntime/helpers_test.go +++ b/pkg/kubelet/kuberuntime/helpers_test.go @@ -51,14 +51,14 @@ func TestIsInitContainerFailed(t *testing.T) { ExitCode: 1, }, isFailed: true, - description: "Init container which in exited state and non-zero exit code should return 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 which in unknown state should return true", + description: "Init container in unknown state should return true", }, { status: &kubecontainer.Status{ @@ -74,21 +74,21 @@ func TestIsInitContainerFailed(t *testing.T) { ExitCode: 0, }, isFailed: false, - description: "Init container which in exited state and zero exit code should return 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 which in running state should return false", + description: "Init container in running state should return false", }, { status: &kubecontainer.Status{ State: kubecontainer.ContainerStateCreated, }, isFailed: false, - description: "Init container which in created state should return false", + description: "Init container in created state should return false", }, } for i, test := range tests {