Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support images filter flag #2410

Merged
merged 1 commit into from
Nov 9, 2018

Conversation

ZYecho
Copy link
Contributor

@ZYecho ZYecho commented Nov 2, 2018

Signed-off-by: zhangyue [email protected]

Ⅰ. Describe what this PR did

add filter flag support for pouch images, support three filters: since/before/reference

  • before ([:], or image@digest) - filter images created before given id or references
  • since ([:], or image@digest) - filter images created since given id or references
  • The reference filter shows only images whose reference matches the specified pattern. may have multi reference filter. the relationship between multi reference filter is or.

Ⅱ. Does this pull request fix one issue?

Related to #2405
fix #2371

Ⅲ. Why don't you add test cases (unit test/integration test)? (你真的觉得不需要加测试吗?)

add api testcase and cli testcase.

Ⅳ. Describe how to verify it

pouch images

IMAGE ID       IMAGE NAME                                          SIZE
8c811b4aec35   registry.hub.docker.com/library/busybox:1.28        710.81 KB
4ab4c602aa5e   registry.hub.docker.com/library/hello-world:linux   5.25 KB

pouch images -f reference=registry.hub.docker.com/library/busybox:*

IMAGE ID       IMAGE NAME                                     SIZE
8c811b4aec35   registry.hub.docker.com/library/busybox:1.28   710.81 KB

pouch images -f after=registry.hub.docker.com/library/busybox:1.28
Got
Error: failed to get image list: {"message":"Invalid filter after"}

pouch images -f before=registry.hub.docker.com/library/hello-world:linux

IMAGE ID       IMAGE NAME                                     SIZE
8c811b4aec35   registry.hub.docker.com/library/busybox:1.28   710.81 KB

Ⅴ. Special notes for reviews

Use the same filter package as pouch events

@codecov
Copy link

codecov bot commented Nov 2, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@401e132). Click here to learn what that means.
The diff coverage is 70%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #2410   +/-   ##
=========================================
  Coverage          ?   68.91%           
=========================================
  Files             ?      276           
  Lines             ?    18287           
  Branches          ?        0           
=========================================
  Hits              ?    12602           
  Misses            ?     4255           
  Partials          ?     1430
Flag Coverage Δ
#criv1alpha1test 31.57% <16.25%> (?)
#criv1alpha2test 35.62% <16.25%> (?)
#integrationtest 40.33% <58.75%> (?)
#nodee2etest 32.98% <16.25%> (?)
#unittest 26.59% <15%> (?)
Impacted Files Coverage Δ
cri/v1alpha1/cri.go 60.59% <ø> (ø)
daemon/mgr/system.go 73.28% <ø> (ø)
cri/v1alpha2/cri.go 69.65% <ø> (ø)
client/image_list.go 64.28% <37.5%> (ø)
apis/server/image_bridge.go 74.1% <40%> (ø)
daemon/mgr/image.go 59.63% <71.42%> (ø)
apis/filters/parse.go 80.72% <88.88%> (ø)

@allencloud
Copy link
Collaborator

CI fails with code check. Please take a look. @ZYecho Thanks.

@ZYecho ZYecho force-pushed the add-images-filter branch from 9487210 to 29c8c67 Compare November 2, 2018 02:49
@ZYecho
Copy link
Contributor Author

ZYecho commented Nov 2, 2018

CI fails with code check. Please take a look. @ZYecho Thanks.

@allencloud fixed it, also cc @Ace-Tang @fuweid request for review.

@@ -150,3 +157,33 @@ func FromParam(p string) (Args, error) {
}
return args, nil
}

