forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
35 lines (28 loc) · 888 Bytes
/
metadata.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
package stack
import "github.com/google/go-containerregistry/pkg/name"
type Metadata struct {
RunImage RunImageMetadata `toml:"run-image" json:"runImage"`
}
type RunImageMetadata struct {
Image string `toml:"image" json:"image"`
Mirrors []string `toml:"mirrors" json:"mirrors"`
}
func (m Metadata) GetBestMirror(desiredRegistry string, localMirrors []string) string {
runImageList := append(localMirrors, append([]string{m.RunImage.Image}, m.RunImage.Mirrors...)...)
for _, img := range runImageList {
if reg, err := registry(img); err == nil && reg == desiredRegistry {
return img
}
}
if len(localMirrors) > 0 {
return localMirrors[0]
}
return m.RunImage.Image
}
func registry(imageName string) (string, error) {
ref, err := name.ParseReference(imageName, name.WeakValidation)
if err != nil {
return "", err
}
return ref.Context().RegistryStr(), nil
}