Skip to content

Commit e4f9a24

Browse files
authored
⚠️ Default selectors to Nothing if it is nil for MatchingLabelsSelector and MatchingFieldsSelector (#3279)
* Default selectors to Everything if it is nil for MatchingLabelsSelector and MatchingFieldsSelector * Address Review
1 parent 542acae commit e4f9a24

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

pkg/client/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,9 @@ type MatchingLabelsSelector struct {
644644

645645
// ApplyToList applies this configuration to the given list options.
646646
func (m MatchingLabelsSelector) ApplyToList(opts *ListOptions) {
647+
if m.Selector == nil {
648+
m.Selector = labels.Nothing()
649+
}
647650
opts.LabelSelector = m
648651
}
649652

@@ -677,6 +680,9 @@ type MatchingFieldsSelector struct {
677680

678681
// ApplyToList applies this configuration to the given list options.
679682
func (m MatchingFieldsSelector) ApplyToList(opts *ListOptions) {
683+
if m.Selector == nil {
684+
m.Selector = fields.Nothing()
685+
}
680686
opts.FieldSelector = m
681687
}
682688

pkg/client/options_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,38 @@ var _ = Describe("ListOptions", func() {
3636
o.ApplyToList(newListOpts)
3737
Expect(newListOpts).To(Equal(o))
3838
})
39+
It("Should set LabelSelector with MatchingLabelsSelector", func() {
40+
labelSelector, err := labels.Parse("a=b")
41+
Expect(err).NotTo(HaveOccurred())
42+
newListOpts := &client.ListOptions{}
43+
newListOpts.ApplyOptions([]client.ListOption{client.MatchingLabelsSelector{Selector: labelSelector}})
44+
expectedListOpts := &client.ListOptions{LabelSelector: client.MatchingLabelsSelector{Selector: labelSelector}}
45+
Expect(newListOpts).To(Equal(expectedListOpts))
46+
})
47+
It("Should set LabelSelector to nothing with empty MatchingLabelsSelector", func() {
48+
newListOpts := &client.ListOptions{}
49+
newListOpts.ApplyOptions([]client.ListOption{client.MatchingLabelsSelector{}})
50+
expectedListOpts := &client.ListOptions{LabelSelector: client.MatchingLabelsSelector{Selector: labels.Nothing()}}
51+
Expect(newListOpts).To(Equal(expectedListOpts))
52+
})
3953
It("Should set FieldSelector", func() {
4054
o := &client.ListOptions{FieldSelector: fields.Nothing()}
4155
newListOpts := &client.ListOptions{}
4256
o.ApplyToList(newListOpts)
4357
Expect(newListOpts).To(Equal(o))
4458
})
59+
It("Should set FieldSelector with MatchingFieldsSelector", func() {
60+
newListOpts := &client.ListOptions{}
61+
newListOpts.ApplyOptions([]client.ListOption{client.MatchingFieldsSelector{Selector: fields.Nothing()}})
62+
expectedListOpts := &client.ListOptions{FieldSelector: client.MatchingFieldsSelector{Selector: fields.Nothing()}}
63+
Expect(newListOpts).To(Equal(expectedListOpts))
64+
})
65+
It("Should set FieldSelector to nothing with empty MatchingFieldsSelector", func() {
66+
newListOpts := &client.ListOptions{}
67+
newListOpts.ApplyOptions([]client.ListOption{client.MatchingFieldsSelector{}})
68+
expectedListOpts := &client.ListOptions{FieldSelector: client.MatchingFieldsSelector{Selector: fields.Nothing()}}
69+
Expect(newListOpts).To(Equal(expectedListOpts))
70+
})
4571
It("Should set Namespace", func() {
4672
o := &client.ListOptions{Namespace: "my-ns"}
4773
newListOpts := &client.ListOptions{}

0 commit comments

Comments
 (0)