-
Notifications
You must be signed in to change notification settings - Fork 8
/
remote.go
159 lines (127 loc) · 3.6 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
// 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"
"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"
)
var (
staticMapping = []keychainMap{}
UsePercentageInTracking = false
)
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 WriteRemoteOptsWithAuth(ctx context.Context, keychain Keychain) []remote.Option {
return []remote.Option{remote.WithContext(ctx), remote.WithAuthFromKeychain(keychainSequence{ctx, keychain, true})}
}
func ParseRefAndKeychain(ctx context.Context, imageRef string, opts ResolveOpts) (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 := []remote.Option{remote.WithContext(ctx)}
if !opts.PublicImage {
if opts.Keychain != nil {
options = append(options, remote.WithAuthFromKeychain(keychainSequence{ctx, opts.Keychain, false}))
} else {
options = append(options, remote.WithAuthFromKeychain(defaultKeychain{ctx, false}))
}
}
return ref, options, 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)
}