From 607eb3d88643ae9136e851af965808c88b263a72 Mon Sep 17 00:00:00 2001 From: gabemontero Date: Wed, 10 Jan 2024 11:41:46 -0500 Subject: [PATCH] allow pipeline runs whose task/custom runs have been deleted still timeout Back with PR 5134 Tekton started using the cancel function for cleaning up underlying TaskRuns of PipelineRuns that are timing out, though separate timeout functions were used (presumably in case the timeout behavior needed to be tweaked from the cancel behavior later on). Then, in PR 5288, improvements to the baseline cancel function were made to still complete cancel processing of a PipelineRun if the underlying TaskRuns were deleted. However, that same accomodation was not made for the timeout path. This change addresses that, but still keeps the timeout codepaths separate from the 'base' cancel codepaths. --- pkg/reconciler/pipelinerun/timeout.go | 14 +++++++++++++- pkg/reconciler/pipelinerun/timeout_test.go | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pkg/reconciler/pipelinerun/timeout.go b/pkg/reconciler/pipelinerun/timeout.go index 32a1da8ae7d..8ae29a62d30 100644 --- a/pkg/reconciler/pipelinerun/timeout.go +++ b/pkg/reconciler/pipelinerun/timeout.go @@ -27,6 +27,7 @@ import ( "go.uber.org/zap" "gomodules.xyz/jsonpatch/v2" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/sets" @@ -92,6 +93,17 @@ func timeoutPipelineRun(ctx context.Context, logger *zap.SugaredLogger, pr *v1.P func timeoutCustomRun(ctx context.Context, customRunName string, namespace string, clientSet clientset.Interface) error { _, err := clientSet.TektonV1beta1().CustomRuns(namespace).Patch(ctx, customRunName, types.JSONPatchType, timeoutCustomRunPatchBytes, metav1.PatchOptions{}, "") + if errors.IsNotFound(err) { + return nil + } + return err +} + +func timeoutTaskRun(ctx context.Context, taskRunName string, namespace string, clientSet clientset.Interface) error { + _, err := clientSet.TektonV1().TaskRuns(namespace).Patch(ctx, taskRunName, types.JSONPatchType, timeoutTaskRunPatchBytes, metav1.PatchOptions{}, "") + if errors.IsNotFound(err) { + return nil + } return err } @@ -112,7 +124,7 @@ func timeoutPipelineTasksForTaskNames(ctx context.Context, logger *zap.SugaredLo for _, taskRunName := range trNames { logger.Infof("patching TaskRun %s for timeout", taskRunName) - if _, err := clientSet.TektonV1().TaskRuns(pr.Namespace).Patch(ctx, taskRunName, types.JSONPatchType, timeoutTaskRunPatchBytes, metav1.PatchOptions{}, ""); err != nil { + if err := timeoutTaskRun(ctx, taskRunName, pr.Namespace, clientSet); err != nil { errs = append(errs, fmt.Errorf("failed to patch TaskRun `%s` with timeout: %w", taskRunName, err).Error()) continue } diff --git a/pkg/reconciler/pipelinerun/timeout_test.go b/pkg/reconciler/pipelinerun/timeout_test.go index 0b65cf08f35..a369aeee268 100644 --- a/pkg/reconciler/pipelinerun/timeout_test.go +++ b/pkg/reconciler/pipelinerun/timeout_test.go @@ -59,6 +59,23 @@ func TestTimeoutPipelineRun(t *testing.T) { taskRuns: []*v1.TaskRun{ {ObjectMeta: metav1.ObjectMeta{Name: "t1"}}, }, + }, { + name: "multiple-runs-missing", + pipelineRun: &v1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{Name: "test-pipeline-run-timedout"}, + Spec: v1.PipelineRunSpec{}, + Status: v1.PipelineRunStatus{PipelineRunStatusFields: v1.PipelineRunStatusFields{ + ChildReferences: []v1.ChildStatusReference{{ + TypeMeta: runtime.TypeMeta{Kind: taskRun}, + Name: "t1", + PipelineTaskName: "task-1", + }, { + TypeMeta: runtime.TypeMeta{Kind: customRun}, + Name: "t2", + PipelineTaskName: "task-2", + }}, + }}, + }, }, { name: "multiple-taskruns", pipelineRun: &v1.PipelineRun{