/
project_image.go
208 lines (187 loc) · 7.24 KB
/
project_image.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
package steps
import (
"context"
"encoding/json"
"fmt"
"path"
coreapi "k8s.io/api/core/v1"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
buildapi "github.com/openshift/api/build/v1"
"github.com/openshift/api/image/docker10"
imagev1 "github.com/openshift/api/image/v1"
"github.com/openshift/ci-tools/pkg/api"
"github.com/openshift/ci-tools/pkg/kubernetes"
"github.com/openshift/ci-tools/pkg/results"
"github.com/openshift/ci-tools/pkg/steps/utils"
)
type projectDirectoryImageBuildStep struct {
config api.ProjectDirectoryImageBuildStepConfiguration
releaseBuildConfig *api.ReleaseBuildConfiguration
resources api.ResourceConfiguration
client BuildClient
podClient kubernetes.PodClient
jobSpec *api.JobSpec
pullSecret *coreapi.Secret
}
func (s *projectDirectoryImageBuildStep) Inputs() (api.InputDefinition, error) {
return nil, nil
}
func (s *projectDirectoryImageBuildStep) Validate() error { return nil }
func (s *projectDirectoryImageBuildStep) Run(ctx context.Context) error {
return results.ForReason("building_project_image").ForError(s.run(ctx))
}
func (s *projectDirectoryImageBuildStep) run(ctx context.Context) error {
sourceTag, images, err := imagesFor(s.config, func(tag string) (string, error) {
return getWorkingDir(s.client, tag, s.jobSpec.Namespace())
}, s.releaseBuildConfig.IsBundleImage)
if err != nil {
return err
}
fromDigest, err := resolvePipelineImageStreamTagReference(ctx, s.client, sourceTag, s.jobSpec)
if err != nil {
return err
}
build := buildFromSource(
s.jobSpec, s.config.From, s.config.To,
buildapi.BuildSource{
Type: buildapi.BuildSourceImage,
Dockerfile: s.config.DockerfileLiteral,
Images: images,
},
fromDigest,
s.config.DockerfilePath,
s.resources,
s.pullSecret,
s.config.BuildArgs,
s.config.Ref,
)
return handleBuilds(ctx, s.client, s.podClient, *build)
}
type workingDir func(tag string) (string, error)
type isBundleImage func(tag string) bool
func imagesFor(config api.ProjectDirectoryImageBuildStepConfiguration, workingDir workingDir, isBundleImage isBundleImage) (api.PipelineImageStreamTagReference, []buildapi.ImageSource, error) {
images := buildInputsFromStep(config.Inputs)
var sourceTag string
var contextDir string
if isBundleImage(string(config.To)) {
// use the operator bundle source for bundle images
sourceTag = string(api.PipelineImageStreamTagReferenceBundleSource)
contextDir = config.ContextDir
} else if api.IsIndexImage(string(config.To)) {
// use the index source for index images
sourceTag = string(api.IndexGeneratorName(config.To))
} else {
// default to using the normal pipeline source image
sourceTag = string(api.PipelineImageStreamTagReferenceSource)
contextDir = config.ContextDir
}
if config.Ref != "" {
sourceTag = fmt.Sprintf("%s-%s", sourceTag, config.Ref)
}
if _, overwritten := config.Inputs[sourceTag]; !overwritten {
// if the user has not overwritten the source, we need to make sure it's mounted in
source := fmt.Sprintf("%s:%s", api.PipelineImageStream, sourceTag)
baseDir, err := workingDir(source)
if err != nil {
return "", nil, fmt.Errorf("failed to get workingDir: %w", err)
}
images = append(images, buildapi.ImageSource{
From: coreapi.ObjectReference{
Kind: "ImageStreamTag",
Name: source,
},
Paths: []buildapi.ImageSourcePath{{
SourcePath: fmt.Sprintf("%s/.", path.Join(baseDir, contextDir)),
DestinationDir: ".",
}},
})
}
return api.PipelineImageStreamTagReference(sourceTag), images, nil
}
func getWorkingDir(client ctrlruntimeclient.Client, source, namespace string) (string, error) {
ist := &imagev1.ImageStreamTag{}
if err := client.Get(context.TODO(), ctrlruntimeclient.ObjectKey{Namespace: namespace, Name: source}, ist); err != nil {
return "", fmt.Errorf("could not fetch source ImageStreamTag: %w", err)
}
image := ist.Image
// If the image contains a manifest list, the docker metadata are empty. Instead
// we need to grab the metadata from one of the images in manifest list.
if len(ist.Image.DockerImageManifests) > 0 {
img := &imagev1.Image{}
if err := client.Get(context.TODO(), ctrlruntimeclient.ObjectKey{Name: ist.Image.DockerImageManifests[0].Digest}, img); err != nil {
return "", fmt.Errorf("could not fetch source ImageStreamTag: %w", err)
}
image = *img
}
metadata := &docker10.DockerImage{}
if len(image.DockerImageMetadata.Raw) == 0 {
return "", fmt.Errorf("could not fetch Docker image metadata for ImageStreamTag %s", source)
}
if err := json.Unmarshal(image.DockerImageMetadata.Raw, metadata); err != nil {
return "", fmt.Errorf("malformed Docker image metadata on ImageStreamTag: %w", err)
}
return metadata.Config.WorkingDir, nil
}
func (s *projectDirectoryImageBuildStep) Requires() []api.StepLink {
source := string(api.PipelineImageStreamTagReferenceSource)
bundleSource := string(api.PipelineImageStreamTagReferenceBundleSource)
indexOutput := string(s.config.To)
if s.config.Ref != "" {
source = fmt.Sprintf("%s-%s", source, s.config.Ref)
bundleSource = fmt.Sprintf("%s-%s", bundleSource, s.config.Ref)
indexOutput = fmt.Sprintf("%s-%s", indexOutput, s.config.Ref)
}
links := []api.StepLink{
api.InternalImageLink(api.PipelineImageStreamTagReference(source)),
}
if len(s.config.From) > 0 {
links = append(links, api.InternalImageLink(s.config.From))
}
if s.releaseBuildConfig.IsBundleImage(string(s.config.To)) {
links = append(links, api.InternalImageLink(api.PipelineImageStreamTagReference(bundleSource)))
}
if api.IsIndexImage(string(s.config.To)) {
links = append(links, api.InternalImageLink(api.IndexGeneratorName(api.PipelineImageStreamTagReference(indexOutput))))
}
for name := range s.config.Inputs {
links = append(links, api.InternalImageLink(api.PipelineImageStreamTagReference(name), api.StepLinkWithUnsatisfiableErrorMessage(fmt.Sprintf("%q is neither an imported nor a built image", name))))
}
return links
}
func (s *projectDirectoryImageBuildStep) Creates() []api.StepLink {
return []api.StepLink{api.InternalImageLink(s.config.To)}
}
func (s *projectDirectoryImageBuildStep) Provides() api.ParameterMap {
if len(s.config.To) == 0 {
return nil
}
return api.ParameterMap{
utils.PipelineImageEnvFor(s.config.To): utils.ImageDigestFor(s.client, s.jobSpec.Namespace, api.PipelineImageStream, string(s.config.To)),
}
}
func (s *projectDirectoryImageBuildStep) Name() string { return s.config.TargetName() }
func (s *projectDirectoryImageBuildStep) Description() string {
return fmt.Sprintf("Build image %s from the repository", s.config.To)
}
func (s *projectDirectoryImageBuildStep) Objects() []ctrlruntimeclient.Object {
return s.client.Objects()
}
func ProjectDirectoryImageBuildStep(
config api.ProjectDirectoryImageBuildStepConfiguration,
releaseBuildConfig *api.ReleaseBuildConfiguration,
resources api.ResourceConfiguration,
buildClient BuildClient,
podClient kubernetes.PodClient,
jobSpec *api.JobSpec,
pullSecret *coreapi.Secret,
) api.Step {
return &projectDirectoryImageBuildStep{
config: config,
releaseBuildConfig: releaseBuildConfig,
resources: resources,
client: buildClient,
podClient: podClient,
jobSpec: jobSpec,
pullSecret: pullSecret,
}
}