Skip to content

Commit 5c8150f

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

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-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: 28 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,23 @@ 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())
117119
pHasFooWithFoo := ctrlutils.HasLabelPredicate("foo", "foo")
118120
pHasFooWithBar := ctrlutils.HasLabelPredicate("foo", "bar")
121+
fooWithFooSelector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
122+
MatchLabels: map[string]string{
123+
"foo": "foo",
124+
},
125+
})
126+
Expect(err).ToNot(HaveOccurred())
127+
fooWithBarSelector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
128+
MatchLabels: map[string]string{
129+
"foo": "bar",
130+
},
131+
})
132+
Expect(err).ToNot(HaveOccurred())
133+
pHasFooWithFooViaSelector := ctrlutils.LabelSelectorPredicate(fooWithFooSelector)
134+
pHasFooWithBarViaSelector := ctrlutils.LabelSelectorPredicate(fooWithBarSelector)
119135
pGotFoo := ctrlutils.GotLabelPredicate("foo", "")
120136
pGotFooWithFoo := ctrlutils.GotLabelPredicate("foo", "foo")
121137
pGotFooWithBar := ctrlutils.GotLabelPredicate("foo", "bar")
@@ -124,9 +140,12 @@ var _ = Describe("Predicates", func() {
124140
pLostFooWithBar := ctrlutils.LostLabelPredicate("foo", "bar")
125141
By("old and new resource are equal")
126142
e := updateEvent(base, changed)
143+
Expect(matchesEverything.Update(e)).To(BeTrue(), "'everything' LabelSelector should always match")
127144
Expect(pHasFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
128145
Expect(pHasFooWithFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
146+
Expect(pHasFooWithFooViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
129147
Expect(pHasFooWithBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
148+
Expect(pHasFooWithBarViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return falseif the labels are not matched")
130149
Expect(pGotFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
131150
Expect(pGotFooWithFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
132151
Expect(pGotFooWithBar.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
@@ -138,10 +157,13 @@ var _ = Describe("Predicates", func() {
138157
"foo": "foo",
139158
})
140159
e = updateEvent(base, changed)
160+
Expect(matchesEverything.Update(e)).To(BeTrue(), "'everything' LabelSelector should always match")
141161
Expect(pHasFoo.Update(e)).To(BeTrue(), "HasLabelPredicate should return true if the label is there")
142162
Expect(pHasBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if the label is not there")
143163
Expect(pHasFooWithFoo.Update(e)).To(BeTrue(), "HasLabelPredicate should return true if the label is there and has the fitting value")
164+
Expect(pHasFooWithFooViaSelector.Update(e)).To(BeTrue(), "LabelSelectorPredicate should return true if the labels are matched")
144165
Expect(pHasFooWithBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if the label is there but has the wrong value")
166+
Expect(pHasFooWithBarViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
145167
Expect(pGotFoo.Update(e)).To(BeTrue(), "GotLabelPredicate should return true if the label was added")
146168
Expect(pGotFooWithFoo.Update(e)).To(BeTrue(), "GotLabelPredicate should return true if the label was added with the correct value")
147169
Expect(pGotFooWithBar.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if the label was added but with the wrong value")
@@ -154,10 +176,13 @@ var _ = Describe("Predicates", func() {
154176
"foo": "bar",
155177
})
156178
e = updateEvent(base, changed)
179+
Expect(matchesEverything.Update(e)).To(BeTrue(), "'everything' LabelSelector should always match")
157180
Expect(pHasFoo.Update(e)).To(BeTrue(), "HasLabelPredicate should return true if the label is there")
158181
Expect(pHasBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if the label is not there")
159182
Expect(pHasFooWithFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if the label is there but has the wrong value")
183+
Expect(pHasFooWithFooViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
160184
Expect(pHasFooWithBar.Update(e)).To(BeTrue(), "HasLabelPredicate should return true if the label is there and has the fitting value")
185+
Expect(pHasFooWithBarViaSelector.Update(e)).To(BeTrue(), "LabelSelectorPredicate should return true if the labels are matched")
161186
Expect(pGotFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if the label was there before")
162187
Expect(pGotFooWithFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if the label was changed but to the wrong value")
163188
Expect(pGotFooWithBar.Update(e)).To(BeTrue(), "GotLabelPredicate should return true if the label was changed to the correct value")
@@ -168,9 +193,12 @@ var _ = Describe("Predicates", func() {
168193
base = changed.DeepCopy()
169194
changed.SetLabels(nil)
170195
e = updateEvent(base, changed)
196+
Expect(matchesEverything.Update(e)).To(BeTrue(), "'everything' LabelSelector should always match")
171197
Expect(pHasFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
172198
Expect(pHasFooWithFoo.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
199+
Expect(pHasFooWithFooViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
173200
Expect(pHasFooWithBar.Update(e)).To(BeFalse(), "HasLabelPredicate should return false if there are no labels")
201+
Expect(pHasFooWithBarViaSelector.Update(e)).To(BeFalse(), "LabelSelectorPredicate should return false if the labels are not matched")
174202
Expect(pGotFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
175203
Expect(pGotFooWithFoo.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")
176204
Expect(pGotFooWithBar.Update(e)).To(BeFalse(), "GotLabelPredicate should return false if there are no labels")

0 commit comments

Comments
 (0)