Skip to content

Commit 335a587

Browse files
authored
Merge pull request #520 from awesomenix/master
🐛 surface controller options when using builder
2 parents dc83571 + 8102998 commit 335a587

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
@@ -44,6 +44,7 @@ type Builder struct {
4444
watchRequest []watchRequest
4545
config *rest.Config
4646
ctrl controller.Controller
47+
ctrlOptions controller.Options
4748
name string
4849
}
4950

@@ -107,6 +108,12 @@ func (blder *Builder) WithEventFilter(p predicate.Predicate) *Builder {
107108
return blder
108109
}
109110

111+
// WithOptions overrides the controller options use in doController. Defaults to empty.
112+
func (blder *Builder) WithOptions(options controller.Options) *Builder {
113+
blder.ctrlOptions = options
114+
return blder
115+
}
116+
110117
// Named sets the name of the controller to the given name. The name shows up
111118
// in metrics, among other things, and thus should be a prometheus compatible name
112119
// (underscores and alphanumeric characters only).
@@ -201,6 +208,8 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
201208
if err != nil {
202209
return err
203210
}
204-
blder.ctrl, err = newController(name, blder.mgr, controller.Options{Reconciler: r})
211+
ctrlOptions := blder.ctrlOptions
212+
ctrlOptions.Reconciler = r
213+
blder.ctrl, err = newController(name, blder.mgr, ctrlOptions)
205214
return err
206215
}

pkg/builder/controller_test.go

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

110+
It("should override max concurrent reconcilers during creation of controller", func() {
111+
const maxConcurrentReconciles = 5
112+
newController = func(name string, mgr manager.Manager, options controller.Options) (
113+
controller.Controller, error) {
114+
if options.MaxConcurrentReconciles == maxConcurrentReconciles {
115+
return controller.New(name, mgr, options)
116+
}
117+
return nil, fmt.Errorf("max concurrent reconcilers expected %d but found %d", maxConcurrentReconciles, options.MaxConcurrentReconciles)
118+
}
119+
instance, err := SimpleController().
120+
For(&appsv1.ReplicaSet{}).
121+
Owns(&appsv1.ReplicaSet{}).
122+
WithOptions(controller.Options{MaxConcurrentReconciles: maxConcurrentReconciles}).
123+
Build(noop)
124+
Expect(err).NotTo(HaveOccurred())
125+
Expect(instance).NotTo(BeNil())
126+
})
127+
110128
It("should allow multiple controllers for the same kind", func() {
111129
By("creating a controller manager")
112130
m, err := manager.New(cfg, manager.Options{})

0 commit comments

Comments
 (0)