Skip to content

Commit 8102998

Browse files
committed
🐛 surface controller options when using builder
Fixes: #317 - Currently there is no way to override MaxConcurrentReconciles using builder - Provide a method to override controller options - Another approach was to provide MaxConcurrentReconciles itself, but it isnt future proof incase additional options are added
1 parent 59b131b commit 8102998

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pkg/builder/controller.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Builder struct {
4747
watchRequest []watchRequest
4848
config *rest.Config
4949
ctrl controller.Controller
50+
ctrlOptions controller.Options
5051
name string
5152
}
5253

@@ -125,6 +126,12 @@ func (blder *Builder) WithEventFilter(p predicate.Predicate) *Builder {
125126
return blder
126127
}
127128

129+
// WithOptions overrides the controller options use in doController. Defaults to empty.
130+
func (blder *Builder) WithOptions(options controller.Options) *Builder {
131+
blder.ctrlOptions = options
132+
return blder
133+
}
134+
128135
// Named sets the name of the controller to the given name. The name shows up
129136
// in metrics, among other things, and thus should be a prometheus compatible name
130137
// (underscores and alphanumeric characters only).
@@ -241,6 +248,8 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
241248
if err != nil {
242249
return err
243250
}
244-
blder.ctrl, err = newController(name, blder.mgr, controller.Options{Reconciler: r})
251+
ctrlOptions := blder.ctrlOptions
252+
ctrlOptions.Reconciler = r
253+
blder.ctrl, err = newController(name, blder.mgr, ctrlOptions)
245254
return err
246255
}

pkg/builder/controller_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ var _ = Describe("application", func() {
121121
Expect(instance).To(BeNil())
122122
})
123123

124+
It("should override max concurrent reconcilers during creation of controller", func() {
125+
const maxConcurrentReconciles = 5
126+
newController = func(name string, mgr manager.Manager, options controller.Options) (
127+
controller.Controller, error) {
128+
if options.MaxConcurrentReconciles == maxConcurrentReconciles {
129+
return controller.New(name, mgr, options)
130+
}
131+
return nil, fmt.Errorf("max concurrent reconcilers expected %d but found %d", maxConcurrentReconciles, options.MaxConcurrentReconciles)
132+
}
133+
instance, err := SimpleController().
134+
For(&appsv1.ReplicaSet{}).
135+
Owns(&appsv1.ReplicaSet{}).
136+
WithOptions(controller.Options{MaxConcurrentReconciles: maxConcurrentReconciles}).
137+
Build(noop)
138+
Expect(err).NotTo(HaveOccurred())
139+
Expect(instance).NotTo(BeNil())
140+
})
141+
124142
It("should allow multiple controllers for the same kind", func() {
125143
By("creating a controller manager")
126144
m, err := manager.New(cfg, manager.Options{})

0 commit comments

Comments
 (0)