@@ -34,6 +34,7 @@ import (
3434 "sigs.k8s.io/controller-runtime/pkg/client"
3535 "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3636 "sigs.k8s.io/controller-runtime/pkg/controller/controllertest"
37+ "sigs.k8s.io/controller-runtime/pkg/controller/priorityqueue"
3738 "sigs.k8s.io/controller-runtime/pkg/event"
3839 "sigs.k8s.io/controller-runtime/pkg/handler"
3940 "sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -773,4 +774,140 @@ var _ = Describe("Eventhandler", func() {
773774 instance .Generic (ctx , evt , q )
774775 })
775776 })
777+
778+ Describe ("WithLowPriorityWhenUnchanged" , func () {
779+ It ("should lower the priority of a create request for an object that was crated more than one minute in the past" , func () {
780+ actualOpts := priorityqueue.AddOpts {}
781+ var actualRequests []reconcile.Request
782+ wq := & fakePriorityQueue {
783+ addWithOpts : func (o priorityqueue.AddOpts , items ... reconcile.Request ) {
784+ actualOpts = o
785+ actualRequests = items
786+ },
787+ }
788+
789+ h := handler .WithLowPriorityWhenUnchanged (& handler.EnqueueRequestForObject {})
790+ h .Create (ctx , event.CreateEvent {
791+ Object : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
792+ Name : "my-pod" ,
793+ }},
794+ }, wq )
795+
796+ Expect (actualOpts ).To (Equal (priorityqueue.AddOpts {Priority : handler .LowPriority }))
797+ Expect (actualRequests ).To (Equal ([]reconcile.Request {{NamespacedName : types.NamespacedName {Name : "my-pod" }}}))
798+ })
799+
800+ It ("should not lower the priority of a create request for an object that was crated less than one minute in the past" , func () {
801+ actualOpts := priorityqueue.AddOpts {}
802+ var actualRequests []reconcile.Request
803+ wq := & fakePriorityQueue {
804+ addWithOpts : func (o priorityqueue.AddOpts , items ... reconcile.Request ) {
805+ actualOpts = o
806+ actualRequests = items
807+ },
808+ }
809+
810+ h := handler .WithLowPriorityWhenUnchanged (& handler.EnqueueRequestForObject {})
811+ h .Create (ctx , event.CreateEvent {
812+ Object : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
813+ Name : "my-pod" ,
814+ CreationTimestamp : metav1 .Now (),
815+ }},
816+ }, wq )
817+
818+ Expect (actualOpts ).To (Equal (priorityqueue.AddOpts {}))
819+ Expect (actualRequests ).To (Equal ([]reconcile.Request {{NamespacedName : types.NamespacedName {Name : "my-pod" }}}))
820+ })
821+
822+ It ("should lower the priority of an update request with unchanged RV" , func () {
823+ actualOpts := priorityqueue.AddOpts {}
824+ var actualRequests []reconcile.Request
825+ wq := & fakePriorityQueue {
826+ addWithOpts : func (o priorityqueue.AddOpts , items ... reconcile.Request ) {
827+ actualOpts = o
828+ actualRequests = items
829+ },
830+ }
831+
832+ h := handler .WithLowPriorityWhenUnchanged (& handler.EnqueueRequestForObject {})
833+ h .Update (ctx , event.UpdateEvent {
834+ ObjectOld : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
835+ Name : "my-pod" ,
836+ }},
837+ ObjectNew : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
838+ Name : "my-pod" ,
839+ }},
840+ }, wq )
841+
842+ Expect (actualOpts ).To (Equal (priorityqueue.AddOpts {Priority : handler .LowPriority }))
843+ Expect (actualRequests ).To (Equal ([]reconcile.Request {{NamespacedName : types.NamespacedName {Name : "my-pod" }}}))
844+ })
845+
846+ It ("should not lower the priority of an update request with changed RV" , func () {
847+ actualOpts := priorityqueue.AddOpts {}
848+ var actualRequests []reconcile.Request
849+ wq := & fakePriorityQueue {
850+ addWithOpts : func (o priorityqueue.AddOpts , items ... reconcile.Request ) {
851+ actualOpts = o
852+ actualRequests = items
853+ },
854+ }
855+
856+ h := handler .WithLowPriorityWhenUnchanged (& handler.EnqueueRequestForObject {})
857+ h .Update (ctx , event.UpdateEvent {
858+ ObjectOld : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
859+ Name : "my-pod" ,
860+ }},
861+ ObjectNew : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
862+ Name : "my-pod" ,
863+ ResourceVersion : "1" ,
864+ }},
865+ }, wq )
866+
867+ Expect (actualOpts ).To (Equal (priorityqueue.AddOpts {}))
868+ Expect (actualRequests ).To (Equal ([]reconcile.Request {{NamespacedName : types.NamespacedName {Name : "my-pod" }}}))
869+ })
870+
871+ It ("should have no effect on create if the workqueue is not a priorityqueue" , func () {
872+ h := handler .WithLowPriorityWhenUnchanged (& handler.EnqueueRequestForObject {})
873+ h .Create (ctx , event.CreateEvent {
874+ Object : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
875+ Name : "my-pod" ,
876+ }},
877+ }, q )
878+
879+ Expect (q .Len ()).To (Equal (1 ))
880+ item , _ := q .Get ()
881+ Expect (item ).To (Equal (reconcile.Request {NamespacedName : types.NamespacedName {Name : "my-pod" }}))
882+ })
883+
884+ It ("should have no effect on Update if the workqueue is not a priorityqueue" , func () {
885+ h := handler .WithLowPriorityWhenUnchanged (& handler.EnqueueRequestForObject {})
886+ h .Update (ctx , event.UpdateEvent {
887+ ObjectOld : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
888+ Name : "my-pod" ,
889+ }},
890+ ObjectNew : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
891+ Name : "my-pod" ,
892+ }},
893+ }, q )
894+
895+ Expect (q .Len ()).To (Equal (1 ))
896+ item , _ := q .Get ()
897+ Expect (item ).To (Equal (reconcile.Request {NamespacedName : types.NamespacedName {Name : "my-pod" }}))
898+ })
899+ })
900+
776901})
902+
903+ type fakePriorityQueue struct {
904+ workqueue.TypedRateLimitingInterface [reconcile.Request ]
905+ addWithOpts func (o priorityqueue.AddOpts , items ... reconcile.Request )
906+ }
907+
908+ func (f * fakePriorityQueue ) AddWithOpts (o priorityqueue.AddOpts , items ... reconcile.Request ) {
909+ f .addWithOpts (o , items ... )
910+ }
911+ func (f * fakePriorityQueue ) GetWithPriority () (item reconcile.Request , priority int , shutdown bool ) {
912+ panic ("GetWithPriority is not expected to be called" )
913+ }
0 commit comments