Skip to content

Commit

Permalink
Add conversion for remote tasks and pipelines to support v1
Browse files Browse the repository at this point in the history
This commit adds conversion for remote tasks and pipelines after they
are resolved by resolvers to support v1 types. Before this commit v1
tasks and pipelines are not supported in remote resolution since we use
v1beta1 as the storage version and in reconciler we will convert v1 crd
into v1beta. But this conversion is missing for remote resources. After
we change the storage version to v1 we should update the code to support
v1beta.

Signed-off-by: Yongxuan Zhang [email protected]
  • Loading branch information
Yongxuanzhang committed Mar 1, 2023
1 parent c53681d commit b67c9e7
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 51 deletions.
17 changes: 15 additions & 2 deletions pkg/reconciler/pipelinerun/resources/pipelineref.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/google/go-containerregistry/pkg/authn/k8schain"
"github.com/tektoncd/pipeline/pkg/apis/config"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -176,8 +177,20 @@ func resolvePipeline(ctx context.Context, resolver remote.Resolver, name string)
// PipelineObject or if there is an error validating or upgrading an
// older PipelineObject into its v1beta1 equivalent.
func readRuntimeObjectAsPipeline(ctx context.Context, obj runtime.Object) (v1beta1.PipelineObject, error) {
if pipeline, ok := obj.(v1beta1.PipelineObject); ok {
return pipeline, nil
switch obj := obj.(type) {
case v1beta1.PipelineObject:
return obj, nil
case *v1.Pipeline:
t := &v1beta1.Pipeline{
TypeMeta: metav1.TypeMeta{
Kind: "Pipeline",
APIVersion: "tekton.dev/v1beta1",
},
}
if err := t.ConvertFrom(ctx, obj); err != nil {
return nil, err
}
return t, nil
}

return nil, errors.New("resource is not a pipeline")
Expand Down
63 changes: 40 additions & 23 deletions pkg/reconciler/pipelinerun/resources/pipelineref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,33 +292,50 @@ func TestGetPipelineFunc_RemoteResolution(t *testing.T) {
ctx = config.ToContext(ctx, cfg)
pipeline := parse.MustParseV1beta1Pipeline(t, pipelineYAMLString)
pipelineRef := &v1beta1.PipelineRef{ResolverRef: v1beta1.ResolverRef{Resolver: "git"}}
pipelineYAML := strings.Join([]string{
"kind: Pipeline",
"apiVersion: tekton.dev/v1beta1",
pipelineYAMLString,
}, "\n")

resolved := test.NewResolvedResource([]byte(pipelineYAML), nil, sampleConfigSource.DeepCopy(), nil)
requester := test.NewRequester(resolved, nil)
fn := resources.GetPipelineFunc(ctx, nil, nil, requester, &v1beta1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{Namespace: "default"},
Spec: v1beta1.PipelineRunSpec{
PipelineRef: pipelineRef,
ServiceAccountName: "default",
},
})
testcases := []struct {
name string
pipelineYAML string
}{{
name: "v1beta1 pipeline",
pipelineYAML: strings.Join([]string{
"kind: Pipeline",
"apiVersion: tekton.dev/v1beta1",
pipelineYAMLString,
}, "\n"),
}, {
name: "v1 pipeline",
pipelineYAML: strings.Join([]string{
"kind: Pipeline",
"apiVersion: tekton.dev/v1",
pipelineYAMLString,
}, "\n"),
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
resolved := test.NewResolvedResource([]byte(tc.pipelineYAML), nil, sampleConfigSource.DeepCopy(), nil)
requester := test.NewRequester(resolved, nil)
fn := resources.GetPipelineFunc(ctx, nil, nil, requester, &v1beta1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{Namespace: "default"},
Spec: v1beta1.PipelineRunSpec{
PipelineRef: pipelineRef,
ServiceAccountName: "default",
},
})

resolvedPipeline, resolvedConfigSource, err := fn(ctx, pipelineRef.Name)
if err != nil {
t.Fatalf("failed to call pipelinefn: %s", err.Error())
}
resolvedPipeline, resolvedConfigSource, err := fn(ctx, pipelineRef.Name)
if err != nil {
t.Fatalf("failed to call pipelinefn: %s", err.Error())
}

if diff := cmp.Diff(pipeline, resolvedPipeline); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(pipeline, resolvedPipeline); diff != "" {
t.Error(diff)
}

if d := cmp.Diff(sampleConfigSource, resolvedConfigSource); d != "" {
t.Errorf("configsource did not match: %s", diff.PrintWantGot(d))
if d := cmp.Diff(sampleConfigSource, resolvedConfigSource); d != "" {
t.Errorf("configsource did not match: %s", diff.PrintWantGot(d))
}
})
}
}

