Skip to content

Commit 5386351

Browse files
committed
add LabelSelectorPredicate function to controller library
1 parent c9a3b4a commit 5386351

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pkg/controller/predicates.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package controller
55
import (
66
"reflect"
77

8+
"k8s.io/apimachinery/pkg/labels"
89
"sigs.k8s.io/controller-runtime/pkg/client"
910
"sigs.k8s.io/controller-runtime/pkg/event"
1011
"sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -179,6 +180,22 @@ func LostLabelPredicate(key, val string) predicate.Predicate {
179180
}
180181
}
181182

183+
// LabelSelectorPredicate returns a predicate based on a label selector.
184+
// Opposed to the similarly named function from the controller-runtime library, this one works on label.Selector
185+
// instead of metav1.LabelSelector.
186+
func LabelSelectorPredicate(sel labels.Selector) predicate.Predicate {
187+
return predicate.NewPredicateFuncs(func(obj client.Object) bool {
188+
if obj == nil {
189+
return false
190+
}
191+
ls := obj.GetLabels()
192+
if ls == nil {
193+
ls = map[string]string{}
194+
}
195+
return sel.Matches(labels.Set(ls))
196+
})
197+
}
198+
182199
/////////////////////////
183200
/// STATUS PREDICATES ///
184201
/////////////////////////

pkg/controller/predicates_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
corev1 "k8s.io/api/core/v1"
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/labels"
910
"k8s.io/utils/ptr"
1011
"sigs.k8s.io/controller-runtime/pkg/client"
1112
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -114,8 +115,24 @@ var _ = Describe("Predicates", func() {
114115
It("should detect changes to the labels", func() {
115116
pHasFoo := ctrlutils.HasLabelPredicate("foo", "")
116117
pHasBar := ctrlutils.HasLabelPredicate("bar", "")
118+
matchesEverything := ctrlutils.LabelSelectorPredicate(labels.Everything())
119+
matchesNothing := ctrlutils.LabelSelectorPredicate(labels.Nothing())
117120
pHasFooWithFoo := ctrlutils.HasLabelPredicate("foo", "foo")
118121
pHasFooWithBar := ctrlutils.HasLabelPredicate("foo", "bar")
122+
fooWithFooSelector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
123+
MatchLabels: map[string]string{
124+
"foo": "foo",
125+
},
126+
})
127+
Expect(err).ToNot(HaveOccurred())
128+
fooWithBarSelector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
129+
MatchLabels: map[string]string{
130+
"foo": "bar",
131+
},
132+
})
133+
Expect(err).ToNot(HaveOccurred())
134+
pHasFooWithFooViaSelector := ctrlutils.LabelSelectorPredicate(fooWithFooSelector)
135+
pHasFooWithBarViaSelector := ctrlutils.LabelSelectorPredicate(fooWithBarSelector)
119136
pGotFoo := ctrlutils.GotLabelPredicate("foo", "")
120137
pGotFooWithFoo := ctrlutils.GotLabelPredicate("foo", "foo")
121138
pGotFooWithBar := ctrlutils.GotLabelPredicate("foo", "bar")
@@ -124,9 +141,13 @@ var _ = Describe("Predicates", func() {
124141
pLostFooWithBar := ctrlutils.LostLabelPredicate("foo", "bar")
125142
By("old and new resource are equal")
126143
e := updateEvent(base, changed)
144+
Expect(matchesEverything.Update(e)).To(BeTrue(), "'everything' LabelSelector should always match")
145+
Expect(matchesNothing.Update(e)).To(BeFalse(), "'nothing' LabelSelector should never match")
127146
Expect(pHasFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
128147
Expect(pHasFooWithFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
148+
Expect(pHasFooWithFooViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
129149
Expect(pHasFooWithBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
150+
Expect(pHasFooWithBarViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return falseif the labels are not matched")
130151
Expect(pGotFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
131152
Expect(pGotFooWithFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
132153
Expect(pGotFooWithBar.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
@@ -138,10 +159,14 @@ var _ = Describe("Predicates", func() {
138159
"foo": "foo",
139160
})
140161
e = updateEvent(base, changed)
162+
Expect(matchesEverything.Update(e)).To(BeTrue(), "'everything' LabelSelector should always match")
163+
Expect(matchesNothing.Update(e)).To(BeFalse(), "'nothing' LabelSelector should never match")
141164
Expect(pHasFoo.Update(e)).To(BeTrue(), "HasLabelPredicate should return true if the label is there")
142165
Expect(pHasBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if the label is not there")
143166
Expect(pHasFooWithFoo.Update(e)).To(BeTrue(), "HasLabelPredicate should return true if the label is there and has the fitting value")
167+
Expect(pHasFooWithFooViaSelector.Update(e)).To(BeTrue(), "LabelSelectorPredicate should return true if the labels are matched")
144168
Expect(pHasFooWithBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if the label is there but has the wrong value")
169+
Expect(pHasFooWithBarViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
145170
Expect(pGotFoo.Update(e)).To(BeTrue(), "GotLabelPredicate should return true if the label was added")
146171
Expect(pGotFooWithFoo.Update(e)).To(BeTrue(), "GotLabelPredicate should return true if the label was added with the correct value")
147172
Expect(pGotFooWithBar.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if the label was added but with the wrong value")
@@ -154,10 +179,14 @@ var _ = Describe("Predicates", func() {
154179
"foo": "bar",
155180
})
156181
e = updateEvent(base, changed)
182+
Expect(matchesEverything.Update(e)).To(BeTrue(), "'everything' LabelSelector should always match")
183+
Expect(matchesNothing.Update(e)).To(BeFalse(), "'nothing' LabelSelector should never match")
157184
Expect(pHasFoo.Update(e)).To(BeTrue(), "HasLabelPredicate should return true if the label is there")
158185
Expect(pHasBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if the label is not there")
159186
Expect(pHasFooWithFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if the label is there but has the wrong value")
187+
Expect(pHasFooWithFooViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
160188
Expect(pHasFooWithBar.Update(e)).To(BeTrue(), "HasLabelPredicate should return true if the label is there and has the fitting value")
189+
Expect(pHasFooWithBarViaSelector.Update(e)).To(BeTrue(), "LabelSelectorPredicate should return true if the labels are matched")
161190
Expect(pGotFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if the label was there before")
162191
Expect(pGotFooWithFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if the label was changed but to the wrong value")
163192
Expect(pGotFooWithBar.Update(e)).To(BeTrue(), "GotLabelPredicate should return true if the label was changed to the correct value")
@@ -168,9 +197,13 @@ var _ = Describe("Predicates", func() {
168197
base = changed.DeepCopy()
169198
changed.SetLabels(nil)
170199
e = updateEvent(base, changed)
200+
Expect(matchesEverything.Update(e)).To(BeTrue(), "'everything' LabelSelector should always match")
201+
Expect(matchesNothing.Update(e)).To(BeFalse(), "'nothing' LabelSelector should never match")
171202
Expect(pHasFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
172203
Expect(pHasFooWithFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
204+
Expect(pHasFooWithFooViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
173205
Expect(pHasFooWithBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
206+
Expect(pHasFooWithBarViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
174207
Expect(pGotFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
175208
Expect(pGotFooWithFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
176209
Expect(pGotFooWithBar.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")

0 commit comments

Comments
 (0)