Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude stopped injected sidecars from TaskRun status #7642

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/pod/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ func IsSidecarStatusRunning(tr *v1.TaskRun) bool {
// represents a step.
func IsContainerStep(name string) bool { return strings.HasPrefix(name, stepPrefix) }

// isContainerSidecar returns true if the container name indicates that it
// IsContainerSidecar returns true if the container name indicates that it
// represents a sidecar.
func isContainerSidecar(name string) bool { return strings.HasPrefix(name, sidecarPrefix) }
func IsContainerSidecar(name string) bool { return strings.HasPrefix(name, sidecarPrefix) }

// trimStepPrefix returns the container name, stripped of its step prefix.
func trimStepPrefix(name string) string { return strings.TrimPrefix(name, stepPrefix) }
Expand Down
2 changes: 1 addition & 1 deletion pkg/pod/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func MakeTaskRunStatus(ctx context.Context, logger *zap.SugaredLogger, tr v1.Tas
for _, s := range pod.Status.ContainerStatuses {
if IsContainerStep(s.Name) {
stepStatuses = append(stepStatuses, s)
} else if isContainerSidecar(s.Name) {
} else if IsContainerSidecar(s.Name) {
sidecarStatuses = append(sidecarStatuses, s)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ func isPodAdmissionFailed(err error) bool {
func updateStoppedSidecarStatus(pod *corev1.Pod, tr *v1.TaskRun) error {
tr.Status.Sidecars = []v1.SidecarState{}
for _, s := range pod.Status.ContainerStatuses {
if !podconvert.IsContainerStep(s.Name) {
if podconvert.IsContainerSidecar(s.Name) {
var sidecarState corev1.ContainerState
if s.LastTerminationState.Terminated != nil {
// Sidecar has successfully by terminated by nop image
Expand Down
45 changes: 41 additions & 4 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5368,20 +5368,36 @@ status:
}

func TestStopSidecars_WithInjectedSidecarsNoTaskSpecSidecars(t *testing.T) {
sidecarTask := &v1.Task{
ObjectMeta: objectMeta("test-task-injected-sidecar", "foo"),
Spec: v1.TaskSpec{
Steps: []v1.Step{simpleStep},
Sidecars: []v1.Sidecar{{
Name: "sidecar1",
Image: "image-id",
}},
},
}

taskRun := parse.MustParseV1TaskRun(t, `
metadata:
name: test-taskrun-injected-sidecars
namespace: foo
spec:
taskRef:
name: test-task
name: test-task-injected-sidecar
status:
podName: test-taskrun-injected-sidecars-pod
conditions:
- message: Build succeeded
reason: Build succeeded
status: "True"
type: Succeeded
sidecars:
- name: sidecar1
container: sidecar-sidecar1
running:
startedAt: "2000-01-01T01:01:01Z"
`)

pod := &corev1.Pod{
Expand All @@ -5395,6 +5411,10 @@ status:
Name: "step-do-something",
Image: "my-step-image",
},
{
Name: "sidecar1",
Image: "image-id",
},
{
Name: "injected-sidecar",
Image: "some-image",
Expand All @@ -5408,6 +5428,10 @@ status:
Name: "step-do-something",
State: corev1.ContainerState{Terminated: &corev1.ContainerStateTerminated{}},
},
{
Name: "sidecar-sidecar1",
State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{}},
},
{
Name: "injected-sidecar",
State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{}},
Expand All @@ -5419,7 +5443,7 @@ status:
d := test.Data{
Pods: []*corev1.Pod{pod},
TaskRuns: []*v1.TaskRun{taskRun},
Tasks: []*v1.Task{simpleTask},
Tasks: []*v1.Task{sidecarTask},
}

testAssets, cancel := getTaskRunController(t, d)
Expand All @@ -5439,14 +5463,27 @@ status:
t.Fatalf("error retrieving pod: %s", err)
}

if len(retrievedPod.Spec.Containers) != 2 {
if len(retrievedPod.Spec.Containers) != 3 {
t.Fatalf("expected pod with two containers")
}

// check that injected sidecar is replaced with nop image
if d := cmp.Diff(images.NopImage, retrievedPod.Spec.Containers[1].Image); d != "" {
if d := cmp.Diff(images.NopImage, retrievedPod.Spec.Containers[2].Image); d != "" {
t.Errorf("expected injected sidecar image to be replaced with nop image %s", diff.PrintWantGot(d))
}

// Get the updated TaskRun.
reconciledRun, err := clients.Pipeline.TektonV1().TaskRuns(taskRun.Namespace).Get(testAssets.Ctx, taskRun.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("Error getting updated TaskRun after reconcile: %v", err)
}

// Verify that the injected sidecar isn't present in the TaskRun's status.
for _, sc := range reconciledRun.Status.Sidecars {
if sc.Container == "injected-sidecar" {
t.Errorf("expected not to find injected-sidecar in TaskRun status, but found %v", sc)
}
}
}

func Test_validateTaskSpecRequestResources_ValidResources(t *testing.T) {
Expand Down