@@ -21,6 +21,7 @@ import (
21
21
22
22
"github.com/go-logr/logr"
23
23
"sigs.k8s.io/cluster-api/util/annotations"
24
+ "sigs.k8s.io/cluster-api/util/labels"
24
25
25
26
"sigs.k8s.io/controller-runtime/pkg/client"
26
27
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -127,6 +128,25 @@ func Any(logger logr.Logger, predicates ...predicate.Funcs) predicate.Funcs {
127
128
}
128
129
}
129
130
131
+ // ResourceHasLabel returns a predicate that returns true only if the provided resource contains
132
+ // a label with the WatchLabel key and the configured label value exactly.
133
+ func ResourceHasLabel (logger logr.Logger , labelValue string ) predicate.Funcs {
134
+ return predicate.Funcs {
135
+ UpdateFunc : func (e event.UpdateEvent ) bool {
136
+ return processIfLabelMatch (logger .WithValues ("predicate" , "updateEvent" ), e .ObjectNew , labelValue )
137
+ },
138
+ CreateFunc : func (e event.CreateEvent ) bool {
139
+ return processIfLabelMatch (logger .WithValues ("predicate" , "createEvent" ), e .Object , labelValue )
140
+ },
141
+ DeleteFunc : func (e event.DeleteEvent ) bool {
142
+ return processIfLabelMatch (logger .WithValues ("predicate" , "deleteEvent" ), e .Object , labelValue )
143
+ },
144
+ GenericFunc : func (e event.GenericEvent ) bool {
145
+ return processIfLabelMatch (logger .WithValues ("predicate" , "genericEvent" ), e .Object , labelValue )
146
+ },
147
+ }
148
+ }
149
+
130
150
// ResourceNotPaused returns a Predicate that returns true only if the provided resource does not contain the
131
151
// paused annotation.
132
152
// This implements a common requirement for all cluster-api and provider controllers skip reconciliation when the paused
@@ -157,6 +177,12 @@ func ResourceNotPaused(logger logr.Logger) predicate.Funcs {
157
177
}
158
178
}
159
179
180
+ // ResourceNotPausedAndHasLabel returns a predicate that returns true only if the
181
+ // ResourceNotPaused and ResourceHasLabel predicates return true.
182
+ func ResourceNotPausedAndHasLabel (logger logr.Logger , labelValue string ) predicate.Funcs {
183
+ return All (logger , ResourceNotPaused (logger ), ResourceHasLabel (logger , labelValue ))
184
+ }
185
+
160
186
func processIfNotPaused (logger logr.Logger , obj client.Object ) bool {
161
187
kind := strings .ToLower (obj .GetObjectKind ().GroupVersionKind ().Kind )
162
188
log := logger .WithValues ("namespace" , obj .GetNamespace (), kind , obj .GetName ())
@@ -167,3 +193,19 @@ func processIfNotPaused(logger logr.Logger, obj client.Object) bool {
167
193
log .V (4 ).Info ("Resource is not paused, will attempt to map resource" )
168
194
return true
169
195
}
196
+
197
+ func processIfLabelMatch (logger logr.Logger , obj client.Object , labelValue string ) bool {
198
+ // Return early if no labelValue was set.
199
+ if labelValue == "" {
200
+ return true
201
+ }
202
+
203
+ kind := strings .ToLower (obj .GetObjectKind ().GroupVersionKind ().Kind )
204
+ log := logger .WithValues ("namespace" , obj .GetNamespace (), kind , obj .GetName ())
205
+ if labels .HasWatchLabel (obj , labelValue ) {
206
+ log .V (4 ).Info ("Resource matches label, will attempt to map resource" )
207
+ return true
208
+ }
209
+ log .V (4 ).Info ("Resource does not match label, will not attempt to map resource" )
210
+ return false
211
+ }
0 commit comments