Skip to content

Commit

Permalink
Add conversion for v1 Task
Browse files Browse the repository at this point in the history
This commit adds conversion functions between v1beta1 and v1 Task.
It does not handle fields deprecated in v1beta1 that will not be present in v1.
It implements ConvertTo and ConvertFrom for v1beta1 Task, and leaves
these functions unimplemented for v1 Task, since it is the highest known version.

Conversions for all types are basically deep copies, rather than using the same struct
definition for both API versions. This will allow us to iterate on v1 without making additional
changes to v1beta1, after v1 is the stored version.
  • Loading branch information
lbernick committed Jul 29, 2022
1 parent 5566576 commit 078b7a8
Show file tree
Hide file tree
Showing 8 changed files with 686 additions and 9 deletions.
39 changes: 39 additions & 0 deletions pkg/apis/pipeline/v1/task_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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 = (*Task)(nil)

// ConvertTo implements apis.Convertible
func (t *Task) 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 (t *Task) ConvertFrom(ctx context.Context, source apis.Convertible) error {
if apis.IsInDelete(ctx) {
return nil
}
return fmt.Errorf("v1 is the highest know version, got: %T", source)
}
43 changes: 43 additions & 0 deletions pkg/apis/pipeline/v1/task_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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"
"knative.dev/pkg/apis"
)

type convertible struct{}

func (c *convertible) ConvertTo(ctx context.Context, sink apis.Convertible) error {
return nil
}
func (c *convertible) ConvertFrom(ctx context.Context, source apis.Convertible) error {
return nil
}

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

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)
}
}
160 changes: 160 additions & 0 deletions pkg/apis/pipeline/v1beta1/container_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package v1beta1

