-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
gar.go
236 lines (190 loc) · 5.91 KB
/
gar.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
package registry
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"math/rand"
"os/exec"
"strings"
"time"
artifactregistry "cloud.google.com/go/artifactregistry/apiv1"
"github.com/containers/image/v5/docker/reference"
ctypes "github.com/containers/image/v5/types"
"github.com/dgraph-io/ristretto"
"github.com/estahn/k8s-image-swapper/pkg/config"
"github.com/go-co-op/gocron"
"google.golang.org/api/option"
"google.golang.org/api/transport"
"github.com/rs/zerolog/log"
)
type GARAPI interface{}
type GARClient struct {
client GARAPI
garDomain string
cache *ristretto.Cache
scheduler *gocron.Scheduler
authToken []byte
}
func NewGARClient(clientConfig config.GCP) (*GARClient, error) {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
if err != nil {
panic(err)
}
scheduler := gocron.NewScheduler(time.UTC)
scheduler.StartAsync()
client := &GARClient{
client: nil,
garDomain: clientConfig.GarDomain(),
cache: cache,
scheduler: scheduler,
}
if err := client.scheduleTokenRenewal(); err != nil {
return nil, err
}
return client, nil
}
// CreateRepository is empty since repositories are not created for artifact registry
func (e *GARClient) CreateRepository(ctx context.Context, name string) error {
return nil
}
func (e *GARClient) RepositoryExists() bool {
panic("implement me")
}
func (e *GARClient) CopyImage(ctx context.Context, srcRef ctypes.ImageReference, srcCreds string, destRef ctypes.ImageReference, destCreds string) error {
src := srcRef.DockerReference().String()
dest := destRef.DockerReference().String()
creds := []string{"--src-authfile", srcCreds}
// use client credentials for any source GAR repositories
if strings.HasSuffix(reference.Domain(srcRef.DockerReference()), "-docker.pkg.dev") {
creds = []string{"--src-creds", e.Credentials()}
}
app := "skopeo"
args := []string{
"--override-os", "linux",
"copy",
"--multi-arch", "all",
"--retry-times", "3",
"docker://" + src,
"docker://" + dest,
}
if len(creds[1]) > 0 {
args = append(args, creds...)
} else {
args = append(args, "--src-no-creds")
}
if len(destCreds) > 0 {
args = append(args, "--dest-creds", destCreds)
} else {
args = append(args, "--dest-no-creds")
}
log.Ctx(ctx).
Trace().
Str("app", app).
Strs("args", args).
Msg("execute command to copy image")
output, cmdErr := exec.CommandContext(ctx, app, args...).CombinedOutput()
// check if the command timed out during execution for proper logging
if err := ctx.Err(); err != nil {
return err
}
// enrich error with output from the command which may contain the actual reason
if cmdErr != nil {
return fmt.Errorf("Command error, stderr: %s, stdout: %s", cmdErr.Error(), string(output))
}
return nil
}
func (e *GARClient) PullImage() error {
panic("implement me")
}
func (e *GARClient) PutImage() error {
panic("implement me")
}
func (e *GARClient) ImageExists(ctx context.Context, imageRef ctypes.ImageReference) bool {
ref := imageRef.DockerReference().String()
if _, found := e.cache.Get(ref); found {
log.Ctx(ctx).Trace().Str("ref", ref).Msg("found in cache")
return true
}
app := "skopeo"
args := []string{
"inspect",
"--retry-times", "3",
"docker://" + ref,
"--creds", e.Credentials(),
}
log.Ctx(ctx).Trace().Str("app", app).Strs("args", args).Msg("executing command to inspect image")
if err := exec.CommandContext(ctx, app, args...).Run(); err != nil {
log.Trace().Str("ref", ref).Msg("not found in target repository")
return false
}
log.Ctx(ctx).Trace().Str("ref", ref).Msg("found in target repository")
e.cache.SetWithTTL(ref, "", 1, 24*time.Hour+time.Duration(rand.Intn(180))*time.Minute)
return true
}
func (e *GARClient) Endpoint() string {
return e.garDomain
}
// IsOrigin returns true if the references origin is from this registry
func (e *GARClient) IsOrigin(imageRef ctypes.ImageReference) bool {
return strings.HasPrefix(imageRef.DockerReference().String(), e.Endpoint())
}
// requestAuthToken requests and returns an authentication token from GAR with its expiration date
func (e *GARClient) requestAuthToken() ([]byte, time.Time, error) {
ctx := context.Background()
creds, err := transport.Creds(ctx, option.WithScopes(artifactregistry.DefaultAuthScopes()...))
if err != nil {
log.Err(err).Msg("generating gcp creds")
return []byte(""), time.Time{}, err
}
token, err := creds.TokenSource.Token()
if err != nil {
log.Err(err).Msg("generating token")
return []byte(""), time.Time{}, err
}
return []byte(fmt.Sprintf("oauth2accesstoken:%v", token.AccessToken)), token.Expiry, nil
}
// scheduleTokenRenewal sets a scheduler to execute token renewal before the token expires
func (e *GARClient) scheduleTokenRenewal() error {
token, expiryAt, err := e.requestAuthToken()
if err != nil {
return err
}
renewalAt := expiryAt.Add(-2 * time.Minute)
e.authToken = token
log.Debug().Time("expiryAt", expiryAt).Time("renewalAt", renewalAt).Msg("auth token set, schedule next token renewal")
j, _ := e.scheduler.Every(1).StartAt(renewalAt).Do(e.scheduleTokenRenewal)
j.LimitRunsTo(1)
return nil
}
func (e *GARClient) Credentials() string {
return string(e.authToken)
}
func (e *GARClient) DockerConfig() ([]byte, error) {
dockerConfig := DockerConfig{
AuthConfigs: map[string]AuthConfig{
e.garDomain: {
Auth: base64.StdEncoding.EncodeToString(e.authToken),
},
},
}
dockerConfigJson, err := json.Marshal(dockerConfig)
if err != nil {
return []byte{}, err
}
return dockerConfigJson, nil
}
func NewMockGARClient(garClient GARAPI, garDomain string) (*GARClient, error) {
client := &GARClient{
client: garClient,
garDomain: garDomain,
cache: nil,
scheduler: nil,
authToken: []byte("oauth2accesstoken:mock-gar-client-fake-auth-token"),
}
return client, nil
}