//FromFilterOpts parse key=value to Args string from cli opts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add some test cases for this new function, WDYT ? @ZYecho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, I add a testcase to cover this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add space between // and FromFilterOpts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@ZYecho ZYecho force-pushed the add-images-filter branch 4 times, most recently from f3bc3fe to 39e74c0 Compare November 2, 2018 09:07
func (args Args) Validate(accepted map[string]bool) error {
for name := range args.fields {
if !accepted[name] {
return errors.New("Invalid filter " + name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we avoid to start with a capital letter in the error string? The reason is here https://github.com/golang/go/wiki/Errors#errors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add accepted filter name ? make user more clearly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add accepted filter name ? make user more clearly

Is there a need to do here? I find that other validate func in pouch also not do this, maybe We can do this by adding docs or cli usage? wdyt? @Ace-Tang

apis/filters/parse_test.go Show resolved Hide resolved
@@ -150,3 +157,33 @@ func FromParam(p string) (Args, error) {
}
return args, nil
}

//FromFilterOpts parse key=value to Args string from cli opts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add space between // and FromFilterOpts?

return nil
}

//FamiliarMatch decide the ref match the pattern or not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add space between // and FromFilterOpts?

daemon/mgr/image.go Show resolved Hide resolved
beforeImages := filter.Get("before")
if len(beforeImages) > 0 {
// use the last one if multi before images was assigned
beforeFilter, err = mgr.GetImage(ctx, beforeImages[len(beforeImages)-1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can we make the beforeImages is sorted?

Copy link
Contributor Author

@ZYecho ZYecho Nov 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I'm curious about the logic dealing with multi-since or multi-before, I think we can not guess the user want to the first one or the last one, use the last input or throw err if multi-since or multi-before? wdyt? @fuweid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, could we limit the multi-since/multi-before here? Since there is no reason for this requirement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fuweid I test the same scenario in moby, have the same problem. I think the single-since/since-before is reasonable, while multi is undefined. So in here, I want to throw err if len > 1, wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

if filter.Contains("reference") {
var found bool
for _, ref := range mgr.localStore.GetReferences(img.ID) {
for _, pattern := range filter.Get("reference") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we retrieve filter.Get("reference") before? I think the Get will allocate new memory each loop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems much condition here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the condition?

func (args Args) Validate(accepted map[string]bool) error {
for name := range args.fields {
if !accepted[name] {
return errors.New("Invalid filter " + name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add accepted filter name ? make user more clearly

apis/server/image_bridge.go Show resolved Hide resolved
cli/image_list.go Show resolved Hide resolved
if filter.Contains("reference") {
var found bool
for _, ref := range mgr.localStore.GetReferences(img.ID) {
for _, pattern := range filter.Get("reference") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems much condition here


//Test invalid filter
invalidRes := command.PouchRun("images", "-f", "after="+busyboxImage)
c.Assert(invalidRes.Stderr(), check.Equals, "Error: failed to get image list: {\"message\":\"Invalid filter after\"}\n\n")
Copy link
Contributor

@fuweid fuweid Nov 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the case is failed

/home/travis/gopath/src/github.com/alibaba/pouch/test/cli_images_test.go:114:
    c.Assert(invalidRes.Stderr(), check.Equals, "Error: failed to get image list: {\"message\":\"Invalid filter after\"}\n\n")
... obtained string = "" +
...     "Error: failed to get image list: {\"message\":\"invalid filter after\"}\n" +
...     "\n"
... expected string = "" +
...     "Error: failed to get image list: {\"message\":\"Invalid filter after\"}\n" +
...     "\n"

I was thinking that we can do this in api level test and only check the status. The message is out of control.
But the status should be stable. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, update it later.

@ZYecho ZYecho force-pushed the add-images-filter branch 2 times, most recently from 55b6f30 to a636407 Compare November 9, 2018 02:00
@ZYecho
Copy link
Contributor Author

ZYecho commented Nov 9, 2018

@fuweid all mentioned have fixed. PTAL, thanks.


// refuse undefined behavior
if len(beforeImages) > 1 || len(sinceImages) > 1 {
return nil, pkgerrors.Wrapf(errtypes.ErrInvalidParam, "can't use since or before filter more than one")
Copy link
Contributor

@fuweid fuweid Nov 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we split the error into two part? I think it can make the error more clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

c.Assert(reflect.DeepEqual(got[0].RepoTags, []string{repoTag}), check.Equals, true)
c.Assert(reflect.DeepEqual(got[0].RepoDigests, []string{repoDigest}), check.Equals, true)

// check invalid filter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we split it into other test case function? I think it is too long.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@ZYecho ZYecho force-pushed the add-images-filter branch from a636407 to 3411c45 Compare November 9, 2018 03:35
@@ -12,14 +12,15 @@ import (

"github.com/alibaba/pouch/apis/types"

"github.com/alibaba/pouch/apis/filters"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put this line in the second part, thanks @ZYecho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, update.

@ZYecho ZYecho force-pushed the add-images-filter branch from 3411c45 to 3248c28 Compare November 9, 2018 03:54
@Ace-Tang
Copy link
Contributor

Ace-Tang commented Nov 9, 2018

lgtm

@pouchrobot pouchrobot added the LGTM one maintainer or community participant agrees to merge the pull reuqest. label Nov 9, 2018
@HusterWan
Copy link
Contributor

LGTM, thanks for your works @ZYecho

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature LGTM one maintainer or community participant agrees to merge the pull reuqest. size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[question] Is there a need to support pouch images filter and no-trunc flag?
6 participants