forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtektonbundles_test.go
671 lines (595 loc) · 20.9 KB
/
tektonbundles_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//go:build e2e
// +build e2e
/*
Copyright 2020 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 test
import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/tektoncd/pipeline/pkg/pod"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun"
"github.com/tektoncd/pipeline/test/parse"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
knativetest "knative.dev/pkg/test"
"knative.dev/pkg/test/helpers"
"sigs.k8s.io/yaml"
)
var requireFeatureFlags = requireAnyGate(map[string]string{
"enable-tekton-oci-bundles": "true",
"enable-api-fields": "alpha",
})
var resolverFeatureFlags = requireAnyGate(map[string]string{
"enable-bundles-resolver": "true",
})
// TestTektonBundlesSimpleWorkingExample is an integration test which tests a simple, working Tekton bundle using OCI
// images.
func TestTektonBundlesSimpleWorkingExample(t *testing.T) {
ctx := context.Background()
c, namespace := setup(ctx, t, withRegistry, requireFeatureFlags)
t.Parallel()
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
taskName := helpers.ObjectNameForTest(t)
pipelineName := helpers.ObjectNameForTest(t)
pipelineRunName := helpers.ObjectNameForTest(t)
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)
}
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:
name: %s
bundle: %s
`, pipelineName, namespace, taskName, repo))
// 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)
// Now generate a PipelineRun to invoke this pipeline and task.
pr := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
spec:
pipelineRef:
name: %s
bundle: %s
`, pipelineRunName, pipelineName, repo))
if _, err := c.V1beta1PipelineRunClient.Create(ctx, pr, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun: %s", err)
}
t.Logf("Waiting for PipelineRun in namespace %s to finish", namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRunName, timeout, PipelineRunSucceed(pipelineRunName), "PipelineRunCompleted", v1beta1Version); err != nil {
t.Errorf("Error waiting for PipelineRun to finish with error: %s", err)
}
trs, err := c.V1beta1TaskRunClient.List(ctx, metav1.ListOptions{})
if err != nil {
t.Errorf("Error retrieving taskrun: %s", err)
}
if len(trs.Items) != 1 {
t.Fatalf("Expected 1 TaskRun but found %d", len(trs.Items))
}
tr := trs.Items[0]
if tr.Status.GetCondition(apis.ConditionSucceeded).IsFalse() {
t.Errorf("Expected TaskRun to succeed but instead found condition: %s", tr.Status.GetCondition(apis.ConditionSucceeded))
}
if tr.Status.PodName == "" {
t.Fatal("Error getting a PodName (empty)")
}
p, err := c.KubeClient.CoreV1().Pods(namespace).Get(ctx, tr.Status.PodName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace)
}
for _, stat := range p.Status.ContainerStatuses {
if strings.Contains(stat.Name, "step-hello") {
req := c.KubeClient.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name})
logContent, err := req.Do(ctx).Raw()
if err != nil {
t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace)
}
if !strings.Contains(string(logContent), "Hello") {
t.Fatalf("Expected logs to say hello but received %v", logContent)
}
}
}
}
// TestTektonBundlesResolver is an integration test which tests a simple, working Tekton bundle using OCI
// images using the remote resolution bundles resolver.
func TestTektonBundlesResolver(t *testing.T) {
ctx := context.Background()
c, namespace := setup(ctx, t, withRegistry, resolverFeatureFlags)
t.Parallel()
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
taskName := helpers.ObjectNameForTest(t)
pipelineName := helpers.ObjectNameForTest(t)
pipelineRunName := helpers.ObjectNameForTest(t)
repo := fmt.Sprintf("%s:5000/tektonbundlesresolver", 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)
}
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))
// 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)
// Now generate a PipelineRun to invoke this pipeline and task.
pr := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
spec:
pipelineRef:
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: pipeline
`, pipelineRunName, repo, pipelineName))
if _, err := c.V1beta1PipelineRunClient.Create(ctx, pr, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun: %s", err)
}
t.Logf("Waiting for PipelineRun in namespace %s to finish", namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRunName, timeout, PipelineRunSucceed(pipelineRunName), "PipelineRunCompleted", v1beta1Version); err != nil {
t.Errorf("Error waiting for PipelineRun to finish with error: %s", err)
}
trs, err := c.V1beta1TaskRunClient.List(ctx, metav1.ListOptions{})
if err != nil {
t.Errorf("Error retrieving taskrun: %s", err)
}
if len(trs.Items) != 1 {
t.Fatalf("Expected 1 TaskRun but found %d", len(trs.Items))
}
tr := trs.Items[0]
if tr.Status.GetCondition(apis.ConditionSucceeded).IsFalse() {
t.Errorf("Expected TaskRun to succeed but instead found condition: %s", tr.Status.GetCondition(apis.ConditionSucceeded))
}
if tr.Status.PodName == "" {
t.Fatal("Error getting a PodName (empty)")
}
p, err := c.KubeClient.CoreV1().Pods(namespace).Get(ctx, tr.Status.PodName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace)
}
for _, stat := range p.Status.ContainerStatuses {
if strings.Contains(stat.Name, "step-hello") {
req := c.KubeClient.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name})
logContent, err := req.Do(ctx).Raw()
if err != nil {
t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace)
}
if !strings.Contains(string(logContent), "Hello") {
t.Fatalf("Expected logs to say hello but received %v", logContent)
}
}
}
}
// TestTektonBundlesUsingRegularImage is an integration test which passes a non-Tekton bundle as a task reference.
func TestTektonBundlesUsingRegularImage(t *testing.T) {
ctx := context.Background()
c, namespace := setup(ctx, t, withRegistry, requireFeatureFlags)
t.Parallel()
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
taskName := helpers.ObjectNameForTest(t)
pipelineName := helpers.ObjectNameForTest(t)
pipelineRunName := helpers.ObjectNameForTest(t)
repo := fmt.Sprintf("%s:5000/tektonbundlesregularimage", 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)
}
pipeline := parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
tasks:
- name: hello-world
taskRef:
name: %s
bundle: registry
`, pipelineName, namespace, taskName))
// Write the pipeline into an image to the registry in the proper format. We don't write the task because we are
// using an non Tekton Bundle.
rawPipeline, err := yaml.Marshal(pipeline)
if err != nil {
t.Fatalf("Failed to marshal task to yaml: %s", err)
}
img := empty.Image
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: 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)
// Now generate a PipelineRun to invoke this pipeline and task.
pr := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
spec:
pipelineRef:
name: %s
bundle: %s
`, pipelineRunName, pipelineName, repo))
if _, err := c.V1beta1PipelineRunClient.Create(ctx, pr, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun: %s", err)
}
t.Logf("Waiting for PipelineRun in namespace %s to finish", namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRunName, timeout,
Chain(
FailedWithReason(pod.ReasonCouldntGetTask, pipelineRunName),
FailedWithMessage("does not contain a dev.tekton.image.apiVersion annotation", pipelineRunName),
), "PipelineRunFailed", v1beta1Version); err != nil {
t.Fatalf("Error waiting for PipelineRun to finish with expected error: %s", err)
}
}
// TestTektonBundlesUsingImproperFormat is an integration test which passes an improperly formatted Tekton bundle as a
// task reference.
func TestTektonBundlesUsingImproperFormat(t *testing.T) {
ctx := context.Background()
c, namespace := setup(ctx, t, withRegistry, requireFeatureFlags)
t.Parallel()
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
taskName := helpers.ObjectNameForTest(t)
pipelineName := helpers.ObjectNameForTest(t)
pipelineRunName := helpers.ObjectNameForTest(t)
repo := fmt.Sprintf("%s:5000/tektonbundlesimproperformat", 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)
}
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:
name: %s
bundle: %s
`, pipelineName, namespace, taskName, repo))
// Write the pipeline into an image to the registry in the proper format. Write the task using incorrect
// annotations.
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{
// intentionally invalid name annotation
"org.opencontainers.image.title": 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)
// Now generate a PipelineRun to invoke this pipeline and task.
pr := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
spec:
pipelineRef:
name: %s
bundle: %s
`, pipelineRunName, pipelineName, repo))
if _, err := c.V1beta1PipelineRunClient.Create(ctx, pr, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun: %s", err)
}
t.Logf("Waiting for PipelineRun in namespace %s to finish", namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRunName, timeout,
Chain(
FailedWithReason(pipelinerun.ReasonCouldntGetPipeline, pipelineRunName),
FailedWithMessage("does not contain a dev.tekton.image.name annotation", pipelineRunName),
), "PipelineRunFailed", v1beta1Version); err != nil {
t.Fatalf("Error waiting for PipelineRun to finish with expected error: %s", err)
}
}
func tarImageInOCIFormat(namespace string, img v1.Image) ([]byte, error) {
// Write the image in the OCI layout and then tar it up.
dir, err := os.MkdirTemp(os.TempDir(), namespace)
if err != nil {
return nil, err
}
p, err := layout.Write(dir, empty.Index)
if err != nil {
return nil, err
}
if err := p.AppendImage(img); err != nil {
return nil, err
}
// Now that the layout is correct, package this up as a tarball.
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
// Generate the initial tar header.
header, err := tar.FileInfoHeader(info, path)
if err != nil {
return err
}
// Rewrite the filename with a relative path to the root dir.
header.Name = strings.Replace(path, dir, "", 1)
if err := tw.WriteHeader(header); err != nil {
return err
}
// If not a dir, write file content as is.
if !info.IsDir() {
data, err := os.Open(path)
if err != nil {
return err
}
if _, err := io.Copy(tw, data); err != nil {
return err
}
}
return nil
}); err != nil {
return nil, err
}
if err := tw.Close(); err != nil {
return nil, err
}
// Pull out the tar bundle into a bytes object.
return io.ReadAll(&buf)
}
// publishImg will generate a Pod that runs in the namespace to publish an OCI compliant image into the local registry
// that runs in the cluster. We can't speak to the in-cluster registry from the test so we need to run a Pod to do it
// for us.
func publishImg(ctx context.Context, t *testing.T, c *clients, namespace string, img v1.Image, ref name.Reference) {
t.Helper()
podName := "publish-tekton-bundle"
tb, err := tarImageInOCIFormat(namespace, img)
if err != nil {
t.Fatalf("Failed to create OCI tar bundle: %s", err)
}
// Create a configmap to contain the tarball which we will mount in the pod.
cmName := namespace + "uploadimage-cm"
if _, err = c.KubeClient.CoreV1().ConfigMaps(namespace).Create(ctx, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: cmName},
BinaryData: map[string][]byte{
"image.tar": tb,
},
}, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create configmap to upload image: %s", err)
}
po, err := c.KubeClient.CoreV1().Pods(namespace).Create(ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
GenerateName: podName,
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{{
Name: "cm",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{LocalObjectReference: corev1.LocalObjectReference{Name: cmName}},
},
}, {
Name: "scratch",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}},
InitContainers: []corev1.Container{{
Name: "untar",
Image: "busybox",
Command: []string{"/bin/sh", "-c"},
Args: []string{"mkdir -p /var/image && tar xvf /var/cm/image.tar -C /var/image"},
VolumeMounts: []corev1.VolumeMount{{
Name: "cm",
MountPath: "/var/cm",
}, {
Name: "scratch",
MountPath: "/var/image",
}},
}},
Containers: []corev1.Container{{
Name: "skopeo",
Image: "gcr.io/tekton-releases/dogfooding/skopeo:latest",
WorkingDir: "/var",
Command: []string{"/bin/sh", "-c"},
Args: []string{"skopeo copy --dest-tls-verify=false oci:image docker://" + ref.String()},
VolumeMounts: []corev1.VolumeMount{{
Name: "scratch",
MountPath: "/var/image",
}},
}},
RestartPolicy: corev1.RestartPolicyNever,
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Failed to create the skopeo pod: %v", err)
}
if err := WaitForPodState(ctx, c, po.Name, namespace, func(pod *corev1.Pod) (bool, error) {
return pod.Status.Phase == "Succeeded", nil
}, "PodContainersTerminated"); err != nil {
req := c.KubeClient.CoreV1().Pods(namespace).GetLogs(po.GetName(), &corev1.PodLogOptions{Container: "skopeo"})
logs, err := req.DoRaw(ctx)
if err != nil {
t.Fatalf("Error waiting for Pod %q to terminate: %v", podName, err)
} else {
t.Fatalf("Failed to create image. Pod logs are: \n%s", string(logs))
}
}
}