From af933fbcb2038627396af2efef0ed8ed6b6d384d Mon Sep 17 00:00:00 2001 From: Alejandro Romero Date: Fri, 24 May 2024 17:10:01 +0000 Subject: [PATCH] Update GetNameAndNamespace Parameters --- pkg/remote/resolution/resolver.go | 6 +++++- pkg/remoteresolution/remote/resolution/resolver.go | 9 ++++++++- pkg/remoteresolution/remote/resolution/resolver_test.go | 9 +++++++++ pkg/resolution/resource/name.go | 7 +++++-- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/pkg/remote/resolution/resolver.go b/pkg/remote/resolution/resolver.go index eb56ab04d2f..f858d1259e0 100644 --- a/pkg/remote/resolution/resolver.go +++ b/pkg/remote/resolution/resolver.go @@ -19,6 +19,7 @@ import ( "fmt" v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + "github.com/tektoncd/pipeline/pkg/apis/resolution/v1beta1" "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/scheme" "github.com/tektoncd/pipeline/pkg/remote" resolutioncommon "github.com/tektoncd/pipeline/pkg/resolution/common" @@ -73,7 +74,10 @@ func (resolver *Resolver) List(_ context.Context) ([]remote.ResolvedObject, erro } func buildRequest(resolverName string, owner kmeta.OwnerRefable, name string, namespace string, params v1.Params) (*resolutionRequest, error) { - name, namespace, err := remoteresource.GetNameAndNamespace(resolverName, owner, name, namespace, params) + rr := &v1beta1.ResolutionRequestSpec{ + Params: params, + } + name, namespace, err := remoteresource.GetNameAndNamespace(resolverName, owner, name, namespace, rr) if err != nil { return nil, err } diff --git a/pkg/remoteresolution/remote/resolution/resolver.go b/pkg/remoteresolution/remote/resolution/resolver.go index 85836addbeb..d3500ae6396 100644 --- a/pkg/remoteresolution/remote/resolution/resolver.go +++ b/pkg/remoteresolution/remote/resolution/resolver.go @@ -18,6 +18,7 @@ import ( "fmt" v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + "github.com/tektoncd/pipeline/pkg/apis/resolution/v1beta1" "github.com/tektoncd/pipeline/pkg/remote" resolution "github.com/tektoncd/pipeline/pkg/remote/resolution" remoteresource "github.com/tektoncd/pipeline/pkg/remoteresolution/resource" @@ -69,15 +70,21 @@ func (resolver *Resolver) List(_ context.Context) ([]remote.ResolvedObject, erro func buildRequest(resolverName string, owner kmeta.OwnerRefable, resolverPayload *remoteresource.ResolverPayload) (*resolutionRequest, error) { var name string var namespace string + var url string var params v1.Params if resolverPayload != nil { name = resolverPayload.Name namespace = resolverPayload.Namespace if resolverPayload.ResolutionSpec != nil { params = resolverPayload.ResolutionSpec.Params + url = resolverPayload.ResolutionSpec.URL } } - name, namespace, err := resource.GetNameAndNamespace(resolverName, owner, name, namespace, params) + rr := &v1beta1.ResolutionRequestSpec{ + Params: params, + URL: url, + } + name, namespace, err := resource.GetNameAndNamespace(resolverName, owner, name, namespace, rr) if err != nil { return nil, err } diff --git a/pkg/remoteresolution/remote/resolution/resolver_test.go b/pkg/remoteresolution/remote/resolution/resolver_test.go index e93f43ef4f2..cf97976c6cd 100644 --- a/pkg/remoteresolution/remote/resolution/resolver_test.go +++ b/pkg/remoteresolution/remote/resolution/resolver_test.go @@ -20,6 +20,8 @@ import ( "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + resv1beta1 "github.com/tektoncd/pipeline/pkg/apis/resolution/v1beta1" + "github.com/tektoncd/pipeline/pkg/remote" "github.com/tektoncd/pipeline/pkg/remote/resolution" remoteresource "github.com/tektoncd/pipeline/pkg/remoteresolution/resource" @@ -142,12 +144,18 @@ func TestBuildRequestV2(t *testing.T) { name string targetName string targetNamespace string + url string }{{ name: "just owner", }, { name: "with target name and namespace", targetName: "some-object", targetNamespace: "some-ns", + }, { + name: "with target name, namespace, and url", + targetName: "some-object", + targetNamespace: "some-ns", + url: "scheme://value", }} { t.Run(tc.name, func(t *testing.T) { owner := &v1beta1.PipelineRun{ @@ -158,6 +166,7 @@ func TestBuildRequestV2(t *testing.T) { } rr := &remoteresource.ResolverPayload{Name: tc.targetName, Namespace: tc.targetNamespace} + rr.ResolutionSpec = &resv1beta1.ResolutionRequestSpec{URL: tc.url} req, err := buildRequest("git", owner, rr) if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/pkg/resolution/resource/name.go b/pkg/resolution/resource/name.go index 4e931978695..8557d824622 100644 --- a/pkg/resolution/resource/name.go +++ b/pkg/resolution/resource/name.go @@ -50,7 +50,10 @@ func GenerateDeterministicName(prefix, base string, params v1.Params) (string, e return GenerateDeterministicNameFromSpec(prefix, base, &v1beta1.ResolutionRequestSpec{Params: params}) } -func GetNameAndNamespace(resolverName string, owner kmeta.OwnerRefable, name string, namespace string, params v1.Params) (string, string, error) { +// GetNameAndNamespace determines the name and namespace for a resource request. +// It prioritizes explicit values, falling back to the owning object and "default" namespace. +// If needed, it generates a deterministic name to prevent duplicate requests within a context. +func GetNameAndNamespace(resolverName string, owner kmeta.OwnerRefable, name string, namespace string, req *v1beta1.ResolutionRequestSpec) (string, string, error) { if name == "" { name = owner.GetObjectMeta().GetName() namespace = owner.GetObjectMeta().GetNamespace() @@ -62,7 +65,7 @@ func GetNameAndNamespace(resolverName string, owner kmeta.OwnerRefable, name str // prevents multiple requests being issued for the same // pipelinerun's pipelineRef or taskrun's taskRef. remoteResourceBaseName := namespace + "/" + name - name, err := GenerateDeterministicNameFromSpec(resolverName, remoteResourceBaseName, &v1beta1.ResolutionRequestSpec{Params: params}) + name, err := GenerateDeterministicNameFromSpec(resolverName, remoteResourceBaseName, req) if err != nil { return "", "", fmt.Errorf("error generating name for taskrun %s/%s: %w", namespace, name, err) }