/
image_tree.go
404 lines (323 loc) · 12.4 KB
/
image_tree.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
package image
import (
"context"
"fmt"
"path"
"path/filepath"
"reflect"
"sort"
"github.com/gookit/color"
"github.com/werf/logboek"
"github.com/werf/logboek/pkg/types"
"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/git_repo"
"github.com/werf/werf/pkg/giterminism_manager"
"github.com/werf/werf/pkg/logging"
"github.com/werf/werf/pkg/util"
)
type ImagesTree struct {
ImagesTreeOptions
werfConfig *config.WerfConfig
allImages []*Image
imagesSets ImagesSets
multiplatformImages []*MultiplatformImage
}
type ImagesTreeOptions struct {
CommonImageOptions
OnlyImages []string
WithoutImages bool
}
func NewImagesTree(werfConfig *config.WerfConfig, opts ImagesTreeOptions) *ImagesTree {
return &ImagesTree{
ImagesTreeOptions: opts,
werfConfig: werfConfig,
}
}
func (tree *ImagesTree) Calculate(ctx context.Context) error {
imageConfigSets, err := tree.werfConfig.GroupImagesByIndependentSets(tree.OnlyImages, tree.WithoutImages)
if err != nil {
return fmt.Errorf("unable to group werf config images by independent sets: %w", err)
}
targetPlatforms, err := tree.Conveyor.GetTargetPlatforms()
if err != nil {
return fmt.Errorf("invalid target platforms: %w", err)
}
if len(targetPlatforms) == 0 {
targetPlatforms = []string{tree.ContainerBackend.GetDefaultPlatform()}
}
commonImageOpts := tree.CommonImageOptions
commonImageOpts.ForceTargetPlatformLogging = (len(targetPlatforms) > 1)
builder := NewImagesSetsBuilder()
for _, iteration := range imageConfigSets {
for _, imageConfigI := range iteration {
for _, targetPlatform := range targetPlatforms {
var imageLogName string
var style color.Style
switch imageConfig := imageConfigI.(type) {
case config.StapelImageInterface:
imageLogName = logging.ImageLogProcessName(imageConfig.ImageBaseConfig().Name, imageConfig.IsArtifact(), targetPlatform)
style = ImageLogProcessStyle(imageConfig.IsArtifact())
case *config.ImageFromDockerfile:
imageLogName = logging.ImageLogProcessName(imageConfig.Name, false, targetPlatform)
style = ImageLogProcessStyle(false)
}
err := logboek.Context(ctx).Info().LogProcess(imageLogName).
Options(func(options types.LogProcessOptionsInterface) {
options.Style(style)
}).
DoError(func() error {
var err error
var newImagesSets ImagesSets
switch imageConfig := imageConfigI.(type) {
case config.StapelImageInterface:
newImagesSets, err = MapStapelConfigToImagesSets(ctx, tree.werfConfig.Meta, imageConfig, targetPlatform, commonImageOpts)
if err != nil {
return fmt.Errorf("unable to map stapel config to images sets: %w", err)
}
case *config.ImageFromDockerfile:
newImagesSets, err = MapDockerfileConfigToImagesSets(ctx, imageConfig, targetPlatform, commonImageOpts)
if err != nil {
return fmt.Errorf("unable to map dockerfile to images sets: %w", err)
}
}
builder.MergeImagesSets(newImagesSets)
return nil
})
if err != nil {
return err
}
}
}
builder.Next()
}
tree.imagesSets = builder.GetImagesSets()
tree.allImages = builder.GetAllImages()
return nil
}
func (tree *ImagesTree) GetImage(name string) *Image {
return nil
}
func (tree *ImagesTree) GetImagesByName(finalOnly bool) []*util.Pair[string, []*Image] {
images := make(map[string]map[string]*Image)
var names []string
appendImage := func(img *Image) {
names = util.UniqAppendString(names, img.Name)
if images[img.Name] == nil {
images[img.Name] = make(map[string]*Image)
}
images[img.Name][img.TargetPlatform] = img
}
for _, img := range tree.GetImages() {
if finalOnly {
for _, finalImageName := range tree.werfConfig.GetAllImages() {
if finalImageName.GetName() == img.Name {
appendImage(img)
}
}
} else {
appendImage(img)
}
}
var res []*util.Pair[string, []*Image]
sort.Strings(names)
for _, name := range names {
platforms := util.MapKeys(images[name])
sort.Strings(platforms)
allPlatformsImages := util.NewPair(name, make([]*Image, 0, len(platforms)))
for _, platform := range platforms {
allPlatformsImages.Second = append(allPlatformsImages.Second, images[name][platform])
}
res = append(res, allPlatformsImages)
}
return res
}
func (tree *ImagesTree) GetImagePlatformsByName(finalOnly bool) map[string][]string {
res := make(map[string][]string)
for _, img := range tree.GetImages() {
if finalOnly {
for _, finalImageName := range tree.werfConfig.GetAllImages() {
if finalImageName.GetName() == img.Name {
res[img.Name] = append(res[img.Name], img.TargetPlatform)
}
}
} else {
res[img.Name] = append(res[img.Name], img.TargetPlatform)
}
}
return res
}
func (tree *ImagesTree) GetImages() []*Image {
return tree.allImages
}
func (tree *ImagesTree) GetImagesSets() ImagesSets {
return tree.imagesSets
}
func (tree *ImagesTree) GetMultiplatformImage(name string) *MultiplatformImage {
for _, img := range tree.multiplatformImages {
if img.Name == name {
return img
}
}
return nil
}
func (tree *ImagesTree) SetMultiplatformImage(newImg *MultiplatformImage) {
for _, img := range tree.multiplatformImages {
if img.Name == newImg.Name {
return
}
}
tree.multiplatformImages = append(tree.multiplatformImages, newImg)
}
func getFromFields(imageBaseConfig *config.StapelImageBase) (string, string, bool) {
var from string
var fromImageName string
switch {
case imageBaseConfig.From != "":
from = imageBaseConfig.From
case imageBaseConfig.FromImageName != "":
fromImageName = imageBaseConfig.FromImageName
case imageBaseConfig.FromArtifactName != "":
fromImageName = imageBaseConfig.FromArtifactName
}
return from, fromImageName, imageBaseConfig.FromLatest
}
func filterAndLogGitMappings(ctx context.Context, gitMappings []*stage.GitMapping, conveyor Conveyor) ([]*stage.GitMapping, error) {
var res []*stage.GitMapping
for ind, gitMapping := range gitMappings {
if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("[%d] git mapping from %s repository", ind, gitMapping.Name)).DoError(func() error {
withTripleIndent := func(f func()) {
if logboek.Context(ctx).Info().IsAccepted() {
logboek.Context(ctx).Streams().IncreaseIndent()
logboek.Context(ctx).Streams().IncreaseIndent()
logboek.Context(ctx).Streams().IncreaseIndent()
}
f()
if logboek.Context(ctx).Info().IsAccepted() {
logboek.Context(ctx).Streams().DecreaseIndent()
logboek.Context(ctx).Streams().DecreaseIndent()
logboek.Context(ctx).Streams().DecreaseIndent()
}
}
withTripleIndent(func() {
logboek.Context(ctx).Info().LogFDetails("add: %s\n", gitMapping.Add)
logboek.Context(ctx).Info().LogFDetails("to: %s\n", gitMapping.To)
if len(gitMapping.IncludePaths) != 0 {
logboek.Context(ctx).Info().LogFDetails("includePaths: %+v\n", gitMapping.IncludePaths)
}
if len(gitMapping.ExcludePaths) != 0 {
logboek.Context(ctx).Info().LogFDetails("excludePaths: %+v\n", gitMapping.ExcludePaths)
}
if gitMapping.Commit != "" {
logboek.Context(ctx).Info().LogFDetails("commit: %s\n", gitMapping.Commit)
}
if gitMapping.Branch != "" {
logboek.Context(ctx).Info().LogFDetails("branch: %s\n", gitMapping.Branch)
}
if gitMapping.Owner != "" {
logboek.Context(ctx).Info().LogFDetails("owner: %s\n", gitMapping.Owner)
}
if gitMapping.Group != "" {
logboek.Context(ctx).Info().LogFDetails("group: %s\n", gitMapping.Group)
}
if len(gitMapping.StagesDependencies) != 0 {
logboek.Context(ctx).Info().LogLnDetails("stageDependencies:")
for s, values := range gitMapping.StagesDependencies {
if len(values) != 0 {
logboek.Context(ctx).Info().LogFDetails(" %s: %v\n", s, values)
}
}
}
})
logboek.Context(ctx).Info().LogLn()
commitInfo, err := gitMapping.GetLatestCommitInfo(ctx, conveyor)
if err != nil {
return fmt.Errorf("unable to get commit of repo %q: %w", gitMapping.GitRepo().GetName(), err)
}
if commitInfo.VirtualMerge {
logboek.Context(ctx).Info().LogFDetails("Commit %s will be used (virtual merge of %s into %s)\n", commitInfo.Commit, commitInfo.VirtualMergeFromCommit, commitInfo.VirtualMergeIntoCommit)
} else {
logboek.Context(ctx).Info().LogFDetails("Commit %s will be used\n", commitInfo.Commit)
}
res = append(res, gitMapping)
return nil
}); err != nil {
return nil, err
}
}
return res, nil
}
func appendIfExist(ctx context.Context, stages []stage.Interface, stage stage.Interface) []stage.Interface {
if !reflect.ValueOf(stage).IsNil() {
logboek.Context(ctx).Info().LogFDetails("Using stage %s\n", stage.Name())
return append(stages, stage)
}
return stages
}
func gitRemoteArtifactInit(ctx context.Context, remoteGitMappingConfig *config.GitRemote, remoteGitRepo *git_repo.Remote, imageName string, conveyor Conveyor, containerWerfDir, tmpDir string) (*stage.GitMapping, error) {
gitMapping := baseGitMappingInit(remoteGitMappingConfig.GitLocalExport, imageName, conveyor, containerWerfDir, tmpDir)
gitMapping.Tag = remoteGitMappingConfig.Tag
gitMapping.Commit = remoteGitMappingConfig.Commit
gitMapping.Branch = remoteGitMappingConfig.Branch
gitMapping.Name = remoteGitMappingConfig.Name
gitMapping.SetGitRepo(remoteGitRepo)
gitMappingTo, err := makeGitMappingTo(ctx, gitMapping, remoteGitMappingConfig.GitLocalExport.GitMappingTo(), conveyor)
if err != nil {
return nil, fmt.Errorf("unable to make remote git.to mapping for image %q: %w", imageName, err)
}
gitMapping.To = gitMappingTo
return gitMapping, nil
}
func gitLocalPathInit(ctx context.Context, localGitMappingConfig *config.GitLocal, imageName string, conveyor Conveyor, giterminismManager giterminism_manager.Interface, containerWerfDir, tmpDir string) (*stage.GitMapping, error) {
gitMapping := baseGitMappingInit(localGitMappingConfig.GitLocalExport, imageName, conveyor, containerWerfDir, tmpDir)
gitMapping.Name = "own"
gitMapping.SetGitRepo(giterminismManager.LocalGitRepo())
gitMappingTo, err := makeGitMappingTo(ctx, gitMapping, localGitMappingConfig.GitLocalExport.GitMappingTo(), conveyor)
if err != nil {
return nil, fmt.Errorf("unable to make local git.to mapping for image %q: %w", imageName, err)
}
gitMapping.To = gitMappingTo
return gitMapping, nil
}
func baseGitMappingInit(local *config.GitLocalExport, imageName string, conveyor Conveyor, containerWerfDir, tmpDir string) *stage.GitMapping {
var stageDependencies map[stage.StageName][]string
if local.StageDependencies != nil {
stageDependencies = stageDependenciesToMap(local.StageDependencies)
}
gitMapping := stage.NewGitMapping()
gitMapping.ContainerPatchesDir = path.Join(containerWerfDir, "patch")
gitMapping.ContainerArchivesDir = path.Join(containerWerfDir, "archive")
gitMapping.ScriptsDir = filepath.Join(tmpDir, imageName, "scripts")
gitMapping.ContainerScriptsDir = path.Join(containerWerfDir, "scripts")
gitMapping.Add = local.GitMappingAdd()
gitMapping.ExcludePaths = local.ExcludePaths
gitMapping.IncludePaths = local.IncludePaths
gitMapping.Owner = local.Owner
gitMapping.Group = local.Group
gitMapping.StagesDependencies = stageDependencies
return gitMapping
}
func makeGitMappingTo(ctx context.Context, gitMapping *stage.GitMapping, gitMappingTo string, conveyor Conveyor) (string, error) {
if gitMappingTo != "/" {
return gitMappingTo, nil
}
gitRepoName := gitMapping.GitRepo().GetName()
commitInfo, err := gitMapping.GetLatestCommitInfo(ctx, conveyor)
if err != nil {
return "", fmt.Errorf("unable to get latest commit info for repo %q: %w", gitRepoName, err)
}
if gitMappingAddIsDir, err := gitMapping.GitRepo().IsCommitTreeEntryDirectory(ctx, commitInfo.Commit, gitMapping.Add); err != nil {
return "", fmt.Errorf("unable to determine whether git `add: %s` is dir or file for repo %q: %w", gitMapping.Add, gitRepoName, err)
} else if !gitMappingAddIsDir {
return "", fmt.Errorf("for git repo %q specifying `to: /` when adding a single file from git with `add: %s` is not allowed. Fix this by changing `to: /` to `to: /%s`.", gitRepoName, gitMapping.Add, filepath.Base(gitMapping.Add))
}
return gitMappingTo, nil
}
func stageDependenciesToMap(sd *config.StageDependencies) map[stage.StageName][]string {
result := map[stage.StageName][]string{
stage.Install: sd.Install,
stage.BeforeSetup: sd.BeforeSetup,
stage.Setup: sd.Setup,
}
return result
}