Expand Down
17 changes: 15 additions & 2 deletions pkg/reconciler/taskrun/resources/taskref.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/google/go-containerregistry/pkg/authn/k8schain"
"github.com/tektoncd/pipeline/pkg/apis/config"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -194,8 +195,20 @@ func resolveTask(ctx context.Context, resolver remote.Resolver, name string, kin
// TaskObject or if there is an error validating or upgrading an
// older TaskObject into its v1beta1 equivalent.
func readRuntimeObjectAsTask(ctx context.Context, obj runtime.Object) (v1beta1.TaskObject, error) {
if task, ok := obj.(v1beta1.TaskObject); ok {
return task, nil
switch obj := obj.(type) {
case v1beta1.TaskObject:
return obj, nil
case *v1.Task:
t := &v1beta1.Task{
TypeMeta: metav1.TypeMeta{
Kind: "Task",
APIVersion: "tekton.dev/v1beta1",
},
}
if err := t.ConvertFrom(ctx, obj); err != nil {
return nil, err
}
return t, nil
}
return nil, errors.New("resource is not a task")
}
Expand Down
66 changes: 42 additions & 24 deletions pkg/reconciler/taskrun/resources/taskref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,33 +537,51 @@ func TestGetTaskFunc_RemoteResolution(t *testing.T) {
ctx = config.ToContext(ctx, cfg)
task := parse.MustParseV1beta1Task(t, taskYAMLString)
taskRef := &v1beta1.TaskRef{ResolverRef: v1beta1.ResolverRef{Resolver: "git"}}
taskYAML := strings.Join([]string{
"kind: Task",
"apiVersion: tekton.dev/v1beta1",
taskYAMLString,
}, "\n")
resolved := test.NewResolvedResource([]byte(taskYAML), nil, sampleConfigSource.DeepCopy(), nil)
requester := test.NewRequester(resolved, nil)
tr := &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{Namespace: "default"},
Spec: v1beta1.TaskRunSpec{
TaskRef: taskRef,
ServiceAccountName: "default",
},
}
fn := resources.GetTaskFunc(ctx, nil, nil, requester, tr, tr.Spec.TaskRef, "", "default", "default")

resolvedTask, resolvedConfigSource, err := fn(ctx, taskRef.Name)
if err != nil {
t.Fatalf("failed to call pipelinefn: %s", err.Error())
}
testcases := []struct {
name string
taskYAML string
}{{
name: "v1beta1 task",
taskYAML: strings.Join([]string{
"kind: Task",
"apiVersion: tekton.dev/v1beta1",
taskYAMLString,
}, "\n"),
}, {
name: "v1 task",
taskYAML: strings.Join([]string{
"kind: Task",
"apiVersion: tekton.dev/v1",
taskYAMLString,
}, "\n"),
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
resolved := test.NewResolvedResource([]byte(tc.taskYAML), nil, sampleConfigSource.DeepCopy(), nil)
requester := test.NewRequester(resolved, nil)
tr := &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{Namespace: "default"},
Spec: v1beta1.TaskRunSpec{
TaskRef: taskRef,
ServiceAccountName: "default",
},
}
fn := resources.GetTaskFunc(ctx, nil, nil, requester, tr, tr.Spec.TaskRef, "", "default", "default")

if d := cmp.Diff(sampleConfigSource, resolvedConfigSource); d != "" {
t.Errorf("configSources did not match: %s", diff.PrintWantGot(d))
}
resolvedTask, resolvedConfigSource, err := fn(ctx, taskRef.Name)
if err != nil {
t.Fatalf("failed to call pipelinefn: %s", err.Error())
}

if d := cmp.Diff(task, resolvedTask); d != "" {
t.Errorf("resolvedTask did not match: %s", diff.PrintWantGot(d))
if d := cmp.Diff(sampleConfigSource, resolvedConfigSource); d != "" {
t.Errorf("configSources did not match: %s", diff.PrintWantGot(d))
}

if d := cmp.Diff(task, resolvedTask); d != "" {
t.Errorf("resolvedTask did not match: %s", diff.PrintWantGot(d))
}
})
}
}

Expand Down

0 comments on commit b67c9e7

Please sign in to comment.