Skip to content

Commit 5b6b801

Browse files
committed
refactor(builder): Consolidate validation logic and simplify duplicated watch-related code
1 parent 9f93124 commit 5b6b801

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

pkg/builder/controller.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,18 @@ func (blder *TypedBuilder[request]) Build(r reconcile.TypedReconciler[request])
273273
if blder.forInput.err != nil {
274274
return nil, blder.forInput.err
275275
}
276+
if blder.forInput.object == nil {
277+
if len(blder.ownsInput) > 0 {
278+
return nil, errors.New("Owns() can only be used together with For()")
279+
}
280+
if len(blder.watchesInput) == 0 && len(blder.rawSources) == 0 {
281+
return nil, errors.New("there are no watches configured, controller will never get triggered. Use For(), Owns(), Watches() or WatchesRawSource() to set them up")
282+
}
283+
} else {
284+
if reflect.TypeFor[request]() != reflect.TypeOf(reconcile.Request{}) {
285+
return nil, fmt.Errorf("For() can only be used with reconcile.Request, got %T", *new(request))
286+
}
287+
}
276288

277289
// Set the ControllerManagedBy
278290
if err := blder.doController(r); err != nil {
@@ -305,31 +317,25 @@ func (blder *TypedBuilder[request]) project(obj client.Object, proj objectProjec
305317
}
306318

307319
func (blder *TypedBuilder[request]) doWatch() error {
320+
321+
var sources []source.TypedSource[request]
322+
308323
// Reconcile type
309324
if blder.forInput.object != nil {
310325
obj, err := blder.project(blder.forInput.object, blder.forInput.objectProjection)
311326
if err != nil {
312327
return err
313328
}
314329

315-
if reflect.TypeFor[request]() != reflect.TypeOf(reconcile.Request{}) {
316-
return fmt.Errorf("For() can only be used with reconcile.Request, got %T", *new(request))
317-
}
318-
319330
var hdler handler.TypedEventHandler[client.Object, request]
320331
reflect.ValueOf(&hdler).Elem().Set(reflect.ValueOf(&handler.EnqueueRequestForObject{}))
321332
allPredicates := append([]predicate.Predicate(nil), blder.globalPredicates...)
322333
allPredicates = append(allPredicates, blder.forInput.predicates...)
323334
src := source.TypedKind(blder.mgr.GetCache(), obj, hdler, allPredicates...)
324-
if err := blder.ctrl.Watch(src); err != nil {
325-
return err
326-
}
335+
sources = append(sources, src)
327336
}
328337

329338
// Watches the managed types
330-
if len(blder.ownsInput) > 0 && blder.forInput.object == nil {
331-
return errors.New("Owns() can only be used together with For()")
332-
}
333339
for _, own := range blder.ownsInput {
334340
obj, err := blder.project(own.object, own.objectProjection)
335341
if err != nil {
@@ -349,27 +355,24 @@ func (blder *TypedBuilder[request]) doWatch() error {
349355
allPredicates := append([]predicate.Predicate(nil), blder.globalPredicates...)
350356
allPredicates = append(allPredicates, own.predicates...)
351357
src := source.TypedKind(blder.mgr.GetCache(), obj, hdler, allPredicates...)
352-
if err := blder.ctrl.Watch(src); err != nil {
353-
return err
354-
}
358+
sources = append(sources, src)
355359
}
356360

357361
// Do the watch requests
358-
if len(blder.watchesInput) == 0 && blder.forInput.object == nil && len(blder.rawSources) == 0 {
359-
return errors.New("there are no watches configured, controller will never get triggered. Use For(), Owns(), Watches() or WatchesRawSource() to set them up")
360-
}
361362
for _, w := range blder.watchesInput {
362363
projected, err := blder.project(w.obj, w.objectProjection)
363364
if err != nil {
364365
return fmt.Errorf("failed to project for %T: %w", w.obj, err)
365366
}
366367
allPredicates := append([]predicate.Predicate(nil), blder.globalPredicates...)
367368
allPredicates = append(allPredicates, w.predicates...)
368-
if err := blder.ctrl.Watch(source.TypedKind(blder.mgr.GetCache(), projected, w.handler, allPredicates...)); err != nil {
369-
return err
370-
}
369+
src := source.TypedKind(blder.mgr.GetCache(), projected, w.handler, allPredicates...)
370+
sources = append(sources, src)
371371
}
372-
for _, src := range blder.rawSources {
372+
373+
sources = append(sources, blder.rawSources...)
374+
375+
for _, src := range sources {
373376
if err := blder.ctrl.Watch(src); err != nil {
374377
return err
375378
}

0 commit comments

Comments
 (0)