Skip to content

Commit

Permalink
Merge pull request #4701 from kzys/content-store-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
AkihiroSuda committed Jan 25, 2021
2 parents f615c58 + e74ace9 commit 2034660
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
52 changes: 52 additions & 0 deletions content/adaptor.go
@@ -0,0 +1,52 @@
/*
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 content

import (
"strings"

"github.com/containerd/containerd/filters"
)

// AdoptInfo returns `filters.Adaptor` that handles `content.Info`.
func AdaptInfo(info Info) filters.Adaptor {
return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
if len(fieldpath) == 0 {
return "", false
}

switch fieldpath[0] {
case "digest":
return info.Digest.String(), true
case "size":
// TODO: support size based filtering
case "labels":
return checkMap(fieldpath[1:], info.Labels)
}

return "", false
})
}

func checkMap(fieldpath []string, m map[string]string) (string, bool) {
if len(m) == 0 {
return "", false
}

value, ok := m[strings.Join(fieldpath, ".")]
return value, ok
}
16 changes: 13 additions & 3 deletions content/local/store.go
Expand Up @@ -240,9 +240,14 @@ func (s *store) Update(ctx context.Context, info content.Info, fieldpaths ...str
return info, nil
}

func (s *store) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
// TODO: Support filters
func (s *store) Walk(ctx context.Context, fn content.WalkFunc, fs ...string) error {
root := filepath.Join(s.root, "blobs")

filter, err := filters.ParseAll(fs...)
if err != nil {
return err
}

var alg digest.Algorithm
return filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -286,7 +291,12 @@ func (s *store) Walk(ctx context.Context, fn content.WalkFunc, filters ...string
return err
}
}
return fn(s.info(dgst, fi, labels))

info := s.info(dgst, fi, labels)
if !filter.Match(content.AdaptInfo(info)) {
return nil
}
return fn(info)
})
}

Expand Down
19 changes: 0 additions & 19 deletions metadata/adaptors.go
Expand Up @@ -90,25 +90,6 @@ func adaptContainer(o interface{}) filters.Adaptor {
})
}

func adaptContentInfo(info content.Info) filters.Adaptor {
return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
if len(fieldpath) == 0 {
return "", false
}

switch fieldpath[0] {
case "digest":
return info.Digest.String(), true
case "size":
// TODO: support size based filtering
case "labels":
return checkMap(fieldpath[1:], info.Labels)
}

return "", false
})
}

func adaptContentStatus(status content.Status) filters.Adaptor {
return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
if len(fieldpath) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion metadata/content.go
Expand Up @@ -181,7 +181,7 @@ func (cs *contentStore) Walk(ctx context.Context, fn content.WalkFunc, fs ...str
if err := readInfo(&info, bkt.Bucket(k)); err != nil {
return err
}
if filter.Match(adaptContentInfo(info)) {
if filter.Match(content.AdaptInfo(info)) {
infos = append(infos, info)
}
return nil
Expand Down

0 comments on commit 2034660

Please sign in to comment.