Skip to content

Commit

Permalink
Fix bundle conversion for pipelineRef
Browse files Browse the repository at this point in the history
This commit fixes the bundle conversion for PipelineRef which should
be converting to the bundle Resolver with a Pipeline kind. It also adds
the conversion integration tests for the bundle logics to prevent such
error.
  • Loading branch information
JeromeJu committed Jun 8, 2023
1 parent b5c2135 commit 0d88181
Show file tree
Hide file tree
Showing 3 changed files with 350 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/pipelineref_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (pr PipelineRef) convertBundleToResolver(sink *v1.PipelineRef) {
Value: v1.ParamValue{StringVal: pr.Name, Type: v1.ParamTypeString},
}, {
Name: "kind",
Value: v1.ParamValue{StringVal: "Task", Type: v1.ParamTypeString},
Value: v1.ParamValue{StringVal: "Pipeline", Type: v1.ParamTypeString},
}},
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/pipelinerun_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func TestPipelineRunConversionFromDeprecated(t *testing.T) {
Params: v1beta1.Params{
{Name: "bundle", Value: v1beta1.ParamValue{StringVal: "test-bundle", Type: "string"}},
{Name: "name", Value: v1beta1.ParamValue{StringVal: "test-bundle-name", Type: "string"}},
{Name: "kind", Value: v1beta1.ParamValue{StringVal: "Task", Type: "string"}},
{Name: "kind", Value: v1beta1.ParamValue{StringVal: "Pipeline", Type: "string"}},
},
},
},
Expand Down
348 changes: 348 additions & 0 deletions test/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,34 @@ limitations under the License.
package test

import (
"bytes"
"context"
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/test/parse"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativetest "knative.dev/pkg/test"
"knative.dev/pkg/test/helpers"
"sigs.k8s.io/yaml"
)

