From 1304e2fffc479dabe9964de0e66fa54bd34949db Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Mon, 25 Apr 2022 15:33:49 -0500 Subject: [PATCH] bugfix(k8s): skip false error in logs correctly We have to use the canonical pause image or it won't match. Also add a helpful note/comment. --- runtime/kubernetes/container.go | 18 ++++------ runtime/kubernetes/container_test.go | 50 +++++++++++++++++++++++++++ runtime/kubernetes/kubernetes_test.go | 2 ++ 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/runtime/kubernetes/container.go b/runtime/kubernetes/container.go index f473aaf20..ab050d36c 100644 --- a/runtime/kubernetes/container.go +++ b/runtime/kubernetes/container.go @@ -47,17 +47,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) @@ -358,6 +350,10 @@ func (p *podTracker) inspectContainerStatuses(pod *v1.Pod) { continue } + // cst.RestartCount is 1 at exit due to switch from kubernetes/pause to actual/target image. + // cst.LastTerminationState has details about the kubernetes/pause's exit. + // cst.State has details about the target image's exit. + // check if the container is in a terminated state // // https://pkg.go.dev/k8s.io/api/core/v1?tab=doc#ContainerState diff --git a/runtime/kubernetes/container_test.go b/runtime/kubernetes/container_test.go index 7ceade35d..743696586 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" "github.com/sirupsen/logrus" @@ -51,6 +52,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), }, }, }, @@ -406,6 +454,7 @@ func TestKubernetes_WaitContainer(t *testing.T) { ExitCode: 0, }, }, + Image: "alpine:latest", }, { Name: "step-github-octocat-1-clone", @@ -415,6 +464,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 f3ce29f02..60e2a5edc 100644 --- a/runtime/kubernetes/kubernetes_test.go +++ b/runtime/kubernetes/kubernetes_test.go @@ -103,6 +103,7 @@ var ( ExitCode: 0, }, }, + Image: "target/vela-git:v0.4.0", }, { Name: "step-github-octocat-1-echo", @@ -112,6 +113,7 @@ var ( ExitCode: 0, }, }, + Image: "alpine:latest", }, }, },