Skip to content

Commit f940717

Browse files
JeromeJutekton-robot
authored andcommitted
Fix bundle conversion for pipelineRef
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. It extracts the setup for bundle and makes it into a helper function.
1 parent 89c2789 commit f940717

File tree

3 files changed

+69
-118
lines changed

3 files changed

+69
-118
lines changed

pkg/apis/pipeline/v1beta1/pipelineref_conversion.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (pr PipelineRef) convertBundleToResolver(sink *v1.PipelineRef) {
3838
Value: v1.ParamValue{StringVal: pr.Name, Type: v1.ParamTypeString},
3939
}, {
4040
Name: "kind",
41-
Value: v1.ParamValue{StringVal: "Task", Type: v1.ParamTypeString},
41+
Value: v1.ParamValue{StringVal: "Pipeline", Type: v1.ParamTypeString},
4242
}},
4343
}
4444
}

pkg/apis/pipeline/v1beta1/pipelinerun_conversion_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestPipelineRunConversionFromDeprecated(t *testing.T) {
258258
Params: []v1beta1.Param{
259259
{Name: "bundle", Value: v1beta1.ParamValue{StringVal: "test-bundle", Type: "string"}},
260260
{Name: "name", Value: v1beta1.ParamValue{StringVal: "test-bundle-name", Type: "string"}},
261-
{Name: "kind", Value: v1beta1.ParamValue{StringVal: "Task", Type: "string"}},
261+
{Name: "kind", Value: v1beta1.ParamValue{StringVal: "Pipeline", Type: "string"}},
262262
},
263263
},
264264
},

test/tektonbundles_test.go

+67-116
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/google/go-containerregistry/pkg/v1/layout"
4040
"github.com/google/go-containerregistry/pkg/v1/mutate"
4141
"github.com/google/go-containerregistry/pkg/v1/tarball"
42+
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
4243
"github.com/tektoncd/pipeline/pkg/pod"
4344
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun"
4445
corev1 "k8s.io/api/core/v1"
@@ -49,7 +50,7 @@ import (
4950
"sigs.k8s.io/yaml"
5051
)
5152

