Skip to content

Commit 2034660

Browse files
authored
Merge pull request containerd#4701 from kzys/content-store-filter
2 parents f615c58 + e74ace9 commit 2034660

File tree

4 files changed

+66
-23
lines changed

4 files changed

+66
-23
lines changed

content/adaptor.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package content
18+
19+
import (
20+
"strings"
21+
22+
"github.com/containerd/containerd/filters"
23+
)
24+
25+
// AdoptInfo returns `filters.Adaptor` that handles `content.Info`.
26+
func AdaptInfo(info Info) filters.Adaptor {
27+
return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
28+
if len(fieldpath) == 0 {
29+
return "", false
30+
}
31+
32+
switch fieldpath[0] {
33+
case "digest":
34+
return info.Digest.String(), true
35+
case "size":
36+
// TODO: support size based filtering
37+
case "labels":
38+
return checkMap(fieldpath[1:], info.Labels)
39+
}
40+
41+
return "", false
42+
})
43+
}
44+
45+
func checkMap(fieldpath []string, m map[string]string) (string, bool) {
46+
if len(m) == 0 {
47+
return "", false
48+
}
49+
50+
value, ok := m[strings.Join(fieldpath, ".")]
51+
return value, ok
52+
}

content/local/store.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,14 @@ func (s *store) Update(ctx context.Context, info content.Info, fieldpaths ...str
240240
return info, nil
241241
}
242242

243-
func (s *store) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
244-
// TODO: Support filters
243+
func (s *store) Walk(ctx context.Context, fn content.WalkFunc, fs ...string) error {
245244
root := filepath.Join(s.root, "blobs")
245+
246+
filter, err := filters.ParseAll(fs...)
247+
if err != nil {
248+
return err
249+
}
250+
246251
var alg digest.Algorithm
247252
return filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
248253
if err != nil {
@@ -286,7 +291,12 @@ func (s *store) Walk(ctx context.Context, fn content.WalkFunc, filters ...string
286291
return err
287292
}
288293
}
289-
return fn(s.info(dgst, fi, labels))
294+
295+
info := s.info(dgst, fi, labels)
296+
if !filter.Match(content.AdaptInfo(info)) {
297+
return nil
298+
}
299+
return fn(info)
290300
})
291301
}
292302

metadata/adaptors.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,6 @@ func adaptContainer(o interface{}) filters.Adaptor {
9090
})
9191
}
9292

93-
func adaptContentInfo(info content.Info) filters.Adaptor {
94-
return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
95-
if len(fieldpath) == 0 {
96-
return "", false
97-
}
98-
99-
switch fieldpath[0] {
100-
case "digest":
101-
return info.Digest.String(), true
102-
case "size":
103-
// TODO: support size based filtering
104-
case "labels":
105-
return checkMap(fieldpath[1:], info.Labels)
106-
}
107-
108-
return "", false
109-
})
110-
}
111-
11293
func adaptContentStatus(status content.Status) filters.Adaptor {
11394
return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
11495
if len(fieldpath) == 0 {

metadata/content.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (cs *contentStore) Walk(ctx context.Context, fn content.WalkFunc, fs ...str
181181
if err := readInfo(&info, bkt.Bucket(k)); err != nil {
182182
return err
183183
}
184-
if filter.Match(adaptContentInfo(info)) {
184+
if filter.Match(content.AdaptInfo(info)) {
185185
infos = append(infos, info)
186186
}
187187
return nil

0 commit comments

Comments
 (0)