-
Notifications
You must be signed in to change notification settings - Fork 948
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 pouch ps filter #1595
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1595 +/- ##
==========================================
+ Coverage 17.97% 41.6% +23.62%
==========================================
Files 224 273 +49
Lines 14780 17699 +2919
==========================================
+ Hits 2657 7364 +4707
+ Misses 11950 9423 -2527
- Partials 173 912 +739
|
daemon/mgr/container_list.go
Outdated
// consider if need to add non-running container | ||
match = isNonStop || fc.all | ||
} | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one match condition will stop this filter routine. what if people need both -f label=site=daily
and -f label=network!=overlay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modify the logic, jump until condition not equal, and add flag jump
to determine if a condition is useful. Test case has add for this case.
test/cli_ps_test.go
Outdated
labelA := "equal-label-a" | ||
command.PouchRun("run", "-d", "--name", labelA, "-l", "a=b", busyboxImage, "top").Assert(c, icmd.Success) | ||
defer DelContainerForceMultyTime(c, labelA) | ||
output := command.PouchRun("inspect", labelA).Stdout() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about use :
output := command.PouchRun("inspect", labelA)..Assert(c, icmd.Success).Stdout()
c.Assert(exist3, check.Equals, true) | ||
c.Assert(exist4, check.Equals, true) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could also add negative tests, like:
-f null
-f wrong format
and use multiple -f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All case has add.
apis/opts/filter.go
Outdated
"status": true, | ||
|
||
// TODO(huamin.thm): | ||
"before": true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it is the TODO list, could we turn it into the false
? or remove the key because the value is placeholder.
apis/opts/filter.go
Outdated
} | ||
|
||
parsed := make(map[string][]string) | ||
for _, str := range filter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the utils
contains the ConvertKVStringsToMap
function. Could we use the same functionality here? DRY :) right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it maybe not as a common function, think about in this case the parameter is map[string]bool
, and can be other type, like map[int]string
, and I want output include only key, maybe next time someone need output include key/value or only value.
apis/opts/filter.go
Outdated
} | ||
|
||
// FromURLParam gets filter from url query | ||
func FromURLParam(param string) (map[string][]string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that we can get the meaning from the name FromURLParam
. It's confusing with other functionality, for example, the parse option into map[string][]string
function. So I suggest that we should use more specific name for that part.
ToURLParam
also has the same problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good, I think about move the file api/opts/filter.go
to pkg
package and make it a package named filter, for one reason it not play a role as other code in package opts do, the other is thatI think the name FromURLParam
and ToURLParam
will make sense.
apis/opts/filter.go
Outdated
err := json.NewDecoder(strings.NewReader(param)).Decode(&filter) | ||
|
||
// params from url may not passed through api, so we need validate here | ||
for name := range filter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the following function ValidateFilter
?
- `name=container-name` | ||
- `status=running` | ||
- `label=key` or `label=key=value` | ||
- `network=container-network` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the pouch doesn't support the condition right now, we should remove them from the description. Just in case that we don't give the wrong information to the user :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think so, interface should think more, we just tell which not support before this, user always not care about defined field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solved
daemon/mgr/container_list.go
Outdated
|
||
// if filterFunc is defined, we not try other filter condition | ||
if fc.filterFunc != nil { | ||
if match := fc.filterFunc(c); match { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change this line into if fc.filterFunc(c) {
to just make it clear :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good
daemon/mgr/container_list.go
Outdated
for k, v := range value { | ||
// filter equal condition | ||
if equalValue, exist := equalKV[k]; exist { | ||
if equalValue == "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
squash the two if conditions into one if equalValue == "" || equalValue == v
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good
daemon/mgr/container_list.go
Outdated
|
||
// filter must meets all condition | ||
var match, jump, statusKey = true, true, false | ||
for name := range fc.condition { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have idea about this part.
The jump
boolean means we should start next round check, right? the matchKVFilter
will return one boolean value if we run the filters, exist := fc.condition[field]
before check. Therefore, we don't need to check the jump
in the following condition.
Does it make senses to you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think about this, filters, exist := fc.condition[field]
if field not exist, whether filter function return true or false is not make sense. Maybe need more think.
@@ -71,6 +72,139 @@ func (suite *PouchPsSuite) TestPsWorks(c *check.C) { | |||
|
|||
} | |||
|
|||
// TestPsFilterInvalid tests "pouch ps -f" invalid | |||
func (suite *PouchPsSuite) TestPsFilterInvalid(c *check.C) { | |||
result := command.PouchRun("ps", "-f", "foo") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Next time, we can use the table-driven test to repeat the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^-^
test/cli_ps_test.go
Outdated
if err := json.Unmarshal([]byte(output), &result); err != nil { | ||
c.Errorf("failed to decode inspect output: %v", err) | ||
} | ||
labelAName := result[0].ID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/labelAName/labelAID/ maybe better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This rule is for variable, WDYT?
support pouch ps --filter, not only equal condition, but also unequal condition, like pouch ps --filter label=a!=b Fixes: #1562. Signed-off-by: Ace-Tang <aceapril@126.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM @YaoZengzeng PTAL about the CRI part.
LGTM, it's all right for CRI part :) |
support pouch ps --filter, not only equal condition, but also
unequal condition, like pouch ps --filter label=a!=b
Fixes: #1562.
Signed-off-by: Ace-Tang aceapril@126.com
Ⅰ. Describe what this PR did
Ⅱ. Does this pull request fix one issue?
fixes: #1562
Ⅲ. Describe how you did it
Ⅳ. Describe how to verify it
Ⅴ. Special notes for reviews