-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
image_exporter.go
98 lines (82 loc) · 3.21 KB
/
image_exporter.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
/*
Copyright 2019 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 resources
import (
"encoding/json"
"path/filepath"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/names"
"golang.org/x/xerrors"
corev1 "k8s.io/api/core/v1"
)
const imageDigestExporterContainerName = "image-digest-exporter"
// AddOutputImageDigestExporter add a step to check the index.json for all output images
func AddOutputImageDigestExporter(
imageDigestExporterImage string,
tr *v1alpha1.TaskRun,
taskSpec *v1alpha1.TaskSpec,
gr GetResource,
) error {
output := []*v1alpha1.ImageResource{}
if len(tr.Spec.Outputs.Resources) > 0 {
for _, trb := range tr.Spec.Outputs.Resources {
boundResource, err := getBoundResource(trb.Name, tr.Spec.Outputs.Resources)
if err != nil {
return xerrors.Errorf("Failed to get bound resource: %w while adding output image digest exporter", err)
}
resource, err := GetResourceFromBinding(&boundResource.PipelineResourceBinding, gr)
if err != nil {
return xerrors.Errorf("Failed to get output pipeline Resource for taskRun %q resource %v; error: %w while adding output image digest exporter", tr.Name, boundResource, err)
}
if resource.Spec.Type == v1alpha1.PipelineResourceTypeImage {
imageResource, err := v1alpha1.NewImageResource(resource)
if err != nil {
return xerrors.Errorf("Invalid Image Resource for taskRun %q resource %v; error: %w", tr.Name, boundResource, err)
}
for _, o := range taskSpec.Outputs.Resources {
if o.Name == boundResource.Name {
if o.TargetPath == "" {
imageResource.OutputImageDir = filepath.Join(outputDir, boundResource.Name)
} else {
imageResource.OutputImageDir = o.TargetPath
}
break
}
}
output = append(output, imageResource)
}
}
if len(output) > 0 {
augmentedSteps := []v1alpha1.Step{}
imagesJSON, err := json.Marshal(output)
if err != nil {
return xerrors.Errorf("Failed to format image resource data for output image exporter: %w", err)
}
augmentedSteps = append(augmentedSteps, taskSpec.Steps...)
augmentedSteps = append(augmentedSteps, imageDigestExporterStep(imageDigestExporterImage, imagesJSON))
taskSpec.Steps = augmentedSteps
}
}
return nil
}
func imageDigestExporterStep(imageDigestExporterImage string, imagesJSON []byte) v1alpha1.Step {
return v1alpha1.Step{Container: corev1.Container{
Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(imageDigestExporterContainerName),
Image: imageDigestExporterImage,
Command: []string{"/ko-app/imagedigestexporter"},
Args: []string{
"-images", string(imagesJSON),
},
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
}}
}