forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
77 lines (66 loc) · 1.97 KB
/
common.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
package pack
import (
"errors"
"fmt"
"github.com/google/go-containerregistry/pkg/name"
"github.com/buildpack/pack/builder"
"github.com/buildpack/pack/style"
)
func (c *Client) parseTagReference(imageName string) (name.Reference, error) {
if imageName == "" {
return nil, errors.New("image is a required parameter")
}
if _, err := name.ParseReference(imageName, name.WeakValidation); err != nil {
return nil, err
}
ref, err := name.NewTag(imageName, name.WeakValidation)
if err != nil {
return nil, fmt.Errorf("'%s' is not a tag reference", imageName)
}
return ref, nil
}
func (c *Client) resolveRunImage(runImage, targetRegistry string, stackInfo builder.StackMetadata, additionalMirrors map[string][]string) string {
if runImage != "" {
c.logger.Debugf("Using provided run-image %s", style.Symbol(runImage))
return runImage
}
runImageName := getBestRunMirror(
targetRegistry,
stackInfo.RunImage.Image,
stackInfo.RunImage.Mirrors,
additionalMirrors[stackInfo.RunImage.Image],
)
switch {
case runImageName == stackInfo.RunImage.Image:
c.logger.Debugf("Selected run image %s", style.Symbol(runImageName))
case contains(stackInfo.RunImage.Mirrors, runImageName):
c.logger.Debugf("Selected run image mirror %s", style.Symbol(runImageName))
default:
c.logger.Debugf("Selected run image mirror %s from local config", style.Symbol(runImageName))
}
return runImageName
}
func contains(slc []string, v string) bool {
for _, s := range slc {
if s == v {
return true
}
}
return false
}
func getBestRunMirror(registry string, runImage string, mirrors []string, preferredMirrors []string) string {
runImageList := append(preferredMirrors, append([]string{runImage}, mirrors...)...)
for _, img := range runImageList {
ref, err := name.ParseReference(img, name.WeakValidation)
if err != nil {
continue
}
if ref.Context().RegistryStr() == registry {
return img
}
}
if len(preferredMirrors) > 0 {
return preferredMirrors[0]
}
return runImage
}