Skip to content

Commit

Permalink
V1: Add conversion for Pipeline.Resources
Browse files Browse the repository at this point in the history
This commit adds support for Pipeline.Resources when converting between v1beta1 and v1
versions of Pipelines. This will allow us to release v1 Pipeline in a backwards compatible way,
by ensuring that v1beta1 Pipelines with Resources will have the Resources serialized into
annotations on the v1 Pipeline on conversion.
  • Loading branch information
JeromeJu committed Aug 19, 2022
1 parent 12681b6 commit e15fde8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
28 changes: 27 additions & 1 deletion pkg/apis/pipeline/v1beta1/pipeline_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 (p *Pipeline) ConvertTo(ctx context.Context, to apis.Convertible) error {
switch sink := to.(type) {
case *v1.Pipeline:
sink.ObjectMeta = p.ObjectMeta
if err := serializePipelineResources(&sink.ObjectMeta, &p.Spec); err != nil {
return err
}
return p.Spec.ConvertTo(ctx, &sink.Spec)
default:
return fmt.Errorf("unknown version, got: %T", sink)
Expand Down Expand Up @@ -79,7 +84,6 @@ func (ps *PipelineSpec) ConvertTo(ctx context.Context, sink *v1.PipelineSpec) er
}
sink.Finally = append(sink.Finally, new)
}
// TODO: Handle Resources in #4546
return nil
}

Expand All @@ -88,6 +92,9 @@ func (p *Pipeline) ConvertFrom(ctx context.Context, from apis.Convertible) error
switch source := from.(type) {
case *v1.Pipeline:
p.ObjectMeta = source.ObjectMeta
if err := deserializePipelineResources(&p.ObjectMeta, &p.Spec); err != nil {
return err
}
return p.Spec.ConvertFrom(ctx, &source.Spec)
default:
return fmt.Errorf("unknown version, got: %T", p)
Expand Down Expand Up @@ -271,3 +278,22 @@ func (pr *PipelineResult) convertFrom(ctx context.Context, source v1.PipelineRes
newValue.convertFrom(ctx, source.Value)
pr.Value = newValue
}

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

func deserializePipelineResources(meta *metav1.ObjectMeta, spec *PipelineSpec) error {
resources := &[]PipelineDeclaredResource{}
err := version.DeserializeFromMetadata(meta, resources, resourcesAnnotationKey)
if err != nil {
return err
}
if len(*resources) != 0 {
spec.Resources = *resources
}
return nil
}
31 changes: 24 additions & 7 deletions pkg/apis/pipeline/v1beta1/pipeline_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func TestPipelineConversion(t *testing.T) {
}
}

// TODO handle resources and bundles in #4546
func TestPipelineConversionFromDeprecated(t *testing.T) {
versions := []apis.Convertible{&v1.Pipeline{}}
tests := []struct {
Expand All @@ -185,18 +184,36 @@ func TestPipelineConversionFromDeprecated(t *testing.T) {
Namespace: "bar",
},
Spec: v1beta1.PipelineSpec{
Resources: []v1beta1.PipelineDeclaredResource{{
Name: "pipeline resource",
Type: v1beta1.PipelineResourceTypeGit,
Optional: true,
}},
Resources: []v1beta1.PipelineDeclaredResource{
{
Name: "1st pipeline resource",
Type: v1beta1.PipelineResourceTypeGit,
Optional: true,
}, {
Name: "2nd pipeline resource",
Type: v1beta1.PipelineResourceTypeGit,
Optional: false,
},
},
}},
want: &v1beta1.Pipeline{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.PipelineSpec{},
Spec: v1beta1.PipelineSpec{
Resources: []v1beta1.PipelineDeclaredResource{
{
Name: "1st pipeline resource",
Type: v1beta1.PipelineResourceTypeGit,
Optional: true,
}, {
Name: "2nd pipeline resource",
Type: v1beta1.PipelineResourceTypeGit,
Optional: false,
},
},
},
},
}}

Expand Down
25 changes: 25 additions & 0 deletions pkg/apis/version/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,28 @@ func TestSerializationRoundTrip(t *testing.T) {
t.Errorf("Unexpected diff after serialization/deserialization round trip: %s", d)
}
}

func TestSliceSerializationRoundTrip(t *testing.T) {
meta := metav1.ObjectMeta{}
source := []testStruct{{Field: "foo"}, {Field: "bar"}}
key := "my-key"
err := version.SerializeToMetadata(&meta, source, key)
if err != nil {
t.Fatalf("Serialization error: %s", err)
}

sink := []testStruct{}
err = version.DeserializeFromMetadata(&meta, &sink, key)
if err != nil {
t.Fatalf("Deserialization error: %s", err)
}

_, ok := meta.Annotations[key]
if ok {
t.Errorf("Expected key %s not to be present in annotations but it was", key)
}

if d := cmp.Diff(source, sink); d != "" {
t.Errorf("Unexpected diff after serialization/deserialization round trip: %s", d)
}
}

0 comments on commit e15fde8

Please sign in to comment.