@@ -18,8 +18,8 @@ package predicate
18
18
19
19
import (
20
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21
+ "k8s.io/apimachinery/pkg/labels"
21
22
"k8s.io/apimachinery/pkg/runtime"
22
-
23
23
"sigs.k8s.io/controller-runtime/pkg/event"
24
24
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
25
25
)
@@ -270,3 +270,48 @@ func (o or) Generic(e event.GenericEvent) bool {
270
270
}
271
271
return false
272
272
}
273
+
274
+ // LabelSelectorPredicate implements predicate functions and requires a selector that will filter an event by its labels.
275
+ //
276
+ // This predicate will skip reconciliation on all events that have labels that do not match the selector of the predicate.
277
+ // A metav1.LabelSelector is passed as an input parameter while instantiating an LabelSelectorPredicate, and this is (explicitly)
278
+ // converted to a labels.Selector object, and stored within the struct. When a predicate function is evoked, the event labels are checked
279
+ // for a match with the predicate's selector to decide whether an event undergoes reconciliation or not.
280
+ // If the labels match, the event will undergo reconciliation. If the labels do not match, it will skip reconciliation for that particular event.
281
+
282
+ type LabelSelectorPredicate struct {
283
+ Funcs
284
+ Selector labels.Selector
285
+ }
286
+
287
+ // eventFilter skips reconciliation of events that have labels matching selectors
288
+ func (r LabelSelectorPredicate ) eventFilter (eventLabels map [string ]string ) bool {
289
+ return r .Selector .Matches (labels .Set (eventLabels ))
290
+ }
291
+
292
+ // NewLabelSelectorPredicate instantiates a new LabelSelectorPredicate with the LabelSelector specified through parameters.
293
+ func NewLabelSelectorPredicate (s metav1.LabelSelector ) (Predicate , error ) {
294
+ selectorSpecs , err := metav1 .LabelSelectorAsSelector (& s )
295
+ requirements := LabelSelectorPredicate {Selector : selectorSpecs }
296
+ return requirements , err
297
+ }
298
+
299
+ // Update implements default UpdateEvent filter for validating event labels against the predicate selector
300
+ func (r LabelSelectorPredicate ) Update (e event.UpdateEvent ) bool {
301
+ return r .eventFilter (e .MetaNew .GetLabels ())
302
+ }
303
+
304
+ // Create implements default CreateEvent filter for validating event labels against the predicate selector
305
+ func (r LabelSelectorPredicate ) Create (e event.CreateEvent ) bool {
306
+ return r .eventFilter (e .Meta .GetLabels ())
307
+ }
308
+
309
+ // Delete implements default DeleteEvent filter for validating event labels against the predicate selector
310
+ func (r LabelSelectorPredicate ) Delete (e event.DeleteEvent ) bool {
311
+ return r .eventFilter (e .Meta .GetLabels ())
312
+ }
313
+
314
+ // Generic implements default GenericEvent filter for validating event labels against the predicate selector
315
+ func (r LabelSelectorPredicate ) Generic (e event.GenericEvent ) bool {
316
+ return r .eventFilter (e .Meta .GetLabels ())
317
+ }
0 commit comments