-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcontroller_test.go
478 lines (372 loc) · 16.1 KB
/
controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller_test
import (
"context"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/goleak"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/priorityqueue"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
internalcontroller "sigs.k8s.io/controller-runtime/pkg/internal/controller"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var _ = Describe("controller.Controller", func() {
rec := reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
return reconcile.Result{}, nil
})
Describe("New", func() {
It("should return an error if Name is not Specified", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("", m, controller.Options{Reconciler: rec})
Expect(c).To(BeNil())
Expect(err.Error()).To(ContainSubstring("must specify Name for Controller"))
})
It("should return an error if Reconciler is not Specified", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("foo", m, controller.Options{})
Expect(c).To(BeNil())
Expect(err.Error()).To(ContainSubstring("must specify Reconciler"))
})
It("should return an error if two controllers are registered with the same name", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c1, err := controller.New("c3", m, controller.Options{Reconciler: rec})
Expect(err).NotTo(HaveOccurred())
Expect(c1).ToNot(BeNil())
c2, err := controller.New("c3", m, controller.Options{Reconciler: rec})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("controller with name c3 already exists"))
Expect(c2).To(BeNil())
})
It("should return an error if two controllers are registered with the same name and SkipNameValidation is set to false on the manager", func() {
m, err := manager.New(cfg, manager.Options{
Controller: config.Controller{
SkipNameValidation: ptr.To(false),
},
})
Expect(err).NotTo(HaveOccurred())
c1, err := controller.New("c4", m, controller.Options{Reconciler: rec})
Expect(err).NotTo(HaveOccurred())
Expect(c1).ToNot(BeNil())
c2, err := controller.New("c4", m, controller.Options{Reconciler: rec})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("controller with name c4 already exists"))
Expect(c2).To(BeNil())
})
It("should not return an error if two controllers are registered with the same name and SkipNameValidation is set on the manager", func() {
m, err := manager.New(cfg, manager.Options{
Controller: config.Controller{
SkipNameValidation: ptr.To(true),
},
})
Expect(err).NotTo(HaveOccurred())
c1, err := controller.New("c5", m, controller.Options{Reconciler: rec})
Expect(err).NotTo(HaveOccurred())
Expect(c1).ToNot(BeNil())
c2, err := controller.New("c5", m, controller.Options{Reconciler: rec})
Expect(err).NotTo(HaveOccurred())
Expect(c2).ToNot(BeNil())
})
It("should not return an error if two controllers are registered with the same name and SkipNameValidation is set on the controller", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c1, err := controller.New("c6", m, controller.Options{Reconciler: rec})
Expect(err).NotTo(HaveOccurred())
Expect(c1).ToNot(BeNil())
c2, err := controller.New("c6", m, controller.Options{Reconciler: rec, SkipNameValidation: ptr.To(true)})
Expect(err).NotTo(HaveOccurred())
Expect(c2).ToNot(BeNil())
})
It("should not return an error if two controllers are registered with different names", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c1, err := controller.New("c1", m, controller.Options{Reconciler: rec})
Expect(err).NotTo(HaveOccurred())
Expect(c1).ToNot(BeNil())
c2, err := controller.New("c2", m, controller.Options{Reconciler: rec})
Expect(err).NotTo(HaveOccurred())
Expect(c2).ToNot(BeNil())
})
It("should not leak goroutines when stopped", func() {
currentGRs := goleak.IgnoreCurrent()
ctx, cancel := context.WithCancel(context.Background())
watchChan := make(chan event.GenericEvent, 1)
watch := source.Channel(watchChan, &handler.EnqueueRequestForObject{})
watchChan <- event.GenericEvent{Object: &corev1.Pod{}}
reconcileStarted := make(chan struct{})
controllerFinished := make(chan struct{})
rec := reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
defer GinkgoRecover()
close(reconcileStarted)
// Make sure reconciliation takes a moment and is not quicker than the controllers
// shutdown.
time.Sleep(50 * time.Millisecond)
// Explicitly test this on top of the leakdetection, as the latter uses Eventually
// so might succeed even when the controller does not wait for all reconciliations
// to finish.
Expect(controllerFinished).NotTo(BeClosed())
return reconcile.Result{}, nil
})
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller-0", m, controller.Options{Reconciler: rec})
Expect(c.Watch(watch)).To(Succeed())
Expect(err).NotTo(HaveOccurred())
go func() {
defer GinkgoRecover()
Expect(m.Start(ctx)).To(Succeed())
close(controllerFinished)
}()
<-reconcileStarted
cancel()
<-controllerFinished
// force-close keep-alive connections. These'll time anyway (after
// like 30s or so) but force it to speed up the tests.
clientTransport.CloseIdleConnections()
Eventually(func() error { return goleak.Find(currentGRs) }).Should(Succeed())
})
It("should not create goroutines if never started", func() {
currentGRs := goleak.IgnoreCurrent()
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
_, err = controller.New("new-controller-1", m, controller.Options{Reconciler: rec})
Expect(err).NotTo(HaveOccurred())
// force-close keep-alive connections. These'll time anyway (after
// like 30s or so) but force it to speed up the tests.
clientTransport.CloseIdleConnections()
Eventually(func() error { return goleak.Find(currentGRs) }).Should(Succeed())
})
It("should default RateLimiter and NewQueue if not specified", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller-2", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
Expect(ok).To(BeTrue())
Expect(ctrl.RateLimiter).NotTo(BeNil())
Expect(ctrl.NewQueue).NotTo(BeNil())
})
It("should not override RateLimiter and NewQueue if specified", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
customRateLimiter := workqueue.NewTypedItemExponentialFailureRateLimiter[reconcile.Request](5*time.Millisecond, 1000*time.Second)
customNewQueueCalled := false
customNewQueue := func(controllerName string, rateLimiter workqueue.TypedRateLimiter[reconcile.Request]) workqueue.TypedRateLimitingInterface[reconcile.Request] {
customNewQueueCalled = true
return nil
}
c, err := controller.New("new-controller-3", m, controller.Options{
Reconciler: reconcile.Func(nil),
RateLimiter: customRateLimiter,
NewQueue: customNewQueue,
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
Expect(ok).To(BeTrue())
Expect(ctrl.RateLimiter).To(BeIdenticalTo(customRateLimiter))
ctrl.NewQueue("controller1", nil)
Expect(customNewQueueCalled).To(BeTrue(), "Expected customNewQueue to be called")
})
It("should default RecoverPanic from the manager", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{RecoverPanic: ptr.To(true)}})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller-4", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
Expect(ok).To(BeTrue())
Expect(ctrl.RecoverPanic).NotTo(BeNil())
Expect(*ctrl.RecoverPanic).To(BeTrue())
})
It("should not override RecoverPanic on the controller", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{RecoverPanic: ptr.To(true)}})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller", m, controller.Options{
RecoverPanic: ptr.To(false),
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
Expect(ok).To(BeTrue())
Expect(ctrl.RecoverPanic).NotTo(BeNil())
Expect(*ctrl.RecoverPanic).To(BeFalse())
})
It("should default NeedLeaderElection from the manager", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{NeedLeaderElection: ptr.To(true)}})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller-5", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
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: ptr.To(true)}})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller-6", m, controller.Options{
NeedLeaderElection: ptr.To(false),
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
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-7", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
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-8", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
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-9", m, controller.Options{
Reconciler: reconcile.Func(nil),
MaxConcurrentReconciles: 5,
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
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-10", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
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-11", m, controller.Options{
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
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-12", m, controller.Options{
Reconciler: reconcile.Func(nil),
CacheSyncTimeout: 5,
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
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())
c, err := controller.New("new-controller-13", m, controller.Options{
Reconciler: rec,
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
Expect(ok).To(BeTrue())
Expect(ctrl.NeedLeaderElection()).To(BeTrue())
})
It("should allow for setting leaderElected to false", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller-14", m, controller.Options{
NeedLeaderElection: ptr.To(false),
Reconciler: rec,
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
Expect(ok).To(BeTrue())
Expect(ctrl.NeedLeaderElection()).To(BeFalse())
})
It("should implement manager.LeaderElectionRunnable", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller-15", m, controller.Options{
Reconciler: rec,
})
Expect(err).NotTo(HaveOccurred())
_, ok := c.(manager.LeaderElectionRunnable)
Expect(ok).To(BeTrue())
})
It("should configure a priority queue if UsePriorityQueue is set", func() {
m, err := manager.New(cfg, manager.Options{
Controller: config.Controller{UsePriorityQueue: ptr.To(true)},
})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller-16", m, controller.Options{
Reconciler: rec,
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
Expect(ok).To(BeTrue())
q := ctrl.NewQueue("foo", nil)
_, ok = q.(priorityqueue.PriorityQueue[reconcile.Request])
Expect(ok).To(BeTrue())
})
It("should not configure a priority queue if UsePriorityQueue is not set", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
c, err := controller.New("new-controller-17", m, controller.Options{
Reconciler: rec,
})
Expect(err).NotTo(HaveOccurred())
ctrl, ok := c.(*internalcontroller.Controller[reconcile.Request])
Expect(ok).To(BeTrue())
q := ctrl.NewQueue("foo", nil)
_, ok = q.(priorityqueue.PriorityQueue[reconcile.Request])
Expect(ok).To(BeFalse())
})
})
})