Skip to content

Commit

Permalink
V1: Add conversion for TaskRun.Resources
Browse files Browse the repository at this point in the history
This commit adds support for TaskRun.Resources when converting between
v1beta1 and v1 versions of Tasks. This allows us to release v1 TaskRun
in a backwards compatible way by ensuring that v1beta1 TaskRuns with
Resources serialized into annotations on the v1 TaskRun on conversion.
  • Loading branch information
JeromeJu authored and tekton-robot committed Aug 25, 2022
1 parent 5188483 commit a91f0a5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
27 changes: 27 additions & 0 deletions pkg/apis/pipeline/v1beta1/taskrun_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"

v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/version"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)

Expand All @@ -34,6 +36,9 @@ func (tr *TaskRun) ConvertTo(ctx context.Context, to apis.Convertible) error {
switch sink := to.(type) {
case *v1.TaskRun:
sink.ObjectMeta = tr.ObjectMeta
if err := serializeTaskRunResources(&sink.ObjectMeta, &tr.Spec); err != nil {
return err
}
return tr.Spec.ConvertTo(ctx, &sink.Spec)
default:
return fmt.Errorf("unknown version, got: %T", sink)
Expand Down Expand Up @@ -98,6 +103,9 @@ func (tr *TaskRun) ConvertFrom(ctx context.Context, from apis.Convertible) error
switch source := from.(type) {
case *v1.TaskRun:
tr.ObjectMeta = source.ObjectMeta
if err := deserializeTaskRunResources(&tr.ObjectMeta, &tr.Spec); err != nil {
return err
}
return tr.Spec.ConvertFrom(ctx, &source.Spec)
default:
return fmt.Errorf("unknown version, got: %T", tr)
Expand Down Expand Up @@ -184,3 +192,22 @@ func (trso *TaskRunSidecarOverride) convertFrom(ctx context.Context, source v1.T
trso.Name = source.Name
trso.Resources = source.Resources
}

func serializeTaskRunResources(meta *metav1.ObjectMeta, spec *TaskRunSpec) error {
if spec.Resources == nil {
return nil
}
return version.SerializeToMetadata(meta, spec.Resources, resourcesAnnotationKey)
}

func deserializeTaskRunResources(meta *metav1.ObjectMeta, spec *TaskRunSpec) error {
resources := &TaskRunResources{}
err := version.DeserializeFromMetadata(meta, resources, resourcesAnnotationKey)
if err != nil {
return err
}
if resources.Inputs != nil || resources.Outputs != nil {
spec.Resources = resources
}
return nil
}
31 changes: 26 additions & 5 deletions pkg/apis/pipeline/v1beta1/taskrun_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ func TestTaskrunConversion(t *testing.T) {
}

func TestTaskRunConversionFromDeprecated(t *testing.T) {
// TODO(#4546): We're just dropping Resources when converting from
// v1beta1 to v1. Before moving the stored version to v1, we should
// come up with a better strategy
versions := []apis.Convertible{&v1.TaskRun{}}
tests := []struct {
name string
Expand Down Expand Up @@ -229,7 +226,19 @@ func TestTaskRunConversionFromDeprecated(t *testing.T) {
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{},
Spec: v1beta1.TaskRunSpec{
Resources: &v1beta1.TaskRunResources{
Inputs: []v1beta1.TaskResourceBinding{{
PipelineResourceBinding: v1beta1.PipelineResourceBinding{
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "the-git-with-branch",
},
Name: "gitspace",
},
Paths: []string{"test-path"},
}},
},
},
},
}, {
name: "output resources",
Expand Down Expand Up @@ -257,7 +266,19 @@ func TestTaskRunConversionFromDeprecated(t *testing.T) {
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{},
Spec: v1beta1.TaskRunSpec{
Resources: &v1beta1.TaskRunResources{
Outputs: []v1beta1.TaskResourceBinding{{
PipelineResourceBinding: v1beta1.PipelineResourceBinding{
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "the-git-with-branch",
},
Name: "gitspace",
},
Paths: []string{"test-path"},
}},
},
},
},
}}
for _, test := range tests {
Expand Down

0 comments on commit a91f0a5

Please sign in to comment.