Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1: Add conversion for Pipeline.Resources #5331

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}