import (
"context"

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

func (s Step) convertTo(ctx context.Context, sink *v1.Step) {
sink.Name = s.Name
sink.Image = s.Image
sink.Command = s.Command
sink.Args = s.Args
sink.WorkingDir = s.WorkingDir
sink.EnvFrom = s.EnvFrom
sink.Env = s.Env
sink.Resources = s.Resources
sink.VolumeMounts = s.VolumeMounts
sink.VolumeDevices = s.VolumeDevices
sink.ImagePullPolicy = s.ImagePullPolicy
sink.SecurityContext = s.SecurityContext
sink.Script = s.Script
sink.Timeout = s.Timeout

sink.Workspaces = nil
for _, w := range s.Workspaces {
new := v1.WorkspaceUsage{}
w.convertTo(ctx, &new)
sink.Workspaces = append(sink.Workspaces, new)
}
sink.OnError = s.OnError
sink.StdoutConfig = (*v1.StepOutputConfig)(s.StdoutConfig)
sink.StderrConfig = (*v1.StepOutputConfig)(s.StderrConfig)

// TODO(#4546): Handle deprecated fields
// Ports, LivenessProbe, ReadinessProbe, StartupProbe, Lifecycle, TerminationMessagePath
// TerminationMessagePolicy, Stdin, StdinOnce, TTY
}

func (s *Step) convertFrom(ctx context.Context, source v1.Step) {
s.Name = source.Name
s.Image = source.Image
s.Command = source.Command
s.Args = source.Args
s.WorkingDir = source.WorkingDir
s.EnvFrom = source.EnvFrom
s.Env = source.Env
s.Resources = source.Resources
s.VolumeMounts = source.VolumeMounts
s.VolumeDevices = source.VolumeDevices
s.ImagePullPolicy = source.ImagePullPolicy
s.SecurityContext = source.SecurityContext
s.Script = source.Script
s.Timeout = source.Timeout

s.Workspaces = nil
for _, w := range source.Workspaces {
new := WorkspaceUsage{}
new.convertFrom(ctx, w)
s.Workspaces = append(s.Workspaces, new)
}
s.OnError = source.OnError
s.StdoutConfig = (*StepOutputConfig)(source.StdoutConfig)
s.StderrConfig = (*StepOutputConfig)(source.StderrConfig)
}

func (s StepTemplate) convertTo(ctx context.Context, sink *v1.StepTemplate) {
sink.Image = s.Image
sink.Command = s.Command
sink.Args = s.Args
sink.WorkingDir = s.WorkingDir
sink.EnvFrom = s.EnvFrom
sink.Env = s.Env
sink.Resources = s.Resources
sink.VolumeMounts = s.VolumeMounts
sink.VolumeDevices = s.VolumeDevices
sink.ImagePullPolicy = s.ImagePullPolicy
sink.SecurityContext = s.SecurityContext
// TODO(#4546): Handle deprecated fields
// Name, Ports, LivenessProbe, ReadinessProbe, StartupProbe, Lifecycle, TerminationMessagePath
// TerminationMessagePolicy, Stdin, StdinOnce, TTY
}

func (s *StepTemplate) convertFrom(ctx context.Context, source *v1.StepTemplate) {
s.Image = source.Image
s.Command = source.Command
s.Args = source.Args
s.WorkingDir = source.WorkingDir
s.EnvFrom = source.EnvFrom
s.Env = source.Env
s.Resources = source.Resources
s.VolumeMounts = source.VolumeMounts
s.VolumeDevices = source.VolumeDevices
s.ImagePullPolicy = source.ImagePullPolicy
s.SecurityContext = source.SecurityContext
}

func (s Sidecar) convertTo(ctx context.Context, sink *v1.Sidecar) {
sink.Name = s.Name
sink.Image = s.Image
sink.Command = s.Command
sink.Args = s.Args
sink.WorkingDir = s.WorkingDir
sink.Ports = s.Ports
sink.EnvFrom = s.EnvFrom
sink.Env = s.Env
sink.Resources = s.Resources
sink.VolumeMounts = s.VolumeMounts
sink.VolumeDevices = s.VolumeDevices
sink.LivenessProbe = s.LivenessProbe
sink.ReadinessProbe = s.ReadinessProbe
sink.StartupProbe = s.StartupProbe
sink.Lifecycle = s.Lifecycle
sink.TerminationMessagePath = s.TerminationMessagePath
sink.TerminationMessagePolicy = s.TerminationMessagePolicy
sink.ImagePullPolicy = s.ImagePullPolicy
sink.SecurityContext = s.SecurityContext
sink.Stdin = s.Stdin
sink.StdinOnce = s.StdinOnce
sink.TTY = s.TTY
sink.Script = s.Script
sink.Workspaces = nil
for _, w := range s.Workspaces {
new := v1.WorkspaceUsage{}
w.convertTo(ctx, &new)
sink.Workspaces = append(sink.Workspaces, new)
}
}

func (s *Sidecar) convertFrom(ctx context.Context, source v1.Sidecar) {
s.Name = source.Name
s.Image = source.Image
s.Command = source.Command
s.Args = source.Args
s.WorkingDir = source.WorkingDir
s.Ports = source.Ports
s.EnvFrom = source.EnvFrom
s.Env = source.Env
s.Resources = source.Resources
s.VolumeMounts = source.VolumeMounts
s.VolumeDevices = source.VolumeDevices
s.LivenessProbe = source.LivenessProbe
s.ReadinessProbe = source.ReadinessProbe
s.StartupProbe = source.StartupProbe
s.Lifecycle = source.Lifecycle
s.TerminationMessagePath = source.TerminationMessagePath
s.TerminationMessagePolicy = source.TerminationMessagePolicy
s.ImagePullPolicy = source.ImagePullPolicy
s.SecurityContext = source.SecurityContext
s.Stdin = source.Stdin
s.StdinOnce = source.StdinOnce
s.TTY = source.TTY
s.Script = source.Script
s.Workspaces = nil
for _, w := range source.Workspaces {
new := WorkspaceUsage{}
new.convertFrom(ctx, w)
s.Workspaces = append(s.Workspaces, new)
}
}
47 changes: 47 additions & 0 deletions pkg/apis/pipeline/v1beta1/param_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package v1beta1

import (
"context"

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

func (p ParamSpec) convertTo(ctx context.Context, sink *v1.ParamSpec) {
sink.Name = p.Name
sink.Type = v1.ParamType(p.Type)
sink.Description = p.Description
var properties map[string]v1.PropertySpec
if p.Properties != nil {
properties = make(map[string]v1.PropertySpec)
}
for k, v := range p.Properties {
properties[k] = v1.PropertySpec{Type: v1.ParamType(v.Type)}
}
sink.Properties = properties
if p.Default != nil {
sink.Default = &v1.ArrayOrString{
Type: v1.ParamType(p.Default.Type), StringVal: p.Default.StringVal,
ArrayVal: p.Default.ArrayVal, ObjectVal: p.Default.ObjectVal,
}
}
}

func (p *ParamSpec) convertFrom(ctx context.Context, source v1.ParamSpec) {
p.Name = source.Name
p.Type = ParamType(source.Type)
p.Description = source.Description
var properties map[string]PropertySpec
if source.Properties != nil {
properties = make(map[string]PropertySpec)
}
for k, v := range source.Properties {
properties[k] = PropertySpec{Type: ParamType(v.Type)}
}
p.Properties = properties
if source.Default != nil {
p.Default = &ArrayOrString{
Type: ParamType(source.Default.Type), StringVal: source.Default.StringVal,
ArrayVal: source.Default.ArrayVal, ObjectVal: source.Default.ObjectVal,
}
}
}
29 changes: 29 additions & 0 deletions pkg/apis/pipeline/v1beta1/result_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v1beta1

import (
"context"

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

func (r TaskResult) convertTo(ctx context.Context, sink *v1.TaskResult) {
sink.Name = r.Name
sink.Type = v1.ResultsType(r.Type)
sink.Description = r.Description
properties := make(map[string]v1.PropertySpec)
for k, v := range r.Properties {
properties[k] = v1.PropertySpec{Type: v1.ParamType(v.Type)}
}
sink.Properties = properties
}

func (r *TaskResult) convertFrom(ctx context.Context, source v1.TaskResult) {
r.Name = source.Name
r.Type = ResultsType(source.Type)
r.Description = source.Description
properties := make(map[string]PropertySpec)
for k, v := range source.Properties {
properties[k] = PropertySpec{Type: ParamType(v.Type)}
}
r.Properties = properties
}
Loading

0 comments on commit 078b7a8

Please sign in to comment.