@@ -23,6 +23,8 @@ import (
23
23
24
24
. "github.com/onsi/ginkgo"
25
25
. "github.com/onsi/gomega"
26
+ "github.com/prometheus/client_golang/prometheus"
27
+ dto "github.com/prometheus/client_model/go"
26
28
"k8s.io/api/apps/v1"
27
29
corev1 "k8s.io/api/core/v1"
28
30
"k8s.io/apimachinery/pkg/types"
@@ -31,6 +33,7 @@ import (
31
33
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
32
34
"sigs.k8s.io/controller-runtime/pkg/controller/controllertest"
33
35
"sigs.k8s.io/controller-runtime/pkg/handler"
36
+ ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
34
37
"sigs.k8s.io/controller-runtime/pkg/predicate"
35
38
"sigs.k8s.io/controller-runtime/pkg/reconcile"
36
39
"sigs.k8s.io/controller-runtime/pkg/reconcile/reconciletest"
@@ -404,6 +407,46 @@ var _ = Describe("controller", func() {
404
407
It ("should create a new go routine for MaxConcurrentReconciles" , func () {
405
408
// TODO(community): write this test
406
409
})
410
+
411
+ Context ("should update prometheus metrics" , func () {
412
+ It ("should requeue a Request if there is an error and continue processing items" , func (done Done ) {
413
+ ctrlmetrics .QueueLength = prometheus .NewGaugeVec (prometheus.GaugeOpts {
414
+ Name : "controller_runtime_reconcile_queue_length" ,
415
+ Help : "Length of reconcile queue per controller" ,
416
+ }, []string {"controller" })
417
+
418
+ fakeReconcile .Err = fmt .Errorf ("expected error: reconcile" )
419
+ go func () {
420
+ defer GinkgoRecover ()
421
+ Expect (ctrl .Start (stop )).NotTo (HaveOccurred ())
422
+ }()
423
+ ctrl .Queue .Add (request )
424
+
425
+ // Reduce the jitterperiod so we don't have to wait a second before the reconcile function is rerun.
426
+ ctrl .JitterPeriod = time .Millisecond
427
+
428
+ By ("Invoking Reconciler which will give an error" )
429
+ Expect (<- reconciled ).To (Equal (request ))
430
+ var queueLength dto.Metric
431
+ Eventually (func () error {
432
+ ctrlmetrics .QueueLength .WithLabelValues (ctrl .Name ).Write (& queueLength )
433
+ if queueLength .GetGauge ().GetValue () != 1.0 {
434
+ return fmt .Errorf ("metrics not updated" )
435
+ }
436
+ return nil
437
+ }, 2.0 ).Should (Succeed ())
438
+
439
+ By ("Invoking Reconciler a second time without error" )
440
+ fakeReconcile .Err = nil
441
+ Expect (<- reconciled ).To (Equal (request ))
442
+
443
+ By ("Removing the item from the queue" )
444
+ Eventually (ctrl .Queue .Len ).Should (Equal (0 ))
445
+ Eventually (func () int { return ctrl .Queue .NumRequeues (request ) }).Should (Equal (0 ))
446
+
447
+ close (done )
448
+ }, 2.0 )
449
+ })
407
450
})
408
451
})
409
452
0 commit comments