@@ -37,9 +37,6 @@ import (
37
37
"sigs.k8s.io/controller-runtime/pkg/source"
38
38
)
39
39
40
- // Supporting mocking out functions for testing.
41
- var getGvk = apiutil .GVKForObject
42
-
43
40
// project represents other forms that we can use to
44
41
// send/receive a given resource (metadata-only, unstructured, etc).
45
42
type objectProjection int
@@ -90,8 +87,9 @@ type ForInput struct {
90
87
91
88
// For defines the type of Object being *reconciled*, and configures the ControllerManagedBy to respond to create / delete /
92
89
// update events by *reconciling the object*.
90
+ //
93
91
// This is the equivalent of calling
94
- // Watches(& source.Kind{Type: apiType }, &handler.EnqueueRequestForObject{}).
92
+ // Watches(source.Kind(cache, &Type{ }, &handler.EnqueueRequestForObject{}) ).
95
93
func (blder * TypedBuilder [request ]) For (object client.Object , opts ... ForOption ) * TypedBuilder [request ] {
96
94
if blder .forInput .object != nil {
97
95
blder .forInput .err = fmt .Errorf ("For(...) should only be called once, could not assign multiple objects for reconciliation" )
@@ -121,7 +119,7 @@ type OwnsInput struct {
121
119
// Use Owns(object, builder.MatchEveryOwner) to reconcile all owners.
122
120
//
123
121
// By default, this is the equivalent of calling
124
- // Watches(object, handler.EnqueueRequestForOwner([...], ownerType , OnlyControllerOwner())).
122
+ // Watches(source.Kind(cache, &Type{}, handler.EnqueueRequestForOwner([...], &OwnerType{} , OnlyControllerOwner() ))).
125
123
func (blder * TypedBuilder [request ]) Owns (object client.Object , opts ... OwnsOption ) * TypedBuilder [request ] {
126
124
input := OwnsInput {object : object }
127
125
for _ , opt := range opts {
@@ -213,12 +211,10 @@ func (blder *TypedBuilder[request]) WatchesMetadata(
213
211
}
214
212
215
213
// WatchesRawSource exposes the lower-level ControllerManagedBy Watches functions through the builder.
216
- // Specified predicates are registered only for given source.
217
- //
218
- // STOP! Consider using For(...), Owns(...), Watches(...), WatchesMetadata(...) instead.
219
- // This method is only exposed for more advanced use cases, most users should use one of the higher level functions.
220
214
//
221
215
// WatchesRawSource does not respect predicates configured through WithEventFilter.
216
+ //
217
+ // WatchesRawSource makes it possible to use typed handlers and predicates with `source.Kind` as well as custom source implementations.
222
218
func (blder * TypedBuilder [request ]) WatchesRawSource (src source.TypedSource [request ]) * TypedBuilder [request ] {
223
219
blder .rawSources = append (blder .rawSources , src )
224
220
@@ -227,7 +223,9 @@ func (blder *TypedBuilder[request]) WatchesRawSource(src source.TypedSource[requ
227
223
228
224
// WithEventFilter sets the event filters, to filter which create/update/delete/generic events eventually
229
225
// trigger reconciliations. For example, filtering on whether the resource version has changed.
230
- // Given predicate is added for all watched objects.
226
+ // Given predicate is added for all watched objects and thus must be able to deal with the type
227
+ // of all watched objects.
228
+ //
231
229
// Defaults to the empty list.
232
230
func (blder * TypedBuilder [request ]) WithEventFilter (p predicate.Predicate ) * TypedBuilder [request ] {
233
231
blder .globalPredicates = append (blder .globalPredicates , p )
@@ -293,7 +291,7 @@ func (blder *TypedBuilder[request]) project(obj client.Object, proj objectProjec
293
291
return obj , nil
294
292
case projectAsMetadata :
295
293
metaObj := & metav1.PartialObjectMetadata {}
296
- gvk , err := getGvk (obj , blder .mgr .GetScheme ())
294
+ gvk , err := apiutil . GVKForObject (obj , blder .mgr .GetScheme ())
297
295
if err != nil {
298
296
return nil , fmt .Errorf ("unable to determine GVK of %T for a metadata-only watch: %w" , obj , err )
299
297
}
@@ -365,7 +363,7 @@ func (blder *TypedBuilder[request]) doWatch() error {
365
363
}
366
364
allPredicates := append ([]predicate.Predicate (nil ), blder .globalPredicates ... )
367
365
allPredicates = append (allPredicates , w .predicates ... )
368
- if err := blder .ctrl .Watch (source .TypedKind [client. Object , request ] (blder .mgr .GetCache (), projected , w .handler , allPredicates ... )); err != nil {
366
+ if err := blder .ctrl .Watch (source .TypedKind (blder .mgr .GetCache (), projected , w .handler , allPredicates ... )); err != nil {
369
367
return err
370
368
}
371
369
}
@@ -404,7 +402,7 @@ func (blder *TypedBuilder[request]) doController(r reconcile.TypedReconciler[req
404
402
hasGVK := blder .forInput .object != nil
405
403
if hasGVK {
406
404
var err error
407
- gvk , err = getGvk (blder .forInput .object , blder .mgr .GetScheme ())
405
+ gvk , err = apiutil . GVKForObject (blder .forInput .object , blder .mgr .GetScheme ())
408
406
if err != nil {
409
407
return err
410
408
}
0 commit comments