Skip to content

Commit 999e0b7

Browse files
committed
export queue's length of Controller
Signed-off-by: zounengren <[email protected]>
1 parent 4d10a06 commit 999e0b7

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

pkg/controller/controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ type Controller interface {
7979

8080
// GetLogger returns this controller logger prefilled with basic information.
8181
GetLogger() logr.Logger
82+
83+
// Len returns the current queue length, for informational purposes only. You
84+
// shouldn't e.g. gate a call to Add() or Get() on Len() being a particular
85+
// value, that can't be synchronized properly.
86+
Len() int
8287
}
8388

8489
// New returns a new Controller registered with the Manager. The Manager will ensure that shared Caches have

pkg/internal/controller/controller.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@ func (c *Controller) processNextWorkItem(ctx context.Context) bool {
267267
return true
268268
}
269269

270+
// Len returns the current queue length, for informational purposes only. You
271+
// shouldn't e.g. gate a call to Add() or Get() on Len() being a particular
272+
// value, that can't be synchronized properly.
273+
func (c *Controller) Len() int {
274+
c.mu.Lock()
275+
defer c.mu.Unlock()
276+
return c.Queue.Len()
277+
}
278+
270279
const (
271280
labelError = "error"
272281
labelRequeueAfter = "requeue_after"

pkg/internal/controller/controller_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,39 @@ var _ = Describe("controller", func() {
467467
Eventually(func() int { return queue.NumRequeues(request) }).Should(Equal(0))
468468
})
469469

470+
It("should requeue a Request if there is an error and continue processing items using Len()", func() {
471+
ctx, cancel := context.WithCancel(context.Background())
472+
req := reconcile.Request{
473+
NamespacedName: types.NamespacedName{Namespace: "bar", Name: "foo"},
474+
}
475+
queue = &controllertest.Queue{
476+
Interface: workqueue.New(),
477+
}
478+
ctrl.Queue = queue
479+
defer cancel()
480+
go func() {
481+
defer GinkgoRecover()
482+
Expect(ctrl.Start(ctx)).NotTo(HaveOccurred())
483+
}()
484+
485+
queue.Add(request)
486+
queue.Add(req)
487+
Expect(ctrl.Len()).To(Equal(2))
488+
By("Invoking Reconciler which will give an error")
489+
490+
fakeReconcile.AddResult(reconcile.Result{}, fmt.Errorf("expected error: reconcile"))
491+
Expect(<-reconciled).To(Equal(req))
492+
493+
By("Invoking Reconciler a second time without error")
494+
fakeReconcile.AddResult(reconcile.Result{}, nil)
495+
Expect(ctrl.Len()).To(Equal(1))
496+
Expect(<-reconciled).To(Equal(req))
497+
498+
By("Removing the item from the queue")
499+
Expect(ctrl.Len()).To(Equal(0))
500+
Eventually(func() int { return queue.NumRequeues(request) }).Should(Equal(0))
501+
}, 1.0)
502+
470503
PIt("should forget an item if it is not a Request and continue processing items", func() {
471504
// TODO(community): write this test
472505
})

0 commit comments

Comments
 (0)