Skip to content

Commit

Permalink
bugfix: fix images filter when show multi-tag images
Browse files Browse the repository at this point in the history
Signed-off-by: zhangyue <[email protected]>
  • Loading branch information
zhangyue authored and fuweid committed Jan 17, 2019
1 parent fa8101e commit 9b92482
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
69 changes: 45 additions & 24 deletions daemon/mgr/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (mgr *ImageManager) ListImages(ctx context.Context, filter filters.Args) ([

beforeImages := filter.Get("before")
sinceImages := filter.Get("since")
referenceFilter := filter.Get("reference")

// refuse undefined behavior
if len(beforeImages) > 1 {
Expand Down Expand Up @@ -269,35 +270,32 @@ func (mgr *ImageManager) ListImages(ctx context.Context, filter filters.Args) ([
}
}

if filter.Contains("reference") {
var found bool
referenceFilters := filter.Get("reference")
for _, ref := range mgr.localStore.GetReferences(img.ID) {
for _, pattern := range referenceFilters {
matched, err := filters.FamiliarMatch(pattern, ref.String())
if err != nil {
return nil, err
}
if matched {
found = true
break
}
}
if found {
break
}
}
if !found {
continue
}
}

imgInfo, err := mgr.containerdImageToImageInfo(ctx, img.ID)
if err != nil {
logrus.Warnf("failed to convert containerd image(%v) to ImageInfo during list images: %v", img.ID, err)
continue
}
imgInfos = append(imgInfos, imgInfo)

if len(referenceFilter) == 0 {
imgInfos = append(imgInfos, imgInfo)
continue
}

// do reference filter
imgInfo.RepoDigests, err = filterReference(referenceFilter, imgInfo.RepoDigests)
if err != nil {
return []types.ImageInfo{}, err
}

imgInfo.RepoTags, err = filterReference(referenceFilter, imgInfo.RepoTags)
if err != nil {
return []types.ImageInfo{}, err
}

if len(imgInfo.RepoTags) > 0 || len(imgInfo.RepoDigests) > 0 {
imgInfos = append(imgInfos, imgInfo)
}

}
return imgInfos, nil
}
Expand Down Expand Up @@ -733,3 +731,26 @@ func parseTagReference(targetTag string) (reference.Named, error) {

return reference.WithDefaultTagIfMissing(ref), nil
}

func filterReference(filter, ref []string) ([]string, error) {
if len(filter) == 0 {
return ref, nil
}

var err error
filteredRefs := make([]string, 0)
for _, ref := range ref {
var found bool
for _, pattern := range filter {
found, err = filters.FamiliarMatch(pattern, ref)
if err != nil {
return []string{}, err
}
if found {
filteredRefs = append(filteredRefs, ref)
break
}
}
}
return filteredRefs, nil
}
2 changes: 0 additions & 2 deletions test/api_image_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (suite *APIImageListSuite) TestImageListDigest(c *check.C) {
func (suite *APIImageListSuite) TestImageListFilter(c *check.C) {
q := url.Values{}

repoDigest := environment.BusyboxRepo + "@" + environment.BusyboxDigest
repoTag := environment.BusyboxRepo + ":" + environment.BusyboxTag

f := filters.NewArgs()
Expand All @@ -88,7 +87,6 @@ func (suite *APIImageListSuite) TestImageListFilter(c *check.C) {
c.Assert(got[0].Size, check.NotNil)
c.Assert(got[0].Os, check.NotNil)
c.Assert(reflect.DeepEqual(got[0].RepoTags, []string{repoTag}), check.Equals, true)
c.Assert(reflect.DeepEqual(got[0].RepoDigests, []string{repoDigest}), check.Equals, true)
}

// TestImageListInvalidFilter tests listing images with invalid filter.
Expand Down

0 comments on commit 9b92482

Please sign in to comment.