-
Notifications
You must be signed in to change notification settings - Fork 571
/
imagewalker.go
113 lines (99 loc) · 3.25 KB
/
imagewalker.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
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package imagewalker
import (
"context"
"fmt"
"regexp"
"strings"
"github.com/containerd/containerd"
"github.com/containerd/containerd/images"
"github.com/containerd/nerdctl/pkg/referenceutil"
"github.com/opencontainers/go-digest"
)
type Found struct {
Image images.Image
Req string // The raw request string. name, short ID, or long ID.
MatchIndex int // Begins with 0, up to MatchCount - 1.
MatchCount int // 1 on exact match. > 1 on ambiguous match. Never be <= 0.
UniqueImages int // Number of unique images in all found images.
}
type OnFound func(ctx context.Context, found Found) error
type ImageWalker struct {
Client *containerd.Client
OnFound OnFound
}
// Walk walks images and calls w.OnFound .
// Req is name, short ID, or long ID.
// Returns the number of the found entries.
func (w *ImageWalker) Walk(ctx context.Context, req string) (int, error) {
var filters []string
if canonicalRef, err := referenceutil.ParseAny(req); err == nil {
filters = append(filters, fmt.Sprintf("name==%s", canonicalRef.String()))
}
filters = append(filters,
fmt.Sprintf("name==%s", req),
fmt.Sprintf("target.digest~=^sha256:%s.*$", regexp.QuoteMeta(req)),
fmt.Sprintf("target.digest~=^%s.*$", regexp.QuoteMeta(req)),
)
images, err := w.Client.ImageService().List(ctx, filters...)
if err != nil {
return -1, err
}
matchCount := len(images)
// to handle the `rmi -f` case where returned images are different but
// have the same short prefix.
uniqueImages := make(map[digest.Digest]bool)
for _, image := range images {
uniqueImages[image.Target.Digest] = true
}
for i, img := range images {
f := Found{
Image: img,
Req: req,
MatchIndex: i,
MatchCount: matchCount,
UniqueImages: len(uniqueImages),
}
if e := w.OnFound(ctx, f); e != nil {
return -1, e
}
}
return matchCount, nil
}
// WalkAll calls `Walk` for each req in `reqs`.
//
// It can be used when the matchCount is not important (e.g., only care if there
// is any error or if matchCount == 0 (not found error) when walking all reqs).
// If `forceAll`, it calls `Walk` on every req
// and return all errors joined by `\n`. If not `forceAll`, it returns the first error
// encountered while calling `Walk`.
func (w *ImageWalker) WalkAll(ctx context.Context, reqs []string, forceAll bool) error {
var errs []string
for _, req := range reqs {
n, err := w.Walk(ctx, req)
if err == nil && n == 0 {
err = fmt.Errorf("no such image: %s", req)
}
if err != nil {
if !forceAll {
return err
}
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return fmt.Errorf("%d errors:\n%s", len(errs), strings.Join(errs, "\n"))
}
return nil
}