forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
127 lines (106 loc) · 3.16 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
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
package pack
import (
"errors"
"fmt"
"github.com/google/go-containerregistry/pkg/name"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/registry"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/logging"
)
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, imgRegistry, bldrRegistry string, stackInfo builder.StackMetadata, additionalMirrors map[string][]string, publish bool) string {
if runImage != "" {
c.logger.Debugf("Using provided run-image %s", style.Symbol(runImage))
return runImage
}
preferredRegistry := bldrRegistry
if publish || bldrRegistry == "" {
preferredRegistry = imgRegistry
}
runImageName := getBestRunMirror(
preferredRegistry,
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 (c *Client) getRegistry(logger logging.Logger, registryName string) (registry.Cache, error) {
home, err := config.PackHome()
if err != nil {
return registry.Cache{}, err
}
if err := config.MkdirAll(home); err != nil {
return registry.Cache{}, err
}
cfg, err := getConfig()
if err != nil {
return registry.Cache{}, err
}
if registryName == "" {
return registry.NewDefaultRegistryCache(logger, home)
}
for _, reg := range config.GetRegistries(cfg) {
if reg.Name == registryName {
return registry.NewRegistryCache(logger, home, reg.URL)
}
}
return registry.Cache{}, fmt.Errorf("registry %s is not defined in your config file", style.Symbol(registryName))
}
func getConfig() (config.Config, error) {
path, err := config.DefaultConfigPath()
if err != nil {
return config.Config{}, err
}
cfg, err := config.Read(path)
if err != nil {
return config.Config{}, err
}
return cfg, nil
}
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
}