Skip to content

Commit

Permalink
🐛 surface controller options when using builder
Browse files Browse the repository at this point in the history
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
  • Loading branch information
awesomenix committed Jul 25, 2019
1 parent 59b131b commit 8102998
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/builder/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Builder struct {
watchRequest []watchRequest
config *rest.Config
ctrl controller.Controller
ctrlOptions controller.Options
name string
}

Expand Down Expand Up @@ -125,6 +126,12 @@ func (blder *Builder) WithEventFilter(p predicate.Predicate) *Builder {
return blder
}

// WithOptions overrides the controller options use in doController. Defaults to empty.
func (blder *Builder) WithOptions(options controller.Options) *Builder {
blder.ctrlOptions = options
return blder
}

// Named sets the name of the controller to the given name. The name shows up
// in metrics, among other things, and thus should be a prometheus compatible name
// (underscores and alphanumeric characters only).
Expand Down Expand Up @@ -241,6 +248,8 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
if err != nil {
return err
}
blder.ctrl, err = newController(name, blder.mgr, controller.Options{Reconciler: r})
ctrlOptions := blder.ctrlOptions
ctrlOptions.Reconciler = r
blder.ctrl, err = newController(name, blder.mgr, ctrlOptions)
return err
}
18 changes: 18 additions & 0 deletions pkg/builder/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ var _ = Describe("application", func() {
Expect(instance).To(BeNil())
})

It("should override max concurrent reconcilers during creation of controller", func() {
const maxConcurrentReconciles = 5
newController = func(name string, mgr manager.Manager, options controller.Options) (
controller.Controller, error) {
if options.MaxConcurrentReconciles == maxConcurrentReconciles {
return controller.New(name, mgr, options)
}
return nil, fmt.Errorf("max concurrent reconcilers expected %d but found %d", maxConcurrentReconciles, options.MaxConcurrentReconciles)
}
instance, err := SimpleController().
For(&appsv1.ReplicaSet{}).
Owns(&appsv1.ReplicaSet{}).
WithOptions(controller.Options{MaxConcurrentReconciles: maxConcurrentReconciles}).
Build(noop)
Expect(err).NotTo(HaveOccurred())
Expect(instance).NotTo(BeNil())
})

It("should allow multiple controllers for the same kind", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
Expand Down

0 comments on commit 8102998

Please sign in to comment.