tektoncd/pipeline

Refactor: extract shared cleanup-after-resolve helper from taskref.go and pipelineref.go

开放

#9,493 创建于 2026年3月4日

 (2 条评论) (0 个反应) (1 位负责人)Go (1,943 个派生)auto 404
good first issuehelp wantedkind/cleanup

仓库指标

星标
 (9,013 个星标)
PR 合并指标
 (平均合并 9天 3小时) (30 天内合并 137 个 PR)

描述

Summary

There are 6 nearly identical code blocks across taskref.go and pipelineref.go that perform the same post-resolve cleanup pattern:

  1. Clear ObjectMeta.OwnerReferences
  2. Optionally call SetDefaults(ctx)
  3. Verify the resource via trustedresources.VerifyResource()
  4. Dry-run validate via apiserver.DryRunValidate()
  5. Restore ObjectMeta on the mutated object

Each block has a // FIXME: extract this in a function comment.

Locations

pkg/reconciler/taskrun/resources/taskref.go — 4 occurrences:

  • Line 236: resolveStepAction — v1beta1.StepAction case
  • Line 249: resolveStepAction — v1alpha1.StepAction case
  • Line 287: readRuntimeObjectAsTask — v1beta1.Task case
  • Line 315: readRuntimeObjectAsTask — v1.Task case

pkg/reconciler/pipelinerun/resources/pipelineref.go — 2 occurrences:

  • Line 155: readRuntimeObjectAsPipeline — v1beta1.Pipeline case
  • Line 180: readRuntimeObjectAsPipeline — v1.Pipeline case

What to do

Extract a shared helper function, something like:

// cleanupAndValidateResolvedObject clears OwnerReferences, sets defaults,
// verifies trusted resources, and dry-run validates the resolved object.
func cleanupAndValidateResolvedObject(ctx context.Context, namespace string, obj client.Object, ...) (runtime.Object, *trustedresources.VerificationResult, error) {
    obj.SetOwnerReferences(nil)
    obj.SetDefaults(ctx)
    vr := trustedresources.VerifyResource(ctx, obj, k8s, refSource, verificationPolicies)
    mutated, err := apiserver.DryRunValidate(ctx, namespace, obj, tekton)
    if err != nil {
        return nil, nil, err
    }
    // Restore ObjectMeta on the mutated object
    ...
    return mutated, &vr, nil
}

The helper could live in a shared package like pkg/reconciler/internal/resolution/ or alongside the existing files.

How to verify

  • go build ./... passes
  • go test ./pkg/reconciler/taskrun/resources/... ./pkg/reconciler/pipelinerun/resources/... passes
  • No behavior change — this is a pure refactor

/kind cleanup

贡献者指南