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