-
Notifications
You must be signed in to change notification settings - Fork 9
/
remote.go
238 lines (191 loc) · 5.62 KB
/
remote.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package oci
import (
"context"
"fmt"
"net"
"net/url"
"os"
"strings"
"sync"
"github.com/dustin/go-humanize"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"golang.org/x/exp/maps"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/versions"
)
var (
staticMapping = []keychainMap{}
mirrorPortMap = map[string]string{
"http": "80",
"https": "443",
}
UsePercentageInTracking = false
)
func DockerHubMirror() string {
return os.Getenv("NS_DOCKERHUB_MIRROR")
}
type KeychainWhen int
const (
Keychain_UseAlways KeychainWhen = iota
Keychain_UseOnWrites
)
type keychainMap struct {
Domain string
Keychain Keychain
When KeychainWhen
}
func RegisterDomainKeychain(suffix string, keychain Keychain, purpose KeychainWhen) {
staticMapping = append(staticMapping, keychainMap{suffix, keychain, purpose})
}
func userAgent() remote.Option {
return remote.WithUserAgent(fmt.Sprintf("NamespaceCLI/%d", versions.Builtin().APIVersion))
}
func RemoteOptsWithAuth(ctx context.Context, access RegistryAccess, writeAccess bool) ([]remote.Option, error) {
options := []remote.Option{
remote.WithContext(ctx),
userAgent(),
}
if !access.PublicImage {
options = append(options, remote.WithAuthFromKeychain(keychainSequence{ctx, access.Keychain, writeAccess}))
}
transportOptions, err := parseTransport(ctx, access.Transport)
if err != nil {
return nil, err
}
options = append(options, transportOptions...)
return options, nil
}
func ParseRefAndKeychain(ctx context.Context, imageRef string, opts RegistryAccess) (name.Reference, []remote.Option, error) {
var nameOpts []name.Option
if opts.InsecureRegistry {
nameOpts = append(nameOpts, name.Insecure)
}
ref, err := name.ParseReference(imageRef, nameOpts...)
if err != nil {
return nil, nil, err
}
options, err := RemoteOptsWithAuth(ctx, opts, false)
if err != nil {
return nil, nil, err
}
ref, err = RefWithRegistryMirror(ctx, ref)
if err != nil {
return nil, nil, err
}
return ref, options, nil
}
func RefWithRegistryMirror(ctx context.Context, imageRef name.Reference) (name.Reference, error) {
// Check if image registry is `index.docker.io` and docker hub mirror is provided.
if imageRef.Context().RegistryStr() != name.DefaultRegistry || DockerHubMirror() == "" {
return imageRef, nil
}
mirrorURL, err := url.Parse(DockerHubMirror())
if err != nil {
return nil, err
}
defaultMirrorPort, ok := mirrorPortMap[mirrorURL.Scheme]
if !ok {
return nil, fnerrors.New("docker hub mirror scheme %q is not supported; supported values: %s",
mirrorURL.Scheme, strings.Join(maps.Keys(mirrorPortMap), ","))
}
var mirrorOpts []name.Option
if mirrorURL.Scheme == "http" {
mirrorOpts = append(mirrorOpts, name.Insecure)
}
mirrorHost := mirrorURL.Host
if mirrorURL.Port() == "" {
mirrorHost = net.JoinHostPort(mirrorHost, defaultMirrorPort)
}
fmt.Fprintf(console.Debug(ctx), "using mirror %q for registry %q\n", DockerHubMirror(), name.DefaultRegistry)
imageRepo := imageRef.Context()
// We create a new repository here to have full name of repository (e.g. including 'library').
mirrorRepo, err := name.NewRepository(fmt.Sprintf("%s/%s", mirrorHost, imageRepo.RepositoryStr()), mirrorOpts...)
if err != nil {
return nil, err
}
fmt.Fprintf(console.Debug(ctx), "using repository %q\n", mirrorRepo.Name())
return &imageReference{
Reference: imageRef,
repository: mirrorRepo,
}, nil
}
type keychainSequence struct {
ctx context.Context
provided Keychain
writing bool
}
func (ks keychainSequence) Resolve(resource authn.Resource) (authn.Authenticator, error) {
if ks.provided != nil {
res, err := ks.provided.Resolve(ks.ctx, resource)
if err != nil {
return nil, err
}
if res != nil {
return res, nil
}
}
return defaultKeychain{ks.ctx, ks.writing}.Resolve(resource)
}
type defaultKeychain struct {
ctx context.Context
writing bool
}
func (ks defaultKeychain) Resolve(resource authn.Resource) (authn.Authenticator, error) {
for _, kc := range staticMapping {
if resource.RegistryStr() == kc.Domain || strings.HasSuffix(resource.RegistryStr(), "."+kc.Domain) {
if kc.When == Keychain_UseAlways || (kc.When == Keychain_UseOnWrites && ks.writing) {
return kc.Keychain.Resolve(ks.ctx, resource)
}
}
}
return authn.DefaultKeychain.Resolve(resource)
}
type RemoteProgress struct {
mu sync.RWMutex
tracking bool
complete, total int64
}
func (rp *RemoteProgress) FormatProgress() string {
rp.mu.RLock()
tracking := rp.tracking
complete, total := rp.complete, rp.total
rp.mu.RUnlock()
if !tracking {
return ""
}
if UsePercentageInTracking {
var percent float64
if total == 0 {
percent = 0
} else {
percent = float64(complete) / float64(total) * 100
}
return fmt.Sprintf("%.2f%%", percent)
}
if total == 0 {
return humanize.Bytes(uint64(complete))
}
return fmt.Sprintf("%s / %s", humanize.Bytes(uint64(complete)), humanize.Bytes(uint64(total)))
}
func (rp *RemoteProgress) Track() remote.Option {
rp.mu.Lock()
rp.tracking = true
rp.mu.Unlock()
ch := make(chan v1.Update, 1)
go func() {
for update := range ch {
rp.mu.Lock()
rp.complete = update.Complete
rp.total = update.Total
rp.mu.Unlock()
}
}()
return remote.WithProgress(ch)
}