Skip to content

Commit 76ed656

Browse files
committed
📖 Builder: Improve godocs
This updates the godocs in the builder to reference the current implementations of `source.Kind` correctly in the godocs that explain `For` and `Owns`. Also removes the warning in `WatchesRawSource`. With the introduction of the typed handler and predicates, this became a valid use case.
1 parent a39ace3 commit 76ed656

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

pkg/builder/controller.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/source"
3838
)
3939

40-
// Supporting mocking out functions for testing.
41-
var getGvk = apiutil.GVKForObject
42-
4340
// project represents other forms that we can use to
4441
// send/receive a given resource (metadata-only, unstructured, etc).
4542
type objectProjection int
@@ -90,8 +87,9 @@ type ForInput struct {
9087

9188
// For defines the type of Object being *reconciled*, and configures the ControllerManagedBy to respond to create / delete /
9289
// update events by *reconciling the object*.
90+
//
9391
// This is the equivalent of calling
94-
// Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{}).
92+
// Watches(source.Kind(cache, &Type{}, &handler.EnqueueRequestForObject{})).
9593
func (blder *TypedBuilder[request]) For(object client.Object, opts ...ForOption) *TypedBuilder[request] {
9694
if blder.forInput.object != nil {
9795
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 {
121119
// Use Owns(object, builder.MatchEveryOwner) to reconcile all owners.
122120
//
123121
// 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()))).
125123
func (blder *TypedBuilder[request]) Owns(object client.Object, opts ...OwnsOption) *TypedBuilder[request] {
126124
input := OwnsInput{object: object}
127125
for _, opt := range opts {
@@ -213,12 +211,10 @@ func (blder *TypedBuilder[request]) WatchesMetadata(
213211
}
214212

215213
// 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.
220214
//
221215
// 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.
222218
func (blder *TypedBuilder[request]) WatchesRawSource(src source.TypedSource[request]) *TypedBuilder[request] {
223219
blder.rawSources = append(blder.rawSources, src)
224220

@@ -227,7 +223,9 @@ func (blder *TypedBuilder[request]) WatchesRawSource(src source.TypedSource[requ
227223

228224
// WithEventFilter sets the event filters, to filter which create/update/delete/generic events eventually
229225
// 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+
//
231229
// Defaults to the empty list.
232230
func (blder *TypedBuilder[request]) WithEventFilter(p predicate.Predicate) *TypedBuilder[request] {
233231
blder.globalPredicates = append(blder.globalPredicates, p)
@@ -293,7 +291,7 @@ func (blder *TypedBuilder[request]) project(obj client.Object, proj objectProjec
293291
return obj, nil
294292
case projectAsMetadata:
295293
metaObj := &metav1.PartialObjectMetadata{}
296-
gvk, err := getGvk(obj, blder.mgr.GetScheme())
294+
gvk, err := apiutil.GVKForObject(obj, blder.mgr.GetScheme())
297295
if err != nil {
298296
return nil, fmt.Errorf("unable to determine GVK of %T for a metadata-only watch: %w", obj, err)
299297
}
@@ -365,7 +363,7 @@ func (blder *TypedBuilder[request]) doWatch() error {
365363
}
366364
allPredicates := append([]predicate.Predicate(nil), blder.globalPredicates...)
367365
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 {
369367
return err
370368
}
371369
}
@@ -404,7 +402,7 @@ func (blder *TypedBuilder[request]) doController(r reconcile.TypedReconciler[req
404402
hasGVK := blder.forInput.object != nil
405403
if hasGVK {
406404
var err error
407-
gvk, err = getGvk(blder.forInput.object, blder.mgr.GetScheme())
405+
gvk, err = apiutil.GVKForObject(blder.forInput.object, blder.mgr.GetScheme())
408406
if err != nil {
409407
return err
410408
}

0 commit comments

Comments
 (0)