Skip to content

Commit

Permalink
bugfix(k8s): skip false error in logs correctly
Browse files Browse the repository at this point in the history
We have to use the canonical pause image or it won't match.

Also add a helpful note/comment.
  • Loading branch information
cognifloyd committed Apr 25, 2022
1 parent b8cbb7a commit 1304e2f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
18 changes: 7 additions & 11 deletions runtime/kubernetes/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions runtime/kubernetes/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
},
},
},
Expand Down Expand Up @@ -406,6 +454,7 @@ func TestKubernetes_WaitContainer(t *testing.T) {
ExitCode: 0,
},
},
Image: "alpine:latest",
},
{
Name: "step-github-octocat-1-clone",
Expand All @@ -415,6 +464,7 @@ func TestKubernetes_WaitContainer(t *testing.T) {
ExitCode: 0,
},
},
Image: "target/vela-git:v0.4.0",
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions runtime/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ var (
ExitCode: 0,
},
},
Image: "target/vela-git:v0.4.0",
},
{
Name: "step-github-octocat-1-echo",
Expand All @@ -112,6 +113,7 @@ var (
ExitCode: 0,
},
},
Image: "alpine:latest",
},
},
},
Expand Down

0 comments on commit 1304e2f

Please sign in to comment.