52-
var requireFeatureFlags = requireAnyGate(map[string]string{
53+
var bundleFeatureFlags = requireAnyGate(map[string]string{
5354
"enable-tekton-oci-bundles": "true",
5455
"enable-api-fields": "alpha",
5556
})
@@ -62,7 +63,7 @@ var resolverFeatureFlags = requireAnyGate(map[string]string{
6263
// images.
6364
func TestTektonBundlesSimpleWorkingExample(t *testing.T) {
6465
ctx := context.Background()
65-
c, namespace := setup(ctx, t, withRegistry, requireFeatureFlags)
66+
c, namespace := setup(ctx, t, withRegistry, bundleFeatureFlags)
6667

6768
t.Parallel()
6869

@@ -102,47 +103,7 @@ spec:
102103
bundle: %s
103104
`, pipelineName, namespace, taskName, repo))
104105

105-
// Write the task and pipeline into an image to the registry in the proper format.
106-
rawTask, err := yaml.Marshal(task)
107-
if err != nil {
108-
t.Fatalf("Failed to marshal task to yaml: %s", err)
109-
}
110-
111-
rawPipeline, err := yaml.Marshal(pipeline)
112-
if err != nil {
113-
t.Fatalf("Failed to marshal task to yaml: %s", err)
114-
}
115-
116-
img := empty.Image
117-
taskLayer, err := tarball.LayerFromReader(bytes.NewBuffer(rawTask))
118-
if err != nil {
119-
t.Fatalf("Failed to create oci layer from task: %s", err)
120-
}
121-
pipelineLayer, err := tarball.LayerFromReader(bytes.NewBuffer(rawPipeline))
122-
if err != nil {
123-
t.Fatalf("Failed to create oci layer from pipeline: %s", err)
124-
}
125-
img, err = mutate.Append(img, mutate.Addendum{
126-
Layer: taskLayer,
127-
Annotations: map[string]string{
128-
"dev.tekton.image.name": taskName,
129-
"dev.tekton.image.kind": strings.ToLower(task.Kind),
130-
"dev.tekton.image.apiVersion": task.APIVersion,
131-
},
132-
}, mutate.Addendum{
133-
Layer: pipelineLayer,
134-
Annotations: map[string]string{
135-
"dev.tekton.image.name": pipelineName,
136-
"dev.tekton.image.kind": strings.ToLower(pipeline.Kind),
137-
"dev.tekton.image.apiVersion": pipeline.APIVersion,
138-
},
139-
})
140-
if err != nil {
141-
t.Fatalf("Failed to create an oci image from the task and pipeline layers: %s", err)
142-
}
143-
144-
// Publish this image to the in-cluster registry.
145-
publishImg(ctx, t, c, namespace, img, ref)
106+
setupBundle(ctx, t, c, namespace, repo, task, pipeline)
146107

147108
// Now generate a PipelineRun to invoke this pipeline and task.
148109
pr := parse.MustParsePipelineRun(t, fmt.Sprintf(`
@@ -212,10 +173,6 @@ func TestTektonBundlesResolver(t *testing.T) {
212173
pipelineName := helpers.ObjectNameForTest(t)
213174
pipelineRunName := helpers.ObjectNameForTest(t)
214175
repo := fmt.Sprintf("%s:5000/tektonbundlesresolver", getRegistryServiceIP(ctx, t, c, namespace))
215-
ref, err := name.ParseReference(repo)
216-
if err != nil {
217-
t.Fatalf("Failed to parse %s as an OCI reference: %s", repo, err)
218-
}
219176

220177
task := parse.MustParseTask(t, fmt.Sprintf(`
221178
metadata:
@@ -244,47 +201,7 @@ spec:
244201
value: %s
245202
`, pipelineName, namespace, repo, taskName))
246203

247-
// Write the task and pipeline into an image to the registry in the proper format.
248-
rawTask, err := yaml.Marshal(task)
249-
if err != nil {
250-
t.Fatalf("Failed to marshal task to yaml: %s", err)
251-
}
252-
253-
rawPipeline, err := yaml.Marshal(pipeline)
254-
if err != nil {
255-
t.Fatalf("Failed to marshal task to yaml: %s", err)
256-
}
257-
258-
img := empty.Image
259-
taskLayer, err := tarball.LayerFromReader(bytes.NewBuffer(rawTask))
260-
if err != nil {
261-
t.Fatalf("Failed to create oci layer from task: %s", err)
262-
}
263-
pipelineLayer, err := tarball.LayerFromReader(bytes.NewBuffer(rawPipeline))
264-
if err != nil {
265-
t.Fatalf("Failed to create oci layer from pipeline: %s", err)
266-
}
267-
img, err = mutate.Append(img, mutate.Addendum{
268-
Layer: taskLayer,
269-
Annotations: map[string]string{
270-
"dev.tekton.image.name": taskName,
271-
"dev.tekton.image.kind": strings.ToLower(task.Kind),
272-
"dev.tekton.image.apiVersion": task.APIVersion,
273-
},
274-
}, mutate.Addendum{
275-
Layer: pipelineLayer,
276-
Annotations: map[string]string{
277-
"dev.tekton.image.name": pipelineName,
278-
"dev.tekton.image.kind": strings.ToLower(pipeline.Kind),
279-
"dev.tekton.image.apiVersion": pipeline.APIVersion,
280-
},
281-
})
282-
if err != nil {
283-
t.Fatalf("Failed to create an oci image from the task and pipeline layers: %s", err)
284-
}
285-
286-
// Publish this image to the in-cluster registry.
287-
publishImg(ctx, t, c, namespace, img, ref)
204+
setupBundle(ctx, t, c, namespace, repo, task, pipeline)
288205

289206
// Now generate a PipelineRun to invoke this pipeline and task.
290207
pr := parse.MustParsePipelineRun(t, fmt.Sprintf(`
@@ -348,7 +265,7 @@ spec:
348265
// TestTektonBundlesUsingRegularImage is an integration test which passes a non-Tekton bundle as a task reference.
349266
func TestTektonBundlesUsingRegularImage(t *testing.T) {
350267
ctx := context.Background()
351-
c, namespace := setup(ctx, t, withRegistry, requireFeatureFlags)
268+
c, namespace := setup(ctx, t, withRegistry, bundleFeatureFlags)
352269

353270
t.Parallel()
354271

@@ -377,32 +294,7 @@ spec:
377294
bundle: registry
378295
`, pipelineName, namespace, taskName))
379296

380-
// Write the pipeline into an image to the registry in the proper format. We don't write the task because we are
381-
// using an non Tekton Bundle.
382-
rawPipeline, err := yaml.Marshal(pipeline)
383-
if err != nil {
384-
t.Fatalf("Failed to marshal task to yaml: %s", err)
385-
}
386-
387-
img := empty.Image
388-
pipelineLayer, err := tarball.LayerFromReader(bytes.NewBuffer(rawPipeline))
389-
if err != nil {
390-
t.Fatalf("Failed to create oci layer from pipeline: %s", err)
391-
}
392-
img, err = mutate.Append(img, mutate.Addendum{
393-
Layer: pipelineLayer,
394-
Annotations: map[string]string{
395-
"dev.tekton.image.name": pipelineName,
396-
"dev.tekton.image.kind": strings.ToLower(pipeline.Kind),
397-
"dev.tekton.image.apiVersion": pipeline.APIVersion,
398-
},
399-
})
400-
if err != nil {
401-
t.Fatalf("Failed to create an oci image from the task and pipeline layers: %s", err)
402-
}
403-
404-
// Publish this image to the in-cluster registry.
405-
publishImg(ctx, t, c, namespace, img, ref)
297+
setupBundle(ctx, t, c, namespace, repo, nil, pipeline)
406298

407299
// Now generate a PipelineRun to invoke this pipeline and task.
408300
pr := parse.MustParsePipelineRun(t, fmt.Sprintf(`
@@ -431,7 +323,7 @@ spec:
431323
// task reference.
432324
func TestTektonBundlesUsingImproperFormat(t *testing.T) {
433325
ctx := context.Background()
434-
c, namespace := setup(ctx, t, withRegistry, requireFeatureFlags)
326+
c, namespace := setup(ctx, t, withRegistry, bundleFeatureFlags)
435327

436328
t.Parallel()
437329

@@ -671,3 +563,62 @@ func publishImg(ctx context.Context, t *testing.T, c *clients, namespace string,
671563
}
672564
}
673565
}
566+
567+
// setupBundle creates an empty image, provides a reference to the fake registry and pushes the
568+
// bundled task and pipeline within an image into the registry
569+
func setupBundle(ctx context.Context, t *testing.T, c *clients, namespace, repo string, task *v1beta1.Task, pipeline *v1beta1.Pipeline) {
570+
t.Helper()
571+
img := empty.Image
572+
ref, err := name.ParseReference(repo)
573+
if err != nil {
574+
t.Fatalf("Failed to parse %s as an OCI reference: %s", repo, err)
575+
}
576+
577+
var rawTask, rawPipeline []byte
578+
var taskLayer, pipelineLayer v1.Layer
579+
// Write the task and pipeline into an image to the registry in the proper format.
580+
if task != nil {
581+
rawTask, err = yaml.Marshal(task)
582+
if err != nil {
583+
t.Fatalf("Failed to marshal task to yaml: %s", err)
584+
}
585+
taskLayer, err = tarball.LayerFromReader(bytes.NewBuffer(rawTask))
586+
if err != nil {
587+
t.Fatalf("Failed to create oci layer from task: %s", err)
588+
}
589+
img, err = mutate.Append(img, mutate.Addendum{
590+
Layer: taskLayer,
591+
Annotations: map[string]string{
592+
"dev.tekton.image.name": task.Name,
593+
"dev.tekton.image.kind": strings.ToLower(task.Kind),
594+
"dev.tekton.image.apiVersion": task.APIVersion,
595+
},
596+
})
597+
}
598+
599+
if pipeline != nil {
600+
rawPipeline, err = yaml.Marshal(pipeline)
601+
if err != nil {
602+
t.Fatalf("Failed to marshal task to yaml: %s", err)
603+
}
604+
pipelineLayer, err = tarball.LayerFromReader(bytes.NewBuffer(rawPipeline))
605+
if err != nil {
606+
t.Fatalf("Failed to create oci layer from pipeline: %s", err)
607+
}
608+
img, err = mutate.Append(img, mutate.Addendum{
609+
Layer: pipelineLayer,
610+
Annotations: map[string]string{
611+
"dev.tekton.image.name": pipeline.Name,
612+
"dev.tekton.image.kind": strings.ToLower(pipeline.Kind),
613+
"dev.tekton.image.apiVersion": pipeline.APIVersion,
614+
},
615+
})
616+
}
617+
618+
if err != nil {
619+
t.Fatalf("Failed to create an oci image from the task and pipeline layers: %s", err)
620+
}
621+
622+
// Publish this image to the in-cluster registry.
623+
publishImg(ctx, t, c, namespace, img, ref)
624+
}

0 commit comments

Comments
 (0)