Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Flatten fields in controller.Options #2307

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/builder/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ var _ = Describe("application", func() {
instance, err := ControllerManagedBy(m).
For(&appsv1.ReplicaSet{}).
Owns(&appsv1.ReplicaSet{}).
WithOptions(controller.Options{Controller: config.Controller{MaxConcurrentReconciles: maxConcurrentReconciles}}).
WithOptions(controller.Options{MaxConcurrentReconciles: maxConcurrentReconciles}).
Build(noop)
Expect(err).NotTo(HaveOccurred())
Expect(instance).NotTo(BeNil())
Expand Down
32 changes: 28 additions & 4 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"

"sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/internal/controller"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand All @@ -37,7 +36,20 @@ import (

// Options are the arguments for creating a new Controller.
type Options struct {
config.Controller
// MaxConcurrentReconciles is the maximum number of concurrent Reconciles which can be run. Defaults to 1.
MaxConcurrentReconciles int
vincepri marked this conversation as resolved.
Show resolved Hide resolved

// CacheSyncTimeout refers to the time limit set to wait for syncing caches.
// Defaults to 2 minutes if not set.
CacheSyncTimeout time.Duration

// RecoverPanic indicates whether the panic caused by reconcile should be recovered.
// Defaults to the Controller.RecoverPanic setting from the Manager if unset.
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
RecoverPanic *bool

// NeedLeaderElection indicates whether the controller needs to use leader election.
// Defaults to true, which means the controller will use leader election.
NeedLeaderElection *bool

// Reconciler reconciles an object
Reconciler reconcile.Reconciler
Expand Down Expand Up @@ -116,11 +128,19 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
}

if options.MaxConcurrentReconciles <= 0 {
options.MaxConcurrentReconciles = 1
if mgr.GetControllerOptions().MaxConcurrentReconciles > 0 {
options.MaxConcurrentReconciles = mgr.GetControllerOptions().MaxConcurrentReconciles
} else {
options.MaxConcurrentReconciles = 1
}
}

if options.CacheSyncTimeout == 0 {
options.CacheSyncTimeout = 2 * time.Minute
if mgr.GetControllerOptions().CacheSyncTimeout != 0 {
options.CacheSyncTimeout = mgr.GetControllerOptions().CacheSyncTimeout
} else {
options.CacheSyncTimeout = 2 * time.Minute
}
}

if options.RateLimiter == nil {
Expand All @@ -131,6 +151,10 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
options.RecoverPanic = mgr.GetControllerOptions().RecoverPanic
}

if options.NeedLeaderElection == nil {
options.NeedLeaderElection = mgr.GetControllerOptions().NeedLeaderElection
}

// Create controller with dependencies set
return &controller.Controller{
Do: options.Reconciler,
Expand Down
135 changes: 127 additions & 8 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,8 @@ var _ = Describe("controller.Controller", func() {
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Controller: config.Controller{
RecoverPanic: pointer.Bool(false),
},
Reconciler: reconcile.Func(nil),
RecoverPanic: pointer.Bool(false),
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())

Expand All @@ -168,6 +166,129 @@ var _ = Describe("controller.Controller", func() {
Expect(*ctrl.RecoverPanic).To(BeFalse())
})

It("should default NeedLeaderElection from the manager", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{NeedLeaderElection: pointer.Bool(true)}})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.NeedLeaderElection()).To(BeTrue())
})

It("should not override NeedLeaderElection on the controller", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{NeedLeaderElection: pointer.Bool(true)}})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
NeedLeaderElection: pointer.Bool(false),
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.NeedLeaderElection()).To(BeFalse())
})

It("Should default MaxConcurrentReconciles from the manager if set", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{MaxConcurrentReconciles: 5}})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.MaxConcurrentReconciles).To(BeEquivalentTo(5))
})

It("Should default MaxConcurrentReconciles to 1 if unset", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.MaxConcurrentReconciles).To(BeEquivalentTo(1))
})

It("Should leave MaxConcurrentReconciles if set", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: reconcile.Func(nil),
MaxConcurrentReconciles: 5,
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.MaxConcurrentReconciles).To(BeEquivalentTo(5))
})

It("Should default CacheSyncTimeout from the manager if set", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{CacheSyncTimeout: 5}})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.CacheSyncTimeout).To(BeEquivalentTo(5))
})

It("Should default CacheSyncTimeout to 2 minutes if unset", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.CacheSyncTimeout).To(BeEquivalentTo(2 * time.Minute))
})

It("Should leave CacheSyncTimeout if set", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: reconcile.Func(nil),
CacheSyncTimeout: 5,
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.CacheSyncTimeout).To(BeEquivalentTo(5))
})

It("should default NeedLeaderElection on the controller to true", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -188,10 +309,8 @@ var _ = Describe("controller.Controller", func() {
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Controller: config.Controller{
NeedLeaderElection: pointer.Bool(false),
},
Reconciler: rec,
NeedLeaderElection: pointer.Bool(false),
Reconciler: rec,
})
Expect(err).NotTo(HaveOccurred())

Expand Down