-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathfetcher.go
248 lines (203 loc) · 6.02 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
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
package image
import (
"context"
"encoding/base64"
"encoding/json"
"io"
"strings"
"github.com/buildpacks/imgutil/layout"
"github.com/buildpacks/imgutil/layout/sparse"
"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)
type LayoutOption struct {
Path string
Sparse bool
}
// WithRegistryMirrors supply your own mirrors for registry.
func WithRegistryMirrors(registryMirrors map[string]string) FetcherOption {
return func(c *Fetcher) {
c.registryMirrors = registryMirrors
}
}
func WithKeychain(keychain authn.Keychain) FetcherOption {
return func(c *Fetcher) {
c.keychain = keychain
}
}
type DockerClient interface {
local.DockerClient
ImagePull(ctx context.Context, ref string, options types.ImagePullOptions) (io.ReadCloser, error)
}
type Fetcher struct {
docker DockerClient
logger logging.Logger
registryMirrors map[string]string
keychain authn.Keychain
}
type FetchOptions struct {
Daemon bool
Platform string
PullPolicy PullPolicy
LayoutOption LayoutOption
}
func NewFetcher(logger logging.Logger, docker DockerClient, opts ...FetcherOption) *Fetcher {
fetcher := &Fetcher{
logger: logger,
docker: docker,
keychain: authn.DefaultKeychain,
}
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.LayoutOption != LayoutOption{}) {
return f.fetchLayoutImage(name, options.LayoutOption)
}
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))
if err = f.pullImage(ctx, name, options.Platform); err != nil {
// sample error from docker engine:
// image with reference <image> was found but does not match the specified platform: wanted linux/amd64, actual: linux
if strings.Contains(err.Error(), "does not match the specified platform") {
err = f.pullImage(ctx, name, "")
}
}
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, f.keychain, 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) fetchLayoutImage(name string, options LayoutOption) (imgutil.Image, error) {
var (
image imgutil.Image
err error
)
v1Image, err := remote.NewV1Image(name, f.keychain)
if err != nil {
return nil, err
}
if options.Sparse {
image, err = sparse.NewImage(options.Path, v1Image)
} else {
image, err = layout.NewImage(options.Path, layout.FromBaseImage(v1Image))
}
if err != nil {
return nil, err
}
err = image.Save()
if err != nil {
return nil, err
}
return image, nil
}
func (f *Fetcher) pullImage(ctx context.Context, imageID string, platform string) error {
regAuth, err := f.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 (f *Fetcher) registryAuth(ref string) (string, error) {
_, a, err := auth.ReferenceForRepoName(f.keychain, 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))
}