var (
requireBundleFeatureFlags = requireAnyGate(map[string]string{
"enable-tekton-oci-bundles": "true",
"enable-api-fields": "alpha",
})

filterLabels = cmpopts.IgnoreFields(metav1.ObjectMeta{}, "Labels")
filterAnnotations = cmpopts.IgnoreFields(metav1.ObjectMeta{}, "Annotations")
filterV1TaskRunStatus = cmpopts.IgnoreFields(v1.TaskRunStatusFields{}, "StartTime", "CompletionTime")
Expand Down Expand Up @@ -649,6 +661,183 @@ status:
name: %s-hello-task
pipelineTaskName: hello-task
`

v1beta1TaskRunWithBundleYaml = `
metadata:
name: %s
namespace: %s
spec:
taskRef:
name: %s
bundle: %s
`

v1beta1PipelineRunWithBundleYaml = `
metadata:
name: %s
namespace: %s
spec:
pipelineRef:
name: %s
bundle: %s
`

v1TaskRunWithBundleExpectedYaml = `
metadata:
name: %s
namespace: %s
spec:
serviceAccountName: default
timeout: 1h
taskRef:
kind: Task
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: Task
status:
conditions:
- type: Succeeded
status: "True"
reason: "Succeeded"
podName: %s-pod
taskSpec:
steps:
- computeResources: {}
image: alpine
name: hello
script: 'echo Hello'
steps:
- image: alpine
name: hello
script: 'echo Hello'
terminated:
reason: Completed
`

v1PipelineRunWithBundleExpectedYaml = `
metadata:
name: %s
namespace: %s
spec:
taskRunTemplate:
serviceAccountName: default
timeouts:
pipeline: 1h
pipelineRef:
kind: Pipeline
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: Pipeline
status:
conditions:
- type: Succeeded
status: "True"
reason: "Succeeded"
pipelineSpec:
tasks:
- name: hello-world
taskRef:
kind: Task
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
childReferences:
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: %s-hello-world
pipelineTaskName: hello-world
`

v1beta1TaskRunWithBundleRoundTripYaml = `
metadata:
name: %s
namespace: %s
spec:
serviceAccountName: default
timeout: 1h
taskRef:
kind: Task
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: Task
status:
conditions:
- type: Succeeded
status: "True"
reason: "Succeeded"
podName: %s-pod
taskSpec:
steps:
- computeResources: {}
image: alpine
name: hello
script: 'echo Hello'
steps:
- image: alpine
name: hello
script: 'echo Hello'
terminated:
reason: Completed
`

v1beta1PipelineRunWithBundleRoundTripYaml = `
metadata:
name: %s
namespace: %s
spec:
serviceAccountName: default
timeouts:
pipeline: 1h
pipelineRef:
kind: Pipeline
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: Pipeline
status:
conditions:
- type: Succeeded
status: "True"
reason: "Succeeded"
pipelineSpec:
tasks:
- name: hello-world
taskRef:
kind: Task
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
childReferences:
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: %s-hello-world
pipelineTaskName: hello-world
`
)

// TestTaskCRDConversion first creates a v1beta1 Task CRD using v1beta1Clients and
Expand Down Expand Up @@ -947,3 +1136,162 @@ func TestPipelineRunCRDConversion(t *testing.T) {
t.Errorf("-want, +got: %v", d)
}
}

// TestBundleConversion tests v1beta1 bundle syntax converted into v1 since it has been
// deprecated in v1 and it would be converted into bundle resolver in pipelineRef and
// taskRef.
func TestBundleConversion(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

t.Parallel()

c, namespace := setup(ctx, t, withRegistry, requireBundleFeatureFlags)
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)

repo := fmt.Sprintf("%s:5000/tektonbundlessimple", getRegistryServiceIP(ctx, t, c, namespace))
ref, err := name.ParseReference(repo)
if err != nil {
t.Fatalf("Failed to parse %s as an OCI reference: %s", repo, err)
}

taskName := helpers.ObjectNameForTest(t)
pipelineName := helpers.ObjectNameForTest(t)
v1beta1TaskRunName := helpers.ObjectNameForTest(t)
task := parse.MustParseV1beta1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
steps:
- name: hello
image: alpine
script: 'echo Hello'
`, taskName, namespace))
pipeline := parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
tasks:
- name: hello-world
taskRef:
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
`, pipelineName, namespace, repo, taskName))
v1beta1TaskRun := parse.MustParseV1beta1TaskRun(t, fmt.Sprintf(v1beta1TaskRunWithBundleYaml, v1beta1TaskRunName, namespace, taskName, repo))
v1TaskRunExpected := parse.MustParseV1TaskRun(t, fmt.Sprintf(v1TaskRunWithBundleExpectedYaml, v1beta1TaskRunName, namespace, repo, taskName, v1beta1TaskRunName))
v1beta1TaskRunRoundTripExpected := parse.MustParseV1beta1TaskRun(t, fmt.Sprintf(v1beta1TaskRunWithBundleRoundTripYaml, v1beta1TaskRunName, namespace, repo, taskName, v1beta1TaskRunName))

// Write the task and pipeline into an image to the registry in the proper format.
rawTask, err := yaml.Marshal(task)
if err != nil {
t.Fatalf("Failed to marshal task to yaml: %s", err)
}

rawPipeline, err := yaml.Marshal(pipeline)
if err != nil {
t.Fatalf("Failed to marshal task to yaml: %s", err)
}
img := empty.Image
taskLayer, err := tarball.LayerFromReader(bytes.NewBuffer(rawTask))
if err != nil {
t.Fatalf("Failed to create oci layer from task: %s", err)
}
pipelineLayer, err := tarball.LayerFromReader(bytes.NewBuffer(rawPipeline))
if err != nil {
t.Fatalf("Failed to create oci layer from pipeline: %s", err)
}
img, err = mutate.Append(img, mutate.Addendum{
Layer: taskLayer,
Annotations: map[string]string{
"dev.tekton.image.name": taskName,
"dev.tekton.image.kind": strings.ToLower(task.Kind),
"dev.tekton.image.apiVersion": task.APIVersion,
},
}, mutate.Addendum{
Layer: pipelineLayer,
Annotations: map[string]string{
"dev.tekton.image.name": pipelineName,
"dev.tekton.image.kind": strings.ToLower(pipeline.Kind),
"dev.tekton.image.apiVersion": pipeline.APIVersion,
},
})
if err != nil {
t.Fatalf("Failed to create an oci image from the task and pipeline layers: %s", err)
}

// Publish this image to the in-cluster registry.
publishImg(ctx, t, c, namespace, img, ref)

v1TaskRunExpected.Status.Provenance = &v1.Provenance{
FeatureFlags: getFeatureFlagsBaseOnAPIFlag(t),
}
v1beta1TaskRunRoundTripExpected.Status.Provenance = &v1beta1.Provenance{
FeatureFlags: getFeatureFlagsBaseOnAPIFlag(t),
}

if _, err := c.V1beta1TaskRunClient.Create(ctx, v1beta1TaskRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create v1beta1 TaskRun: %s", err)
}
if err := WaitForTaskRunState(ctx, c, v1beta1TaskRunName, Succeed(v1beta1TaskRunName), v1beta1TaskRunName, "v1beta1"); err != nil {
t.Fatalf("Failed waiting for v1beta1 TaskRun done: %v", err)
}

v1TaskRunGot, err := c.V1TaskRunClient.Get(ctx, v1beta1TaskRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Couldn't get expected v1 TaskRun for %s: %s", v1beta1TaskRunName, err)
}
if d := cmp.Diff(v1TaskRunExpected, v1TaskRunGot, filterV1TaskRunFields...); d != "" {
t.Errorf("-want, +got: %v", d)
}

v1beta1TaskRunRoundTrip := &v1beta1.TaskRun{}
if err := v1beta1TaskRunRoundTrip.ConvertFrom(context.Background(), v1TaskRunGot); err != nil {
t.Fatalf("Failed to convert roundtrip v1beta1TaskRunGot ConvertFrom v1 = %v", err)
}
if d := cmp.Diff(v1beta1TaskRunRoundTripExpected, v1beta1TaskRunRoundTrip, filterV1beta1TaskRunFields...); d != "" {
t.Errorf("-want, +got: %v", d)
}

v1beta1ToV1PipelineRunName := helpers.ObjectNameForTest(t)
v1beta1PipelineRun := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(v1beta1PipelineRunWithBundleYaml, v1beta1ToV1PipelineRunName, namespace, pipelineName, repo))
v1PipelineRunExpected := parse.MustParseV1PipelineRun(t, fmt.Sprintf(v1PipelineRunWithBundleExpectedYaml, v1beta1ToV1PipelineRunName, namespace, repo, pipelineName, repo, taskName, v1beta1ToV1PipelineRunName))
v1beta1PRRoundTripExpected := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(v1beta1PipelineRunWithBundleRoundTripYaml, v1beta1ToV1PipelineRunName, namespace, repo, pipelineName, repo, taskName, v1beta1ToV1PipelineRunName))

v1PipelineRunExpected.Status.Provenance = &v1.Provenance{
FeatureFlags: getFeatureFlagsBaseOnAPIFlag(t),
}
v1beta1PRRoundTripExpected.Status.Provenance = &v1beta1.Provenance{
FeatureFlags: getFeatureFlagsBaseOnAPIFlag(t),
}

if _, err := c.V1beta1PipelineRunClient.Create(ctx, v1beta1PipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create v1beta1 PipelineRun: %s", err)
}
if err := WaitForPipelineRunState(ctx, c, v1beta1ToV1PipelineRunName, timeout, Succeed(v1beta1ToV1PipelineRunName), v1beta1ToV1PipelineRunName, "v1beta1"); err != nil {
t.Fatalf("Failed waiting for v1beta1 PipelineRun done: %v", err)
}

v1PipelineRunGot, err := c.V1PipelineRunClient.Get(ctx, v1beta1ToV1PipelineRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Couldn't get expected v1 PipelineRun for %s: %s", v1beta1ToV1PipelineRunName, err)
}
if d := cmp.Diff(v1PipelineRunExpected, v1PipelineRunGot, filterV1PipelineRunFields...); d != "" {
t.Errorf("-want, +got: %v", d)
}

v1beta1PRRoundTrip := &v1beta1.PipelineRun{}
if err := v1beta1PRRoundTrip.ConvertFrom(context.Background(), v1PipelineRunGot); err != nil {
t.Fatalf("Error roundtrip v1beta1PipelineRun ConvertFrom v1PipelineRunGot = %v", err)
}
if d := cmp.Diff(v1beta1PRRoundTripExpected, v1beta1PRRoundTrip, filterV1beta1PipelineRunFields...); d != "" {
t.Errorf("-want, +got: %v", d)
}
}

0 comments on commit 0d88181

Please sign in to comment.