-
Notifications
You must be signed in to change notification settings - Fork 289
/
fetcher.go
187 lines (153 loc) · 4.64 KB
/
fetcher.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
package image
import (
"context"
"encoding/base64"
"encoding/json"
"io"
"strings"
"github.com/buildpacks/imgutil"
"github.com/buildpacks/imgutil/local"
"github.com/buildpacks/imgutil/remote"
"github.com/buildpacks/lifecycle/auth"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/pkg/errors"
pname "github.com/buildpacks/pack/internal/name"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/internal/term"
"github.com/buildpacks/pack/pkg/logging"
)
// FetcherOption is a type of function that mutate settings on the client.
// Values in these functions are set through currying.
type FetcherOption func(c *Fetcher)
// WithRegistryMirrors supply your own mirrors for registry.
func WithRegistryMirrors(registryMirrors map[string]string) FetcherOption {
return func(c *Fetcher) {
c.registryMirrors = registryMirrors
}
}
type Fetcher struct {
docker client.CommonAPIClient
logger logging.Logger
registryMirrors map[string]string
}
type FetchOptions struct {
Daemon bool
Platform string
PullPolicy PullPolicy
}
func NewFetcher(logger logging.Logger, docker client.CommonAPIClient, opts ...FetcherOption) *Fetcher {
var fetcher = &Fetcher{
logger: logger,
docker: docker,
}
for _, opt := range opts {
opt(fetcher)
}
return fetcher
}
var ErrNotFound = errors.New("not found")
func (f *Fetcher) Fetch(ctx context.Context, name string, options FetchOptions) (imgutil.Image, error) {
name, err := pname.TranslateRegistry(name, f.registryMirrors, f.logger)
if err != nil {
return nil, err
}
if !options.Daemon {
return f.fetchRemoteImage(name)
}
switch options.PullPolicy {
case PullNever:
img, err := f.fetchDaemonImage(name)
return img, err
case PullIfNotPresent:
img, err := f.fetchDaemonImage(name)
if err == nil || !errors.Is(err, ErrNotFound) {
return img, err
}
}
f.logger.Debugf("Pulling image %s", style.Symbol(name))
err = f.pullImage(ctx, name, options.Platform)
if err != nil && !errors.Is(err, ErrNotFound) {
return nil, err
}
return f.fetchDaemonImage(name)
}
func (f *Fetcher) fetchDaemonImage(name string) (imgutil.Image, error) {
image, err := local.NewImage(name, f.docker, local.FromBaseImage(name))
if err != nil {
return nil, err
}
if !image.Found() {
return nil, errors.Wrapf(ErrNotFound, "image %s does not exist on the daemon", style.Symbol(name))
}
return image, nil
}
func (f *Fetcher) fetchRemoteImage(name string) (imgutil.Image, error) {
image, err := remote.NewImage(name, authn.DefaultKeychain, remote.FromBaseImage(name))
if err != nil {
return nil, err
}
if !image.Found() {
return nil, errors.Wrapf(ErrNotFound, "image %s does not exist in registry", style.Symbol(name))
}
return image, nil
}
func (f *Fetcher) pullImage(ctx context.Context, imageID string, platform string) error {
regAuth, err := registryAuth(imageID)
if err != nil {
return err
}
rc, err := f.docker.ImagePull(ctx, imageID, types.ImagePullOptions{RegistryAuth: regAuth, Platform: platform})
if err != nil {
if client.IsErrNotFound(err) {
return errors.Wrapf(ErrNotFound, "image %s does not exist on the daemon", style.Symbol(imageID))
}
return err
}
writer := logging.GetWriterForLevel(f.logger, logging.InfoLevel)
termFd, isTerm := term.IsTerminal(writer)
err = jsonmessage.DisplayJSONMessagesStream(rc, &colorizedWriter{writer}, termFd, isTerm, nil)
if err != nil {
return err
}
return rc.Close()
}
func registryAuth(ref string) (string, error) {
_, a, err := auth.ReferenceForRepoName(authn.DefaultKeychain, ref)
if err != nil {
return "", errors.Wrapf(err, "resolve auth for ref %s", ref)
}
authConfig, err := a.Authorization()
if err != nil {
return "", err
}
dataJSON, err := json.Marshal(authConfig)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(dataJSON), nil
}
type colorizedWriter struct {
writer io.Writer
}
type colorFunc = func(string, ...interface{}) string
func (w *colorizedWriter) Write(p []byte) (n int, err error) {
msg := string(p)
colorizers := map[string]colorFunc{
"Waiting": style.Waiting,
"Pulling fs layer": style.Waiting,
"Downloading": style.Working,
"Download complete": style.Working,
"Extracting": style.Working,
"Pull complete": style.Complete,
"Already exists": style.Complete,
"=": style.ProgressBar,
">": style.ProgressBar,
}
for pattern, colorize := range colorizers {
msg = strings.ReplaceAll(msg, pattern, colorize(pattern))
}
return w.writer.Write([]byte(msg))
}