Skip to content

Commit

Permalink
Add conversion for v1 TaskRun
Browse files Browse the repository at this point in the history
This commit adds conversion functions between v1beta1 and v1 TaskRun.
It does not handle fields deprecated in v1beta1 that will not be present in v1.
It implements ConvertTo and ConvertFrom for v1beta1 TaskRun, and leaves
these functions unimplemented for v1 TaskRun, since it is the highest known version.
  • Loading branch information
JeromeJu committed Aug 22, 2022
1 parent acf50a1 commit d36a9cb
Show file tree
Hide file tree
Showing 5 changed files with 437 additions and 6 deletions.
42 changes: 42 additions & 0 deletions pkg/apis/pipeline/v1/taskrun_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
"context"
"fmt"

"knative.dev/pkg/apis"
)

var _ apis.Convertible = (*TaskRun)(nil)

// ConvertTo implements apis.Convertible
func (tr *TaskRun) ConvertTo(ctx context.Context, sink apis.Convertible) error {
if apis.IsInDelete(ctx) {
return nil
}
return fmt.Errorf("v1 is the highest known version, got: %T", sink)
}

// ConvertFrom implements apis.Convertible
func (tr *TaskRun) ConvertFrom(ctx context.Context, source apis.Convertible) error {
if apis.IsInDelete(ctx) {
return nil
}
return fmt.Errorf("v1 is the highest known version, got: %T", source)
}
36 changes: 36 additions & 0 deletions pkg/apis/pipeline/v1/taskrun_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2022 The Tetkon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1_test

import (
"context"
"testing"

v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
)

func TestTaskRunConversionBadType(t *testing.T) {
good, bad := &v1.TaskRun{}, &v1.Task{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
t.Errorf("ConvertTo() = %#v, wanted error", bad)
}

if err := good.ConvertFrom(context.Background(), bad); err == nil {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}
78 changes: 74 additions & 4 deletions pkg/apis/pipeline/v1beta1/taskrun_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,93 @@ import (
"context"
"fmt"

v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"knative.dev/pkg/apis"
)

var _ apis.Convertible = (*TaskRun)(nil)

// ConvertTo implements apis.Convertible
func (tr *TaskRun) ConvertTo(ctx context.Context, sink apis.Convertible) error {
func (tr *TaskRun) ConvertTo(ctx context.Context, to apis.Convertible) error {
if apis.IsInDelete(ctx) {
return nil
}
return fmt.Errorf("v1beta1 is the highest known version, got: %T", sink)
switch sink := to.(type) {
case *v1.TaskRun:
sink.ObjectMeta = tr.ObjectMeta
return tr.Spec.ConvertTo(ctx & sink.Spec)
default:
return fmt.Errorf("unkown version, got: %T", sink)
}
}

// ConvertTo implements apis.Convertible
func (ts *TaskRunSpec) ConvertTo(ctx context.Context, sink *v1.TaskRunSpec) error {
sink.Debug = ts.Debug
sink.Params = nil
for _, p := range ts.Params {
new := v1.ParamSpec{}
p.convertTo(ctx, &new)
sink.Params = append(sink.Params, new)
}
sink.ServiceAccountName = ts.ServiceAccountName
sink.TaskRef = ts.TaskRef
sink.TaskSpec = ts.TaskSpec
sink.Status = ts.Status
sink.Timeout = ts.Timeout
sink.PodTemplate = ts.PodTemplate
sink.Workspaces = nil
for _, w := range ts.Workspaces {
new := v1.WorkspaceBinding{}
w.convertTo(ctx, &new)
sink.Workspaces = append(sink.Workspaces, new)
}
sink.StepOverrides = nil
for _, so := range ts.StepOverrides {
new := v1.TaskRunStepOverride{}
so.convertTo(ctx, &new)
sink.StepOverrides = append(sink.StepOverrides, new)
}
sink.SidecarOverrides = nil
for _, so := range ts.SidecarOverrides {
new := v1.TaskRunSidecarOverride{}
so.convertTo(ctx, &new)
sink.SidecarOverrides = append(sink.SidecarOverrides, new)
}
sink.ComputeResources = ts.ComputeResources
return nil
}

// ConvertFrom implements apis.Convertible
func (tr *TaskRun) ConvertFrom(ctx context.Context, source apis.Convertible) error {
func (tr *TaskRun) ConvertFrom(ctx context.Context, from apis.Convertible) error {
if apis.IsInDelete(ctx) {
return nil
}
return fmt.Errorf("v1beta1 is the highest known version, got: %T", source)
switch source := from.(type) {
case *v1.TaskRun:
tr.ObjectMeta = source.ObjectMeta
return tr.Spec.ConvertFrom(ctx, &source.Spec)
default:
return fmt.Errorf("unkown version, got: %T", tr)
}
}

func (trso *TaskRunStepOverride) convertTo(ctx context.Context, sink *v1.WorkspaceBinding) {
sink.Name = trso.Name
sink.Resources = trso.Resources
}

func (trso *TaskRunStepOverride) convertFrom(ctx context.Context, source v1.WorkspaceBinding) {
trso.Name = source.Name
trso.Resources = source.Resources
}

func (trso *TaskRunSidecarOverride) convertTo(ctx context.Context, sink *v1.WorkspaceBinding) {
sink.Name = trso.Name
sink.Resources = trso.Resources
}

func (trso *TaskRunSidecarOverride) convertFrom(ctx context.Context, source v1.WorkspaceBinding) {
trso.Name = source.Name
trso.Resources = source.Resources
}
Loading

0 comments on commit d36a9cb

Please sign in to comment.