Skip to content

Commit

Permalink
V1: Add conversion for TaskRef.Bundle
Browse files Browse the repository at this point in the history
This commit adds support for TaskRef.Bundle when converting between
v1beta1 and v1 versions of TaskRef. This allows us to release v1 TaskRun
and PipelineRun in a backwards compatible way by ensuring that v1beta1
TaskRuns and PipelineRuns with Bundle serialized into annotations on the
v1 TaskRuns and PipelineRuns on conversion.
  • Loading branch information
JeromeJu committed Aug 25, 2022
1 parent a91f0a5 commit 49ad7a1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1beta1/taskref_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
)

const bundleAnnotationKey = "tekton.dev/v1beta1Bundle"

func (tr TaskRef) convertTo(ctx context.Context, sink *v1.TaskRef) {
sink.Name = tr.Name
sink.Kind = v1.TaskKind(tr.Kind)
sink.APIVersion = tr.APIVersion
// TODO: handle bundle in #4546
new := v1.ResolverRef{}
tr.ResolverRef.convertTo(ctx, &new)
sink.ResolverRef = new
Expand All @@ -20,7 +21,6 @@ func (tr *TaskRef) convertFrom(ctx context.Context, source v1.TaskRef) {
tr.Name = source.Name
tr.Kind = TaskKind(source.Kind)
tr.APIVersion = source.APIVersion
// TODO: handle bundle in #4546
new := ResolverRef{}
new.convertFrom(ctx, source.ResolverRef)
tr.ResolverRef = new
Expand Down
30 changes: 26 additions & 4 deletions pkg/apis/pipeline/v1beta1/taskrun_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ func (tr *TaskRun) ConvertTo(ctx context.Context, to apis.Convertible) error {
if err := serializeTaskRunResources(&sink.ObjectMeta, &tr.Spec); err != nil {
return err
}
return tr.Spec.ConvertTo(ctx, &sink.Spec)
return tr.Spec.ConvertTo(ctx, &sink.Spec, &sink.ObjectMeta)
default:
return fmt.Errorf("unknown version, got: %T", sink)
}
}

// ConvertTo implements apis.Convertible
func (trs *TaskRunSpec) ConvertTo(ctx context.Context, sink *v1.TaskRunSpec) error {
func (trs *TaskRunSpec) ConvertTo(ctx context.Context, sink *v1.TaskRunSpec, objectMeta *metav1.ObjectMeta) error {
if trs.Debug != nil {
sink.Debug = &v1.TaskRunDebug{}
trs.Debug.convertTo(ctx, sink.Debug)
Expand All @@ -59,6 +59,9 @@ func (trs *TaskRunSpec) ConvertTo(ctx context.Context, sink *v1.TaskRunSpec) err
}
sink.ServiceAccountName = trs.ServiceAccountName
if trs.TaskRef != nil {
if err := serializeBundle(objectMeta, trs.TaskRef); err != nil {
return err
}
sink.TaskRef = &v1.TaskRef{}
trs.TaskRef.convertTo(ctx, sink.TaskRef)
}
Expand Down Expand Up @@ -106,14 +109,14 @@ func (tr *TaskRun) ConvertFrom(ctx context.Context, from apis.Convertible) error
if err := deserializeTaskRunResources(&tr.ObjectMeta, &tr.Spec); err != nil {
return err
}
return tr.Spec.ConvertFrom(ctx, &source.Spec)
return tr.Spec.ConvertFrom(ctx, &source.Spec, &tr.ObjectMeta)
default:
return fmt.Errorf("unknown version, got: %T", tr)
}
}

// ConvertFrom implements apis.Convertible
func (trs *TaskRunSpec) ConvertFrom(ctx context.Context, source *v1.TaskRunSpec) error {
func (trs *TaskRunSpec) ConvertFrom(ctx context.Context, source *v1.TaskRunSpec, objectMeta *metav1.ObjectMeta) error {
if source.Debug != nil {
newDebug := TaskRunDebug{}
newDebug.convertFrom(ctx, *source.Debug)
Expand All @@ -130,6 +133,9 @@ func (trs *TaskRunSpec) ConvertFrom(ctx context.Context, source *v1.TaskRunSpec)
newTaskRef := TaskRef{}
newTaskRef.convertFrom(ctx, *source.TaskRef)
trs.TaskRef = &newTaskRef
if err := deserializeBundle(objectMeta, trs.TaskRef); err != nil {
return err
}
}
if source.TaskSpec != nil {
newTaskSpec := TaskSpec{}
Expand Down Expand Up @@ -209,5 +215,21 @@ func deserializeTaskRunResources(meta *metav1.ObjectMeta, spec *TaskRunSpec) err
if resources.Inputs != nil || resources.Outputs != nil {
spec.Resources = resources
}
}

func serializeBundle(meta *metav1.ObjectMeta, tr *TaskRef) error {
if tr.Bundle == "" {
return nil
}
return version.SerializeToMetadata(meta, tr.Bundle, bundleAnnotationKey)
}

func deserializeBundle(meta *metav1.ObjectMeta, tr *TaskRef) error {
bundle := ""
err := version.DeserializeFromMetadata(meta, &bundle, bundleAnnotationKey)
if err != nil {
return err
}
tr.Bundle = bundle
return nil
}
20 changes: 20 additions & 0 deletions pkg/apis/pipeline/v1beta1/taskrun_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,26 @@ func TestTaskRunConversionFromDeprecated(t *testing.T) {
},
},
},
}, {
name: "bundle",
in: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{Bundle: "test-bundle"},
},
},
want: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{Bundle: "test-bundle"},
},
},
}}
for _, test := range tests {
for _, version := range versions {
Expand Down

0 comments on commit 49ad7a1

Please sign in to comment.