@@ -41,14 +41,14 @@ import (
41
41
var newController = controller .New
42
42
var getGvk = apiutil .GVKForObject
43
43
44
- // project represents other forms that the we can use to
44
+ // project represents other forms that we can use to
45
45
// send/receive a given resource (metadata-only, unstructured, etc).
46
46
type objectProjection int
47
47
48
48
const (
49
49
// projectAsNormal doesn't change the object from the form given.
50
50
projectAsNormal objectProjection = iota
51
- // projectAsMetadata turns this into an metadata-only watch.
51
+ // projectAsMetadata turns this into a metadata-only watch.
52
52
projectAsMetadata
53
53
)
54
54
@@ -69,7 +69,7 @@ func ControllerManagedBy(m manager.Manager) *Builder {
69
69
return & Builder {mgr : m }
70
70
}
71
71
72
- // ForInput represents the information set by For method.
72
+ // ForInput represents the information set by the For method.
73
73
type ForInput struct {
74
74
object client.Object
75
75
predicates []predicate.Predicate
@@ -124,7 +124,7 @@ func (blder *Builder) Owns(object client.Object, opts ...OwnsOption) *Builder {
124
124
// WatchesInput represents the information set by Watches method.
125
125
type WatchesInput struct {
126
126
src source.Source
127
- eventhandler handler.EventHandler
127
+ eventHandler handler.EventHandler
128
128
predicates []predicate.Predicate
129
129
objectProjection objectProjection
130
130
}
@@ -133,16 +133,16 @@ 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(scheme , object), eventhandler , opts...).
137
- func (blder * Builder ) Watches (object client.Object , eventhandler handler.EventHandler , opts ... WatchesOption ) * Builder {
136
+ // WatchesRawSource(source.Kind(cache , object), eventHandler , opts...).
137
+ func (blder * Builder ) Watches (object client.Object , eventHandler handler.EventHandler , opts ... WatchesOption ) * Builder {
138
138
src := source .Kind (blder .mgr .GetCache (), object )
139
- return blder .WatchesRawSource (src , eventhandler , opts ... )
139
+ return blder .WatchesRawSource (src , eventHandler , opts ... )
140
140
}
141
141
142
142
// WatchesMetadata is the same as Watches, but forces the internal cache to only watch PartialObjectMetadata.
143
143
//
144
144
// This is useful when watching lots of objects, really big objects, or objects for which you only know
145
- // the GVK, but not the structure. You'll need to pass metav1.PartialObjectMetadata to the client
145
+ // the GVK, but not the structure. You'll need to pass metav1.PartialObjectMetadata to the client
146
146
// when fetching objects in your reconciler, otherwise you'll end up with a duplicate structured or unstructured cache.
147
147
//
148
148
// When watching a resource with metadata only, for example the v1.Pod, you should not Get and List using the v1.Pod type.
@@ -166,18 +166,18 @@ func (blder *Builder) Watches(object client.Object, eventhandler handler.EventHa
166
166
// In the first case, controller-runtime will create another cache for the
167
167
// concrete type on top of the metadata cache; this increases memory
168
168
// 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 {
169
+ func (blder * Builder ) WatchesMetadata (object client.Object , eventHandler handler.EventHandler , opts ... WatchesOption ) * Builder {
170
170
opts = append (opts , OnlyMetadata )
171
- return blder .Watches (object , eventhandler , opts ... )
171
+ return blder .Watches (object , eventHandler , opts ... )
172
172
}
173
173
174
174
// WatchesRawSource exposes the lower-level ControllerManagedBy Watches functions through the builder.
175
175
// Specified predicates are registered only for given source.
176
176
//
177
177
// STOP! Consider using For(...), Owns(...), Watches(...), WatchesMetadata(...) instead.
178
- // This method is only exposed for more advanced use cases, most users should use higher level functions.
179
- func (blder * Builder ) WatchesRawSource (src source.Source , eventhandler handler.EventHandler , opts ... WatchesOption ) * Builder {
180
- input := WatchesInput {src : src , eventhandler : eventhandler }
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
181
for _ , opt := range opts {
182
182
opt .ApplyToWatches (& input )
183
183
}
@@ -187,15 +187,15 @@ func (blder *Builder) WatchesRawSource(src source.Source, eventhandler handler.E
187
187
}
188
188
189
189
// WithEventFilter sets the event filters, to filter which create/update/delete/generic events eventually
190
- // trigger reconciliations. For example, filtering on whether the resource version has changed.
190
+ // trigger reconciliations. For example, filtering on whether the resource version has changed.
191
191
// Given predicate is added for all watched objects.
192
192
// Defaults to the empty list.
193
193
func (blder * Builder ) WithEventFilter (p predicate.Predicate ) * Builder {
194
194
blder .globalPredicates = append (blder .globalPredicates , p )
195
195
return blder
196
196
}
197
197
198
- // WithOptions overrides the controller options use in doController. Defaults to empty.
198
+ // WithOptions overrides the controller options used in doController. Defaults to empty.
199
199
func (blder * Builder ) WithOptions (options controller.Options ) * Builder {
200
200
blder .ctrlOptions = options
201
201
return blder
@@ -207,7 +207,7 @@ func (blder *Builder) WithLogConstructor(logConstructor func(*reconcile.Request)
207
207
return blder
208
208
}
209
209
210
- // Named sets the name of the controller to the given name. The name shows up
210
+ // Named sets the name of the controller to the given name. The name shows up
211
211
// in metrics, among other things, and thus should be a prometheus compatible name
212
212
// (underscores and alphanumeric characters only).
213
213
//
@@ -299,8 +299,7 @@ func (blder *Builder) doWatch() error {
299
299
blder .forInput .object ,
300
300
opts ... ,
301
301
)
302
- allPredicates := append ([]predicate.Predicate (nil ), blder .globalPredicates ... )
303
- allPredicates = append (allPredicates , own .predicates ... )
302
+ allPredicates := append (blder .globalPredicates , own .predicates ... )
304
303
if err := blder .ctrl .Watch (src , hdler , allPredicates ... ); err != nil {
305
304
return err
306
305
}
@@ -311,19 +310,16 @@ func (blder *Builder) doWatch() error {
311
310
return errors .New ("there are no watches configured, controller will never get triggered. Use For(), Owns() or Watches() to set them up" )
312
311
}
313
312
for _ , w := range blder .watchesInput {
314
- allPredicates := append ([]predicate.Predicate (nil ), blder .globalPredicates ... )
315
- allPredicates = append (allPredicates , w .predicates ... )
316
-
317
313
// If the source of this watch is of type Kind, project it.
318
- if srckind , ok := w .src .(* internalsource.Kind ); ok {
319
- typeForSrc , err := blder .project (srckind .Type , w .objectProjection )
314
+ if srcKind , ok := w .src .(* internalsource.Kind ); ok {
315
+ typeForSrc , err := blder .project (srcKind .Type , w .objectProjection )
320
316
if err != nil {
321
317
return err
322
318
}
323
- srckind .Type = typeForSrc
319
+ srcKind .Type = typeForSrc
324
320
}
325
-
326
- if err := blder .ctrl .Watch (w .src , w .eventhandler , allPredicates ... ); err != nil {
321
+ allPredicates := append ( blder . globalPredicates , w . predicates ... )
322
+ if err := blder .ctrl .Watch (w .src , w .eventHandler , allPredicates ... ); err != nil {
327
323
return err
328
324
}
329
325
}
@@ -344,12 +340,15 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
344
340
globalOpts := blder .mgr .GetControllerOptions ()
345
341
346
342
ctrlOptions := blder .ctrlOptions
343
+ if ctrlOptions .Reconciler != nil && r != nil {
344
+ return errors .New ("reconciler was set via WithOptions() and via Build() or Complete()" )
345
+ }
347
346
if ctrlOptions .Reconciler == nil {
348
347
ctrlOptions .Reconciler = r
349
348
}
350
349
351
350
// Retrieve the GVK from the object we're reconciling
352
- // to prepopulate logger information, and to optionally generate a default name.
351
+ // to pre-populate logger information, and to optionally generate a default name.
353
352
var gvk schema.GroupVersionKind
354
353
hasGVK := blder .forInput .object != nil
355
354
if hasGVK {
0 commit comments