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

pkg/validation: add selector validation #288

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/index/validation/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ func validateSelector(sel *metav1.LabelSelector) error {
if sel == nil {
return errors.New("nil selector is not supported")
}
if sel.MatchLabels == nil && len(sel.MatchExpressions) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

To be on the safe side, this should be

if len(sel.MatchLabels) == 0 && len(sel.MatchExpressions) == 0 {

Otherwise, sel.MatchLabels could be non-nil, but empty. And that would match everything, right?

Copy link
Member Author

@ahmetb ahmetb Jul 22, 2019

Choose a reason for hiding this comment

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

we have a check for that case below.
I wanted to distinguish error messages for these:

selector:
selector:
  matchLabels:

which means different things.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see.

return errors.New("empty selector is not supported")
}

// check for unsupported keys
keys := []string{}
Expand Down
5 changes: 5 additions & 0 deletions pkg/index/validation/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ func Test_validateSelector(t *testing.T) {
sel: nil,
wantErr: true,
},
{
Copy link
Contributor

Choose a reason for hiding this comment

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

This test does not pass, and should be added:

                 {
			name:    "wildcard selector",
			sel:     &metav1.LabelSelector{},
			wantErr: true,
		},

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a great catch. Added a commit.

name: "empty (wildcard) selector",
sel: &metav1.LabelSelector{},
wantErr: true,
},
{
name: "valid matchLabels",
sel: &metav1.LabelSelector{MatchLabels: map[string]string{"os": "foo", "arch": "bar"}},
Expand Down