-
Notifications
You must be signed in to change notification settings - Fork 1
/
print.go
386 lines (345 loc) · 8.93 KB
/
print.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"slices"
"sort"
"strconv"
"strings"
"sync"
"github.com/ricardobranco777/regview/oci"
"github.com/ricardobranco777/regview/registry"
"github.com/ricardobranco777/regview/repoutils"
concurrently "github.com/tejzpr/ordered-concurrently/v3"
)
type loadWorker struct {
reg *registry.Registry
repo string
}
var maxWorkers = 10
func (w *loadWorker) Run(ctx context.Context) any {
tags, err := w.reg.Tags(ctx, w.repo)
if err != nil {
log.Printf("%s: %v\n", w.repo, err)
return []*registry.Info{}
}
tags = filterRegex(tags, tagRegex, false)
tags = filterRegex(tags, ignoreTags, true)
sort.Strings(tags)
tag2Infos := make(map[string][]*registry.Info)
var wg sync.WaitGroup
var m sync.Mutex
wg.Add(len(tags))
for _, tag := range tags {
go func(tag string) {
defer wg.Done()
infos, err := getInfos(ctx, w.reg, w.repo, tag)
if err != nil {
// Ignore this error that can happen when manifests may be available but not for this platform
if err.Error() != "MANIFEST_UNKNOWN" {
log.Printf("%s:%s: %v\n", w.repo, tag, err)
}
return
}
m.Lock()
tag2Infos[tag] = infos
m.Unlock()
}(tag)
}
wg.Wait()
var xinfos []*registry.Info
for _, tag := range tags {
if infos, ok := tag2Infos[tag]; ok {
xinfos = append(xinfos, infos...)
}
}
if !opts.all && !opts.verbose {
return xinfos
}
id2Blob := make(map[string]*oci.Image)
for _, infos := range tag2Infos {
for _, info := range infos {
id2Blob[info.ID] = nil
}
}
wg.Add(len(id2Blob))
for id := range id2Blob {
go func(id string) {
defer wg.Done()
blob, err := w.reg.GetImage(ctx, w.repo, id)
if err != nil {
log.Printf("%s@%s: %v\n", w.repo, id, err)
return
}
m.Lock()
id2Blob[id] = blob
m.Unlock()
}(id)
}
wg.Wait()
for _, info := range xinfos {
info.Image = id2Blob[info.ID]
}
return xinfos
}
func createRegistryClient(ctx context.Context, domain string) (*registry.Registry, error) {
auth, err := repoutils.GetAuthConfig(opts.username, opts.password, domain)
if err != nil {
return nil, err
}
// Prevent non-ssl unless explicitly forced
if !opts.insecure && strings.HasPrefix(auth.ServerAddress, "http:") {
return nil, fmt.Errorf("attempted to use insecure protocol! Use --insecure option to force")
}
return registry.New(ctx, auth, registry.Opt{
CAFile: opts.cacert,
CertFile: opts.cert,
KeyFile: opts.key,
Debug: opts.debug,
Digests: opts.digests,
Domain: domain,
Insecure: opts.insecure,
NonSSL: opts.insecure,
Passphrase: opts.keypass,
})
}
func getInfos(ctx context.Context, r *registry.Registry, repo string, ref string) (infos []*registry.Info, err error) {
infos, err = r.GetInfoAll(ctx, repo, ref, opts.arch, opts.os)
if err != nil {
return []*registry.Info{}, err
}
return infos, nil
}
func deleteImage(ctx context.Context, domain string, image string) {
r, err := createRegistryClient(ctx, domain)
if err != nil {
log.Fatal(err)
}
repo, ref, _ := repoutils.GetRepoAndRef(image)
infos, err := getInfos(ctx, r, repo, ref)
if err != nil {
log.Fatalf("%s %s: %v\n", repo, ref, err)
}
for _, info := range infos {
fmt.Printf("Deleting %s@%s\n", repo, info.Digest)
if !opts.dryRun {
r.Delete(ctx, repo, info.Digest)
}
}
if opts.all {
// Also delete multi-arch digest
if infos[0].DigestAll != infos[0].Digest {
fmt.Printf("Deleting %s@%s\n", infos[0].Repo, infos[0].DigestAll)
if !opts.dryRun {
r.Delete(ctx, infos[0].Repo, infos[0].DigestAll)
}
}
// OCI spec allows for deletions of tags
fmt.Printf("Deleting %s:%s\n", repo, ref)
if !opts.dryRun {
r.Delete(ctx, repo, ref)
}
}
}
func printHeader() {
fmt.Printf("%-*s", repoWidth+20, "REPOSITORY:TAG")
if opts.digests {
fmt.Printf(" %-72s", "DIGEST")
}
if opts.noTrunc {
fmt.Printf(" %-72s", "IMAGE ID")
} else {
fmt.Printf(" %-12s", "IMAGE ID")
}
if opts.verbose {
fmt.Printf(" %-31s", "CREATED")
}
if opts.all {
fmt.Printf(" %-8s %s", "OS", "ARCH")
}
fmt.Println()
}
func printInfo(info *registry.Info) {
if opts.format != "" {
format.Execute(os.Stdout, info)
fmt.Println()
return
}
fmt.Printf("%-*s", repoWidth+20, info.Repo+":"+info.Ref)
if opts.digests {
fmt.Printf(" %-72s", info.Digest)
}
if opts.noTrunc {
fmt.Printf(" %-72s", info.ID)
} else {
v := strings.SplitN(info.ID, ":", 2)
fmt.Printf(" %-12s", v[1][:12])
}
if opts.verbose {
if info.Image.Created != nil {
if opts.raw {
fmt.Printf(" %-31s", info.Image.Created.String())
} else {
fmt.Printf(" %-31s", prettyTime(info.Image.Created))
}
} else {
fmt.Printf(" %-31s", "-")
}
}
if opts.all {
fmt.Printf(" %-8s %s", info.Image.OS, info.Image.Architecture)
}
fmt.Println()
}
func printIt(format string, name string, it any) {
var value string
var b []byte
v := reflect.ValueOf(it)
if v.Len() == 0 {
return
}
switch v := it.(type) {
case string:
value = v
case map[string]struct{}:
var ss []string
for s := range v {
ss = append(ss, s)
}
b, _ = json.Marshal(ss)
default:
b, _ = json.Marshal(v)
}
if value == "" {
value = string(b)
}
fmt.Printf(format, name, value)
}
func printImage(ctx context.Context, domain string, image string) {
r, err := createRegistryClient(ctx, domain)
if err != nil {
log.Fatal(err)
}
repo, ref, _ := repoutils.GetRepoAndRef(image)
infos, err := getInfos(ctx, r, repo, ref)
if err != nil {
log.Fatalf("%s: %v\n", image, err)
}
for _, info := range infos {
if opts.delete {
fmt.Printf("Deleting %s@%s\n", repo, info.Digest)
if !opts.dryRun {
r.Delete(ctx, repo, info.Digest)
}
continue
}
if opts.verbose {
info.Image, _ = r.GetImage(ctx, repo, info.ID)
}
format := "%-20s\t%s\n"
if info.Image != nil {
// We also have to filter by arch & os because the registry may not return a list
if len(opts.arch) > 0 && !slices.Contains(opts.arch, info.Image.Architecture) {
continue
}
if len(opts.os) > 0 && !slices.Contains(opts.os, info.Image.OS) {
continue
}
printIt(format, "Author", info.Image.Author)
if info.Platform == nil {
printIt(format, "Architecture", info.Image.Architecture)
printIt(format, "OS", info.Image.OS)
}
}
if info.Platform != nil {
printIt(format, "Architecture", info.Platform.Architecture)
printIt(format, "Variant", info.Platform.Variant)
printIt(format, "OS", info.Platform.OS)
printIt(format, "OSVersion", info.Platform.OSVersion)
printIt(format, "OSFeatures", info.Platform.OSFeatures)
}
printIt(format, "Digest", info.Digest)
printIt(format, "DigestAll", info.DigestAll)
printIt(format, "Id", info.ID)
if opts.raw {
printIt("%-20s\t%s\n", "Size", strconv.FormatInt(info.Size, 10))
} else {
printIt(format, "Size", prettySize(info.Size))
}
if info.Image != nil {
if info.Image.Created != nil {
if opts.raw {
printIt(format, "Created", info.Image.Created.String())
} else {
printIt(format, "Created", prettyTime(info.Image.Created))
}
}
printIt(format, "Cmd", info.Image.Config.Cmd)
printIt(format, "Entrypoint", info.Image.Config.Entrypoint)
printIt(format, "ExposedPorts", info.Image.Config.ExposedPorts)
printIt(format, "Labels", info.Image.Config.Labels)
printIt(format, "StopSignal", info.Image.Config.StopSignal)
printIt(format, "User", info.Image.Config.User)
printIt(format, "Volumes", info.Image.Config.Volumes)
printIt(format, "WorkingDir", info.Image.Config.WorkingDir)
for i := range info.Image.History {
fmt.Printf("History[%d]\t\t%s\n", i, info.Image.History[i].CreatedBy)
}
}
fmt.Println()
}
if opts.delete {
// OCI spec allows for deletions of tags
fmt.Printf("Deleting %s %s\n", repo, ref)
if !opts.dryRun {
r.Delete(ctx, repo, ref)
}
}
}
func printAll(ctx context.Context, domain string) {
r, err := createRegistryClient(ctx, domain)
if err != nil {
log.Fatal(err)
}
repos, err := r.Catalog(ctx, "")
if err != nil {
if _, ok := err.(*json.SyntaxError); ok {
log.Fatalf("domain %s is not a valid registry", r.Domain)
} else {
log.Fatal(err)
}
}
repos = filterRegex(repos, repoRegex, false)
sort.Strings(repos)
repoWidth = getMax(repos)
if opts.format == "" {
printHeader()
}
inputChan := make(chan concurrently.WorkFunction)
output := concurrently.Process(ctx, inputChan, &concurrently.Options{PoolSize: maxWorkers, OutChannelBuffer: maxWorkers})
go func() {
for _, repo := range repos {
inputChan <- &loadWorker{reg: r, repo: repo}
}
close(inputChan)
}()
for out := range output {
infos := out.Value.([]*registry.Info)
for _, info := range infos {
if info.Image != nil {
// We also have to filter by arch & os because the registry may not return a list
if len(opts.arch) > 0 && !slices.Contains(opts.arch, info.Image.Architecture) {
continue
}
if len(opts.os) > 0 && !slices.Contains(opts.os, info.Image.OS) {
continue
}
}
printInfo(info)
}
}
}