forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_exporter.go
126 lines (107 loc) · 4.15 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
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
/*
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"
"flag"
"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 TerminationMessagePath = "/builder/home/image-outputs/termination-log"
var (
imageDigestExporterImage = flag.String("imagedigest-exporter-image", "override-with-imagedigest-exporter-image:latest", "The container image containing our image digest exporter binary.")
)
// AddOutputImageDigestExporter add a step to check the index.json for all output images
func AddOutputImageDigestExporter(
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 := getResource(boundResource, 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.OutputImageDir != "" {
imageResource.OutputImageDir = o.OutputImageDir
break
}
}
}
output = append(output, imageResource)
}
}
if len(output) > 0 {
augmentedSteps := []corev1.Container{}
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, imageDigestExporterContainer(imagesJSON))
taskSpec.Steps = augmentedSteps
}
}
return nil
}
// UpdateTaskRunStatusWithResourceResult if there is an update to the outout image resource, add to taskrun status result
func UpdateTaskRunStatusWithResourceResult(taskRun *v1alpha1.TaskRun, logContent []byte) error {
err := json.Unmarshal(logContent, &taskRun.Status.ResourcesResult)
if err != nil {
return xerrors.Errorf("Failed to unmarshal output image exporter JSON output: %w", err)
}
return nil
}
func imageDigestExporterContainer(imagesJSON []byte) corev1.Container {
return corev1.Container{
Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("image-digest-exporter"),
Image: *imageDigestExporterImage,
Command: []string{"/ko-app/imagedigestexporter"},
Args: []string{
"-images", string(imagesJSON),
"-terminationMessagePath", TerminationMessagePath,
},
TerminationMessagePath: TerminationMessagePath,
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
}
}
// TaskRunHasOutputImageResource return true if the task has any output resources of type image
func TaskRunHasOutputImageResource(gr GetResource, taskRun *v1alpha1.TaskRun) bool {
if len(taskRun.Spec.Outputs.Resources) > 0 {
for _, r := range taskRun.Spec.Outputs.Resources {
resource, err := gr(r.ResourceRef.Name)
if err != nil {
return false
}
if resource.Spec.Type == v1alpha1.PipelineResourceTypeImage {
return true
}
}
}
return false
}