@@ -53,14 +53,15 @@ const (
53
53
54
54
// Builder builds a Controller.
55
55
type Builder struct {
56
- forInput ForInput
57
- ownsInput []OwnsInput
58
- watchesInput []WatchesInput
59
- mgr manager.Manager
60
- globalPredicates []predicate.Predicate
61
- ctrl controller.Controller
62
- ctrlOptions controller.Options
63
- name string
56
+ forInput ForInput
57
+ ownsInput []OwnsInput
58
+ watchesObjectInput []WatchesObjectInput
59
+ watchesSourceInput []WatchesSourceInput
60
+ mgr manager.Manager
61
+ globalPredicates []predicate.Predicate
62
+ ctrl controller.Controller
63
+ ctrlOptions controller.Options
64
+ name string
64
65
}
65
66
66
67
// ControllerManagedBy returns a new controller builder that will be started by the provided Manager.
@@ -79,7 +80,7 @@ type ForInput struct {
79
80
// For defines the type of Object being *reconciled*, and configures the ControllerManagedBy to respond to create / delete /
80
81
// update events by *reconciling the object*.
81
82
// This is the equivalent of calling
82
- // Watches(& source.Kind{Type: apiType} , &handler.EnqueueRequestForObject{}).
83
+ // Watches(source.Kind(mgr.GetCache(), apiType, &handler.EnqueueRequestForObject{}) ).
83
84
func (blder * Builder ) For (object client.Object , opts ... ForOption ) * Builder {
84
85
if blder .forInput .object != nil {
85
86
blder .forInput .err = fmt .Errorf ("For(...) should only be called once, could not assign multiple objects for reconciliation" )
@@ -120,10 +121,10 @@ func (blder *Builder) Owns(object client.Object, opts ...OwnsOption) *Builder {
120
121
return blder
121
122
}
122
123
123
- // WatchesInput represents the information set by Watches method.
124
- type WatchesInput struct {
125
- src source. Source
126
- eventHandler handler.EventHandler
124
+ // WatchesObjectInput represents the information set by Watches method.
125
+ type WatchesObjectInput struct {
126
+ object client. Object
127
+ eventhandler handler.EventHandler
127
128
predicates []predicate.Predicate
128
129
objectProjection objectProjection
129
130
}
@@ -132,10 +133,15 @@ type WatchesInput struct {
132
133
// update events by *reconciling the object* with the given EventHandler.
133
134
//
134
135
// This is the equivalent of calling
135
- // WatchesRawSource(source.Kind(cache, object), eventHandler, opts...).
136
- func (blder * Builder ) Watches (object client.Object , eventHandler handler.EventHandler , opts ... WatchesOption ) * Builder {
137
- src := source .Kind (blder .mgr .GetCache (), object )
138
- return blder .WatchesRawSource (src , eventHandler , opts ... )
136
+ // WatchesRawSource(source.Kind(scheme, object, eventhandler, opts...)).
137
+ func (blder * Builder ) Watches (object client.Object , eventhandler handler.EventHandler , opts ... WatchesObjectOption ) * Builder {
138
+ input := WatchesObjectInput {object : object , eventhandler : eventhandler }
139
+ for _ , opt := range opts {
140
+ opt .ApplyToWatchesObject (& input )
141
+ }
142
+
143
+ blder .watchesObjectInput = append (blder .watchesObjectInput , input )
144
+ return blder
139
145
}
140
146
141
147
// WatchesMetadata is the same as Watches, but forces the internal cache to only watch PartialObjectMetadata.
@@ -165,29 +171,30 @@ func (blder *Builder) Watches(object client.Object, eventHandler handler.EventHa
165
171
// In the first case, controller-runtime will create another cache for the
166
172
// concrete type on top of the metadata cache; this increases memory
167
173
// consumption and leads to race conditions as caches are not in sync.
168
- func (blder * Builder ) WatchesMetadata (object client.Object , eventHandler handler.EventHandler , opts ... WatchesOption ) * Builder {
174
+ func (blder * Builder ) WatchesMetadata (object client.Object , eventhandler handler.EventHandler , opts ... WatchesObjectOption ) * Builder {
169
175
opts = append (opts , OnlyMetadata )
170
- return blder .Watches (object , eventHandler , opts ... )
176
+ return blder .Watches (object , eventhandler , opts ... )
177
+ }
178
+
179
+ // WatchesSourceInput represents the information set by Watches method.
180
+ type WatchesSourceInput struct {
181
+ src source.Source
171
182
}
172
183
173
184
// WatchesRawSource exposes the lower-level ControllerManagedBy Watches functions through the builder.
174
- // Specified predicates are registered only for given source.
175
185
//
176
186
// STOP! Consider using For(...), Owns(...), Watches(...), WatchesMetadata(...) instead.
177
- // This method is only exposed for more advanced use cases, most users should use one of the higher level functions.
178
- func (blder * Builder ) WatchesRawSource (src source.Source , eventHandler handler.EventHandler , opts ... WatchesOption ) * Builder {
179
- input := WatchesInput {src : src , eventHandler : eventHandler }
180
- for _ , opt := range opts {
181
- opt .ApplyToWatches (& input )
182
- }
183
-
184
- blder .watchesInput = append (blder .watchesInput , input )
187
+ // This method is only exposed for more advanced use cases, most users should use higher level functions.
188
+ // This method does generally disregard all the global configuration set by the builder.
189
+ func (blder * Builder ) WatchesRawSource (src source.Source ) * Builder {
190
+ blder .watchesSourceInput = append (blder .watchesSourceInput , WatchesSourceInput {src : src })
185
191
return blder
186
192
}
187
193
188
194
// WithEventFilter sets the event filters, to filter which create/update/delete/generic events eventually
189
195
// trigger reconciliations. For example, filtering on whether the resource version has changed.
190
196
// Given predicate is added for all watched objects.
197
+ // The predicates are not applied to sources watched with WatchesRawSource(...).
191
198
// Defaults to the empty list.
192
199
func (blder * Builder ) WithEventFilter (p predicate.Predicate ) * Builder {
193
200
blder .globalPredicates = append (blder .globalPredicates , p )
@@ -271,11 +278,14 @@ func (blder *Builder) doWatch() error {
271
278
if err != nil {
272
279
return err
273
280
}
274
- src := source .Kind (blder .mgr .GetCache (), obj )
275
- hdler := & handler.EnqueueRequestForObject {}
276
281
allPredicates := append ([]predicate.Predicate (nil ), blder .globalPredicates ... )
277
282
allPredicates = append (allPredicates , blder .forInput .predicates ... )
278
- if err := blder .ctrl .Watch (src , hdler , allPredicates ... ); err != nil {
283
+ src := source .Kind (
284
+ blder .mgr .GetCache (),
285
+ obj ,
286
+ handler .WithPredicates (& handler.EnqueueRequestForObject {}, allPredicates ... ),
287
+ )
288
+ if err := blder .ctrl .Watch (src ); err != nil {
279
289
return err
280
290
}
281
291
}
@@ -289,7 +299,6 @@ func (blder *Builder) doWatch() error {
289
299
if err != nil {
290
300
return err
291
301
}
292
- src := source .Kind (blder .mgr .GetCache (), obj )
293
302
opts := []handler.OwnerOption {}
294
303
if ! own .matchEveryOwner {
295
304
opts = append (opts , handler .OnlyControllerOwner ())
@@ -301,32 +310,43 @@ func (blder *Builder) doWatch() error {
301
310
)
302
311
allPredicates := append ([]predicate.Predicate (nil ), blder .globalPredicates ... )
303
312
allPredicates = append (allPredicates , own .predicates ... )
304
- if err := blder .ctrl .Watch (src , hdler , allPredicates ... ); err != nil {
313
+ src := source .Kind (
314
+ blder .mgr .GetCache (),
315
+ obj ,
316
+ handler .WithPredicates (hdler , allPredicates ... ),
317
+ )
318
+ if err := blder .ctrl .Watch (src ); err != nil {
305
319
return err
306
320
}
307
321
}
308
322
309
323
// Do the watch requests
310
- if len (blder .watchesInput ) == 0 && blder .forInput .object == nil {
324
+ if len (blder .watchesObjectInput ) == 0 && len ( blder . watchesSourceInput ) == 0 && blder .forInput .object == nil {
311
325
return errors .New ("there are no watches configured, controller will never get triggered. Use For(), Owns() or Watches() to set them up" )
312
326
}
313
- for _ , w := range blder .watchesInput {
314
- // If the source of this watch is of type Kind, project it.
315
- if srcKind , ok := w .src .(interface {
316
- ProjectObject (func (client.Object ) (client.Object , error )) error
317
- }); ok {
318
- if err := srcKind .ProjectObject (func (o client.Object ) (client.Object , error ) {
319
- return blder .project (o , w .objectProjection )
320
- }); err != nil {
321
- return err
322
- }
327
+ for _ , w := range blder .watchesObjectInput {
328
+ obj , err := blder .project (w .object , w .objectProjection )
329
+ if err != nil {
330
+ return err
323
331
}
332
+
324
333
allPredicates := append ([]predicate.Predicate (nil ), blder .globalPredicates ... )
325
334
allPredicates = append (allPredicates , w .predicates ... )
326
- if err := blder .ctrl .Watch (w .src , w .eventHandler , allPredicates ... ); err != nil {
335
+ src := source .Kind (
336
+ blder .mgr .GetCache (), obj ,
337
+ handler .WithPredicates (w .eventhandler , allPredicates ... ),
338
+ )
339
+ if err := blder .ctrl .Watch (src ); err != nil {
327
340
return err
328
341
}
329
342
}
343
+
344
+ for _ , w := range blder .watchesSourceInput {
345
+ if err := blder .ctrl .Watch (w .src ); err != nil {
346
+ return err
347
+ }
348
+ }
349
+
330
350
return nil
331
351
}
332
352
0 commit comments