-
Notifications
You must be signed in to change notification settings - Fork 1.2k
⚠️ Default selectors to Nothing if it is nil for MatchingLabelsSelector
and MatchingFieldsSelector
#3279
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
Conversation
…or and MatchingFieldsSelector
Hi @acumino. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
/cc @sbueringer |
/ok-to-test |
@@ -644,6 +644,9 @@ type MatchingLabelsSelector struct { | |||
|
|||
// ApplyToList applies this configuration to the given list options. | |||
func (m MatchingLabelsSelector) ApplyToList(opts *ListOptions) { | |||
if m.Selector == nil { |
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 would a label or field selector be set if it has no value?
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.
Two reasons -
- When a list call happens with empty/nil label selector, it lists everything, since
MatchingLabelsSelector
also wants to achieve a similar thing; therefore, in case of a nil label selector, it should list all the objects. - Currently if nil is passed for selector in
MatchingLabelsSelector
the list call panics, which should not happen
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.
For what it's worth, this is the official godoc on metav1.LabelSelector
// A label selector is a label query over a set of resources. The result of matchLabels and
// matchExpressions are ANDed. An *empty* label selector matches *all objects*. A *null*
// label selector matches *no objects*.
If we follow that semantic here as well null would mean match no objects
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 I use -
client.List(ctx, nodeList, &client.ListOptions{LabelSelector: nil})
It lists all objects. What should be the way to go here?
Also
A null label selector matches no objects.
If we follow this, then I can modify the PR to use Nothing instead of Everything when MatchingLabelsSelector
's selector is not set so it doesn't panic.
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 you explain why you have code that ends up with a nil selector and why that is not considered to be a bug?
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.
WDYT about the below changes -
// ApplyToList applies this configuration to the given list options.
func (m MatchingLabelsSelector) ApplyToList(opts *ListOptions) {
if m.Selector == nil {
m.Selector = labels.SelectorFromSet(labels.Set{})
}
opts.LabelSelector = m
}
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.
Having a nil selector in there is incorrect usage and we can not error. We do recover panics though so it seems all right to me.
Your suggestion makes a nil selector match everything, which as mentioned earlier we do not want.
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.
recover panics though so it seems all right to me.
This doesn't allow us to let the panic happen at the first place.
Also, IMO we should allow list all, because as per the doc string MatchingLabelsSelector
is supposed to behave the same as LabelSelector, which it doesn't currently.
By my main concern for this PR was that we should not let the panic happen, we should stop it one way or another and not let the user figure it out after trial and error. I am open to suggestions for that.
If we really don't want to list anything in case of nil Selector in MatchingLabelsSelector
, I can use labels.Nothing
instead of Everything
.
This will fix both of our concerns.
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.
Also, IMO we should allow list all, because as per the doc string MatchingLabelsSelector is supposed to behave the same as LabelSelector, which it doesn't currently.
Which could also be fixed by changing LabelSelector rather than MatchingLabelSelector
.
I don't personally mind the panic, given our default is to recover panics and this is basically incorrect usage and we don't have a way to report that, as the listOpts can not error. I am okay changing it to labels.Nothing()
if you want to do that, but I would veto changing it to labels.Everything()
due to potential security implications.
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.
My main issue was nil pointer panic, I am ok with nothing also.
Update the PR, PTAL.
MatchingLabelsSelector
and MatchingFieldsSelector
MatchingLabelsSelector
and MatchingFieldsSelector
MatchingLabelsSelector
and MatchingFieldsSelector
MatchingLabelsSelector
and MatchingFieldsSelector
LGTM label has been added. Git tree hash: 16a47334870891aed9482e6966ba44660880fdb8
|
/hold in case @sbueringer wants to check |
Thx! /lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: acumino, alvaroaleman, sbueringer The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
If the selector is not set for
MatchingLabelsSelector
andMatchingFieldsSelector
, then the list call panics.This PR updates
MatchingLabelsSelector
andMatchingFieldsSelector
to default their Selector fields to labels.Nothing() and fields.Nothing() respectively if unset in ApplyToList.