diff --git a/pkg/reconciler/pipelinerun/timeout.go b/pkg/reconciler/pipelinerun/timeout.go index eb76d8bda5d..826164f88b1 100644 --- a/pkg/reconciler/pipelinerun/timeout.go +++ b/pkg/reconciler/pipelinerun/timeout.go @@ -17,6 +17,7 @@ import ( "context" "encoding/json" "fmt" + "k8s.io/apimachinery/pkg/api/errors" "log" "strings" "time" @@ -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("cancelling 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 cancellation: %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{