From 2a0efdd7af63fb8875079e0899396e8e9c034fdf Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Thu, 28 Apr 2022 10:13:36 -0500 Subject: [PATCH] bugfix(k8s): skip false error in logs correctly (#323) --- runtime/kubernetes/container.go | 14 ++------ runtime/kubernetes/container_test.go | 50 +++++++++++++++++++++++++++ runtime/kubernetes/kubernetes_test.go | 2 ++ 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/runtime/kubernetes/container.go b/runtime/kubernetes/container.go index 11428e3a..1b25b442 100644 --- a/runtime/kubernetes/container.go +++ b/runtime/kubernetes/container.go @@ -53,17 +53,9 @@ func (c *client) InspectContainer(ctx context.Context, ctn *pipeline.Container) // avoid a panic if the build ends without terminating all containers if cst.State.Terminated == nil { - for _, container := range pod.Spec.Containers { - if cst.Name != container.Name { - continue - } - - // steps that were not executed will still be "running" the pause image as expected. - if container.Image == pauseImage { - return nil - } - - break + // steps that were not executed will still be "running" the pause image as expected. + if cst.Image == pauseImage || cst.Image == image.Parse(pauseImage) { + return nil } return fmt.Errorf("expected container %s to be terminated, got %v", ctn.ID, cst.State) diff --git a/runtime/kubernetes/container_test.go b/runtime/kubernetes/container_test.go index 2efc8043..d71d3753 100644 --- a/runtime/kubernetes/container_test.go +++ b/runtime/kubernetes/container_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/go-vela/types/pipeline" + "github.com/go-vela/worker/internal/image" velav1alpha1 "github.com/go-vela/worker/runtime/kubernetes/apis/vela/v1alpha1" v1 "k8s.io/api/core/v1" @@ -52,6 +53,53 @@ func TestKubernetes_InspectContainer(t *testing.T) { State: v1.ContainerState{ Running: &v1.ContainerStateRunning{}, }, + Image: _container.Image, + }, + }, + }, + }, + container: _container, + }, + { + name: "build stops before container execution with raw pauseImage", + failure: false, + pod: &v1.Pod{ + ObjectMeta: _pod.ObjectMeta, + TypeMeta: _pod.TypeMeta, + Spec: _pod.Spec, + Status: v1.PodStatus{ + Phase: v1.PodRunning, + ContainerStatuses: []v1.ContainerStatus{ + { + Name: "step-github-octocat-1-clone", + State: v1.ContainerState{ + Running: &v1.ContainerStateRunning{}, + }, + // container not patched yet with correct image + Image: pauseImage, + }, + }, + }, + }, + container: _container, + }, + { + name: "build stops before container execution with canonical pauseImage", + failure: false, + pod: &v1.Pod{ + ObjectMeta: _pod.ObjectMeta, + TypeMeta: _pod.TypeMeta, + Spec: _pod.Spec, + Status: v1.PodStatus{ + Phase: v1.PodRunning, + ContainerStatuses: []v1.ContainerStatus{ + { + Name: "step-github-octocat-1-clone", + State: v1.ContainerState{ + Running: &v1.ContainerStateRunning{}, + }, + // container not patched yet with correct image + Image: image.Parse(pauseImage), }, }, }, @@ -411,6 +459,7 @@ func TestKubernetes_WaitContainer(t *testing.T) { ExitCode: 0, }, }, + Image: "alpine:latest", }, { Name: "step-github-octocat-1-clone", @@ -420,6 +469,7 @@ func TestKubernetes_WaitContainer(t *testing.T) { ExitCode: 0, }, }, + Image: "target/vela-git:v0.4.0", }, }, }, diff --git a/runtime/kubernetes/kubernetes_test.go b/runtime/kubernetes/kubernetes_test.go index 39da9c63..364a90b0 100644 --- a/runtime/kubernetes/kubernetes_test.go +++ b/runtime/kubernetes/kubernetes_test.go @@ -106,6 +106,7 @@ var ( ExitCode: 0, }, }, + Image: "target/vela-git:v0.4.0", }, { Name: "step-github-octocat-1-echo", @@ -115,6 +116,7 @@ var ( ExitCode: 0, }, }, + Image: "alpine:latest", }, }, },