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

Add conversion for v1 Task #5202

Merged
merged 1 commit into from
Aug 1, 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
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 known 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Will conversions fail if we do not implement this in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, since we don't have the v1 version added to our CRD yet. I have a follow-up PR almost ready that adds conversion for Resources. If we did have v1 versions in the task crd, conversion wouldn't fail, but it would silently drop any resources the user has defined. (I think this may only be backwards incompatible once we update the storage version of the crd but I'm not completely sure)

// 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