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

fixes #2817 container run with the empty label #2824

Merged
merged 1 commit into from
May 6, 2019

Conversation

Flsorescence
Copy link
Contributor

Ⅰ. Describe what this PR did

fixes the issue #2817 Failed to run a container with empty label

Ⅱ. Does this pull request fix one issue?

fixes #2817

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

I have modified the labels_test.go and add some new unit tests.

Ⅳ. Describe how to verify it

pouch run --label k without value can run successfully

Ⅴ. Special notes for reviews

@pouchrobot
Copy link
Collaborator

We found this is your first time to contribute to Pouch, @Flsorescence
👏 We really appreciate it.
Just remind that you have read the contribution guide: https://github.com/alibaba/pouch/blob/master/CONTRIBUTING.md
If you didn't, you should do that first. If done, welcome again and please enjoy hacking! 🍻

@codecov
Copy link

codecov bot commented Apr 30, 2019

Codecov Report

Merging #2824 into master will decrease coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2824      +/-   ##
==========================================
- Coverage   69.19%   69.14%   -0.05%     
==========================================
  Files         279      279              
  Lines       17607    17603       -4     
==========================================
- Hits        12183    12172      -11     
  Misses       4052     4052              
- Partials     1372     1379       +7
Flag Coverage Δ
#criv1alpha2_test 39.35% <0%> (ø) ⬆️
#integration_test_0 36.36% <71.42%> (ø) ⬆️
#integration_test_1 35.19% <0%> (-0.03%) ⬇️
#integration_test_2 36.34% <0%> (-0.03%) ⬇️
#integration_test_3 35.26% <0%> (-0.02%) ⬇️
#node_e2e_test 35.06% <0%> (-0.04%) ⬇️
#unittest 28.69% <100%> (-0.01%) ⬇️
Impacted Files Coverage Δ
daemon/mgr/container.go 60.18% <ø> (+0.12%) ⬆️
apis/opts/labels.go 100% <100%> (ø) ⬆️
ctrd/watch.go 72.97% <0%> (-5.41%) ⬇️
ctrd/container.go 53.51% <0%> (-1.52%) ⬇️
ctrd/supervisord/daemon.go 49.32% <0%> (-1.36%) ⬇️
cri/v1alpha2/cri.go 69.06% <0%> (ø) ⬆️
pkg/streams/utils.go 89.28% <0%> (+7.14%) ⬆️

if len(fields) == 1 {
fields = append(fields, "")
return fields, nil
}
return nil, fmt.Errorf("invalid label %s: label must be in format of key=value", label)
Copy link
Collaborator

@allencloud allencloud May 2, 2019

Choose a reason for hiding this comment

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

If you compare the length of fields with 1, then this line of code is useless. Here is my reason.

Whatever the string label is, when it is been called with fields := strings.SplitN(label, "=", 2), the length of fileds will only be 1 or 2. Then if len(fields) is not 2, then it must be 1. And as a result, code will never run to this line of code return nil, fmt.Errorf("invalid label %s: label must be in format of key=value", label), and it is redudant.

How about doing this to simplify the code and make it more readable:

fields := strings.SplitN(label, "=", 2)
if len(fields) == 1 {
    fields = append(fields, "")
}
// length of fields must be 2 when code runs here
return fields, nil

In addition, we can judge whether to define this function parseLabel to only return one parameter []string, you see the error which is returned as the second parameter will always be nil. Thus no need to return it. But it depends on you about this part's modification. @Flsorescence

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 for your comment.

Yes, I know the statement return nil, fmt.Errorf("invalid label %s: label must be in format of key=value", label) will never be executed, but I don't know whether the second parameter is essential for future development.

Thus, I will annotate the useless statement and reserve the second parameter.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the update. I would like to invite @CodeJuan to take a review of this.

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.

Please confirm env command is same to label command here. Thanks

// Only input key without value
if len(fields) == 1 {
fields = append(fields, "")
return fields, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicate return here. just use global return

BTW, do we need error here?

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 have checked the env command and it has solved the empty value problem.

Do you mean I should modify the code logic of label command as same as env command?

Reserve the parameter of error is beacause I don't know whether it is essential for future development.

I will modify the return statement. Thanks.

Signed-off-by: JoJo <1043708974@qq.com>
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

@CodeJuan
Copy link
Contributor

CodeJuan commented May 6, 2019

LGTM

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

Successfully merging this pull request may close these issues.

[bug]Failed to run a container with empty label
5 participants