Skip to content

Commit 74bcf1e

Browse files
committed
:warn: Flatten fields in controller.Options
Currently, `controller.Options` embedds `config.Controller`. This makes its usage pretty annoying, to set `MaxConcurrentReconciles` for example, one has to do it like this: ``` controller.Options{config.Options{MaxConcurrentReconciles: 8}} ``` This makes it harder to find what options exist and causes a lot of churn for downstream consumers. Re-Define the fields from `config.Controller` in `controller.Options` instead to avoid that. This also fixes some defaulting bugs where we wouldn't default `MaxConcurrentReconciles` and `CacheSyncTimeout` from the `config.Controller` setting in the manager.
1 parent eb0ebc9 commit 74bcf1e

File tree

3 files changed

+121
-13
lines changed

3 files changed

+121
-13
lines changed

pkg/builder/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ var _ = Describe("application", func() {
217217
instance, err := ControllerManagedBy(m).
218218
For(&appsv1.ReplicaSet{}).
219219
Owns(&appsv1.ReplicaSet{}).
220-
WithOptions(controller.Options{Controller: config.Controller{MaxConcurrentReconciles: maxConcurrentReconciles}}).
220+
WithOptions(controller.Options{MaxConcurrentReconciles: maxConcurrentReconciles}).
221221
Build(noop)
222222
Expect(err).NotTo(HaveOccurred())
223223
Expect(instance).NotTo(BeNil())

pkg/controller/controller.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"k8s.io/client-go/util/workqueue"
2626
"k8s.io/klog/v2"
2727

28-
"sigs.k8s.io/controller-runtime/pkg/config"
2928
"sigs.k8s.io/controller-runtime/pkg/handler"
3029
"sigs.k8s.io/controller-runtime/pkg/internal/controller"
3130
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -37,7 +36,20 @@ import (
3736

3837
// Options are the arguments for creating a new Controller.
3938
type Options struct {
40-
config.Controller
39+
// MaxConcurrentReconciles is the maximum number of concurrent Reconciles which can be run. Defaults to 1.
40+
MaxConcurrentReconciles int
41+
42+
// CacheSyncTimeout refers to the time limit set to wait for syncing caches.
43+
// Defaults to 2 minutes if not set.
44+
CacheSyncTimeout time.Duration
45+
46+
// RecoverPanic indicates whether the panic caused by reconcile should be recovered.
47+
// Defaults to the Controller.RecoverPanic setting from the Manager if unset.
48+
RecoverPanic *bool
49+
50+
// NeedLeaderElection indicates whether the controller needs to use leader election.
51+
// Defaults to true, which means the controller will use leader election.
52+
NeedLeaderElection *bool
4153

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

118130
if options.MaxConcurrentReconciles <= 0 {
119-
options.MaxConcurrentReconciles = 1
131+
if mgr.GetControllerOptions().MaxConcurrentReconciles > 0 {
132+
options.MaxConcurrentReconciles = mgr.GetControllerOptions().MaxConcurrentReconciles
133+
} else {
134+
options.MaxConcurrentReconciles = 1
135+
}
120136
}
121137

122138
if options.CacheSyncTimeout == 0 {
123-
options.CacheSyncTimeout = 2 * time.Minute
139+
if mgr.GetControllerOptions().CacheSyncTimeout != 0 {
140+
options.CacheSyncTimeout = mgr.GetControllerOptions().CacheSyncTimeout
141+
} else {
142+
options.CacheSyncTimeout = 2 * time.Minute
143+
}
124144
}
125145

126146
if options.RateLimiter == nil {

pkg/controller/controller_test.go

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,8 @@ var _ = Describe("controller.Controller", func() {
154154
Expect(err).NotTo(HaveOccurred())
155155

156156
c, err := controller.New("new-controller", m, controller.Options{
157-
Controller: config.Controller{
158-
RecoverPanic: pointer.Bool(false),
159-
},
160-
Reconciler: reconcile.Func(nil),
157+
RecoverPanic: pointer.Bool(false),
158+
Reconciler: reconcile.Func(nil),
161159
})
162160
Expect(err).NotTo(HaveOccurred())
163161

@@ -168,6 +166,98 @@ var _ = Describe("controller.Controller", func() {
168166
Expect(*ctrl.RecoverPanic).To(BeFalse())
169167
})
170168

169+
It("Should default MaxConcurrentReconciles from the manager if set", func() {
170+
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{MaxConcurrentReconciles: 5}})
171+
Expect(err).NotTo(HaveOccurred())
172+
173+
c, err := controller.New("new-controller", m, controller.Options{
174+
Reconciler: reconcile.Func(nil),
175+
})
176+
Expect(err).NotTo(HaveOccurred())
177+
178+
ctrl, ok := c.(*internalcontroller.Controller)
179+
Expect(ok).To(BeTrue())
180+
181+
Expect(ctrl.MaxConcurrentReconciles).To(BeEquivalentTo(5))
182+
})
183+
184+
It("Should default MaxConcurrentReconciles to 1 if unset", func() {
185+
m, err := manager.New(cfg, manager.Options{})
186+
Expect(err).NotTo(HaveOccurred())
187+
188+
c, err := controller.New("new-controller", m, controller.Options{
189+
Reconciler: reconcile.Func(nil),
190+
})
191+
Expect(err).NotTo(HaveOccurred())
192+
193+
ctrl, ok := c.(*internalcontroller.Controller)
194+
Expect(ok).To(BeTrue())
195+
196+
Expect(ctrl.MaxConcurrentReconciles).To(BeEquivalentTo(1))
197+
})
198+
199+
It("Should leave MaxConcurrentReconciles if set", func() {
200+
m, err := manager.New(cfg, manager.Options{})
201+
Expect(err).NotTo(HaveOccurred())
202+
203+
c, err := controller.New("new-controller", m, controller.Options{
204+
Reconciler: reconcile.Func(nil),
205+
MaxConcurrentReconciles: 5,
206+
})
207+
Expect(err).NotTo(HaveOccurred())
208+
209+
ctrl, ok := c.(*internalcontroller.Controller)
210+
Expect(ok).To(BeTrue())
211+
212+
Expect(ctrl.MaxConcurrentReconciles).To(BeEquivalentTo(5))
213+
})
214+
215+
It("Should default CacheSyncTimeout from the manager if set", func() {
216+
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{CacheSyncTimeout: 5}})
217+
Expect(err).NotTo(HaveOccurred())
218+
219+
c, err := controller.New("new-controller", m, controller.Options{
220+
Reconciler: reconcile.Func(nil),
221+
})
222+
Expect(err).NotTo(HaveOccurred())
223+
224+
ctrl, ok := c.(*internalcontroller.Controller)
225+
Expect(ok).To(BeTrue())
226+
227+
Expect(ctrl.CacheSyncTimeout).To(BeEquivalentTo(5))
228+
})
229+
230+
It("Should default CacheSyncTimeout to 2 minutes if unset", func() {
231+
m, err := manager.New(cfg, manager.Options{})
232+
Expect(err).NotTo(HaveOccurred())
233+
234+
c, err := controller.New("new-controller", m, controller.Options{
235+
Reconciler: reconcile.Func(nil),
236+
})
237+
Expect(err).NotTo(HaveOccurred())
238+
239+
ctrl, ok := c.(*internalcontroller.Controller)
240+
Expect(ok).To(BeTrue())
241+
242+
Expect(ctrl.CacheSyncTimeout).To(BeEquivalentTo(2 * time.Minute))
243+
})
244+
245+
It("Should leave CacheSyncTimeout if set", func() {
246+
m, err := manager.New(cfg, manager.Options{})
247+
Expect(err).NotTo(HaveOccurred())
248+
249+
c, err := controller.New("new-controller", m, controller.Options{
250+
Reconciler: reconcile.Func(nil),
251+
CacheSyncTimeout: 5,
252+
})
253+
Expect(err).NotTo(HaveOccurred())
254+
255+
ctrl, ok := c.(*internalcontroller.Controller)
256+
Expect(ok).To(BeTrue())
257+
258+
Expect(ctrl.CacheSyncTimeout).To(BeEquivalentTo(5))
259+
})
260+
171261
It("should default NeedLeaderElection on the controller to true", func() {
172262
m, err := manager.New(cfg, manager.Options{})
173263
Expect(err).NotTo(HaveOccurred())
@@ -188,10 +278,8 @@ var _ = Describe("controller.Controller", func() {
188278
Expect(err).NotTo(HaveOccurred())
189279

190280
c, err := controller.New("new-controller", m, controller.Options{
191-
Controller: config.Controller{
192-
NeedLeaderElection: pointer.Bool(false),
193-
},
194-
Reconciler: rec,
281+
RecoverPanic: pointer.Bool(false),
282+
Reconciler: rec,
195283
})
196284
Expect(err).NotTo(HaveOccurred())
197285

0 commit comments

Comments
 (0)