Skip to content

Commit e1ced80

Browse files
authored
Merge pull request #1022 from Danil-Grigorev/fix-reconciler-options
🐛 Prefer reconciler from controller.Options
2 parents 21a81bc + 201ac3b commit e1ced80

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pkg/builder/controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
245245
return err
246246
}
247247
ctrlOptions := blder.ctrlOptions
248-
ctrlOptions.Reconciler = r
248+
if ctrlOptions.Reconciler == nil {
249+
ctrlOptions.Reconciler = r
250+
}
249251
blder.ctrl, err = newController(name, blder.mgr, ctrlOptions)
250252
return err
251253
}

pkg/builder/controller_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ import (
4242
"sigs.k8s.io/controller-runtime/pkg/source"
4343
)
4444

45+
type typedNoop struct{}
46+
47+
func (typedNoop) Reconcile(reconcile.Request) (reconcile.Result, error) {
48+
return reconcile.Result{}, nil
49+
}
50+
4551
var _ = Describe("application", func() {
4652
var stop chan struct{}
4753

@@ -152,6 +158,27 @@ var _ = Describe("application", func() {
152158
Expect(instance).NotTo(BeNil())
153159
})
154160

161+
It("should prefer reconciler from options during creation of controller", func() {
162+
newController = func(name string, mgr manager.Manager, options controller.Options) (controller.Controller, error) {
163+
if options.Reconciler != (typedNoop{}) {
164+
return nil, fmt.Errorf("Custom reconciler expected %T but found %T", typedNoop{}, options.Reconciler)
165+
}
166+
return controller.New(name, mgr, options)
167+
}
168+
169+
By("creating a controller manager")
170+
m, err := manager.New(cfg, manager.Options{})
171+
Expect(err).NotTo(HaveOccurred())
172+
173+
instance, err := ControllerManagedBy(m).
174+
For(&appsv1.ReplicaSet{}).
175+
Owns(&appsv1.ReplicaSet{}).
176+
WithOptions(controller.Options{Reconciler: typedNoop{}}).
177+
Build(noop)
178+
Expect(err).NotTo(HaveOccurred())
179+
Expect(instance).NotTo(BeNil())
180+
})
181+
155182
It("should allow multiple controllers for the same kind", func() {
156183
By("creating a controller manager")
157184
m, err := manager.New(cfg, manager.Options{})

0 commit comments

Comments
 (0)