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 pouch ps filter #1595

Merged
merged 1 commit into from
Jun 28, 2018
Merged

feature: support pouch ps filter #1595

merged 1 commit into from
Jun 28, 2018

Conversation

Ace-Tang
Copy link
Contributor

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

@codecov-io
Copy link

codecov-io commented Jun 26, 2018

Codecov Report

Merging #1595 into master will increase coverage by 23.62%.
The diff coverage is 63.49%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
daemon/mgr/container.go 50.19% <ø> (+50.19%) ⬆️
daemon/mgr/container_types.go 76.11% <ø> (+43.28%) ⬆️
cri/v1alpha1/cri.go 0% <0%> (ø) ⬆️
cri/v1alpha2/cri.go 0% <0%> (ø) ⬆️
cli/ps.go 0% <0%> (ø) ⬆️
apis/server/image_bridge.go 65.82% <100%> (+54.13%) ⬆️
client/container_list.go 68.75% <28.57%> (-31.25%) ⬇️
daemon/mgr/network.go 68.14% <40%> (+64.92%) ⬆️
apis/server/container_bridge.go 82.01% <60%> (+82.01%) ⬆️
pkg/utils/filters/filter.go 67.5% <67.5%> (ø)
... and 137 more

// consider if need to add non-running container
match = isNonStop || fc.all
}
break
Copy link
Collaborator

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

Copy link
Contributor Author

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.

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()
Copy link
Contributor

@Letty5411 Letty5411 Jun 26, 2018

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)
}

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All case has add.

"status": true,

// TODO(huamin.thm):
"before": true,
Copy link
Contributor

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.

}

parsed := make(map[string][]string)
for _, str := range filter {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

}

// FromURLParam gets filter from url query
func FromURLParam(param string) (map[string][]string, error) {
Copy link
Contributor

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.

Copy link
Contributor Author

@Ace-Tang Ace-Tang Jun 27, 2018

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.

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 {
Copy link
Contributor

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`
Copy link
Contributor

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 :)

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Solved


// if filterFunc is defined, we not try other filter condition
if fc.filterFunc != nil {
if match := fc.filterFunc(c); match {
Copy link
Contributor

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 :)

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

for k, v := range value {
// filter equal condition
if equalValue, exist := equalKV[k]; exist {
if equalValue == "" {
Copy link
Contributor

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

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


// filter must meets all condition
var match, jump, statusKey = true, true, false
for name := range fc.condition {
Copy link
Contributor

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?

Copy link
Contributor Author

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")
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

^-^

if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
labelAName := result[0].ID
Copy link
Contributor

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

Copy link
Contributor Author

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>
@fuweid fuweid changed the title feature: support ps filter feature: support pouch ps filter Jun 28, 2018
Copy link
Contributor

@fuweid fuweid left a 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.

@YaoZengzeng
Copy link
Contributor

LGTM, it's all right for CRI part :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature request] pouch [ps|containers list API] with filter functionality
6 participants