@@ -8,11 +8,13 @@ import (
8
8
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
9
"k8s.io/apimachinery/pkg/runtime"
10
10
"k8s.io/apimachinery/pkg/types"
11
+ "k8s.io/apimachinery/pkg/util/cache"
11
12
"k8s.io/apimachinery/pkg/util/sets"
12
13
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
13
14
"k8s.io/client-go/util/workqueue"
14
15
elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
15
16
"sigs.k8s.io/aws-load-balancer-controller/pkg/annotations"
17
+ "sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
16
18
testclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
17
19
"sigs.k8s.io/controller-runtime/pkg/log"
18
20
"strconv"
@@ -31,15 +33,14 @@ func TestDeferredReconcilerConstructor(t *testing.T) {
31
33
32
34
deferredReconciler := d .(* deferredTargetGroupBindingReconcilerImpl )
33
35
assert .Equal (t , dq , deferredReconciler .delayQueue )
34
- assert .Equal (t , syncPeriod , deferredReconciler .syncPeriod )
35
36
assert .Equal (t , k8sClient , deferredReconciler .k8sClient )
36
37
assert .Equal (t , logger , deferredReconciler .logger )
37
38
}
38
39
39
40
func TestDeferredReconcilerEnqueue (t * testing.T ) {
40
- syncPeriod := 5 * time .Minute
41
41
testCases := []struct {
42
42
name string
43
+ tgbInCache []* elbv2api.TargetGroupBinding
43
44
tgbToEnqueue []* elbv2api.TargetGroupBinding
44
45
expectedQueueEntries sets.Set [types.NamespacedName ]
45
46
}{
@@ -59,8 +60,8 @@ func TestDeferredReconcilerEnqueue(t *testing.T) {
59
60
}),
60
61
},
61
62
{
62
- name : "sync period too short, do not enqueue" ,
63
- tgbToEnqueue : []* elbv2api.TargetGroupBinding {
63
+ name : "item in cache, no enqueue" ,
64
+ tgbInCache : []* elbv2api.TargetGroupBinding {
64
65
{
65
66
ObjectMeta : metav1.ObjectMeta {
66
67
Name : "tgb1" ,
@@ -71,25 +72,18 @@ func TestDeferredReconcilerEnqueue(t *testing.T) {
71
72
},
72
73
},
73
74
},
74
- expectedQueueEntries : make (sets.Set [types.NamespacedName ]),
75
- },
76
- {
77
- name : "sync period too long, do enqueue" ,
78
75
tgbToEnqueue : []* elbv2api.TargetGroupBinding {
79
76
{
80
77
ObjectMeta : metav1.ObjectMeta {
81
78
Name : "tgb1" ,
82
79
Namespace : "ns" ,
83
80
Annotations : map [string ]string {
84
- annotations .AnnotationCheckPointTimestamp : strconv .FormatInt (time .Now ().Add ( - 2 * syncPeriod ). Unix (), 10 ),
81
+ annotations .AnnotationCheckPointTimestamp : strconv .FormatInt (time .Now ().Unix (), 10 ),
85
82
},
86
83
},
87
84
},
88
85
},
89
- expectedQueueEntries : sets .New (types.NamespacedName {
90
- Name : "tgb1" ,
91
- Namespace : "ns" ,
92
- }),
86
+ expectedQueueEntries : make (sets.Set [types.NamespacedName ]),
93
87
},
94
88
{
95
89
name : "multiple tgb" ,
@@ -196,15 +190,20 @@ func TestDeferredReconcilerEnqueue(t *testing.T) {
196
190
Build ()
197
191
198
192
impl := deferredTargetGroupBindingReconcilerImpl {
199
- delayQueue : dq ,
200
- syncPeriod : syncPeriod ,
201
- k8sClient : k8sClient ,
202
- logger : logr .New (& log.NullLogSink {}),
193
+ delayQueue : dq ,
194
+ k8sClient : k8sClient ,
195
+ logger : logr .New (& log.NullLogSink {}),
196
+ processedTGBCache : cache .NewExpiring (),
197
+ processedTGBCacheTTL : 1 * time .Minute ,
203
198
204
199
delayedReconcileTime : 0 * time .Millisecond ,
205
200
maxJitter : 0 * time .Millisecond ,
206
201
}
207
202
203
+ for _ , tgb := range tc .tgbInCache {
204
+ impl .processedTGBCache .Set (k8s .NamespacedName (tgb ), true , time .Minute * 1 )
205
+ }
206
+
208
207
for _ , tgb := range tc .tgbToEnqueue {
209
208
impl .Enqueue (tgb )
210
209
}
@@ -261,10 +260,12 @@ func TestDeferredReconcilerRun(t *testing.T) {
261
260
262
261
impl := deferredTargetGroupBindingReconcilerImpl {
263
262
delayQueue : dq ,
264
- syncPeriod : 5 * time .Minute ,
265
263
k8sClient : k8sClient ,
266
264
logger : logr .New (& log.NullLogSink {}),
267
265
266
+ processedTGBCache : cache .NewExpiring (),
267
+ processedTGBCacheTTL : 1 * time .Minute ,
268
+
268
269
delayedReconcileTime : 0 * time .Millisecond ,
269
270
maxJitter : 0 * time .Millisecond ,
270
271
}
@@ -292,24 +293,6 @@ func TestHandleDeferredItem(t *testing.T) {
292
293
Namespace : "ns" ,
293
294
},
294
295
},
295
- {
296
- name : "not eligible" ,
297
- nsn : types.NamespacedName {
298
- Name : "name" ,
299
- Namespace : "ns" ,
300
- },
301
- storedTGB : & elbv2api.TargetGroupBinding {
302
- ObjectMeta : metav1.ObjectMeta {
303
- Name : "name" ,
304
- Namespace : "ns" ,
305
- Annotations : map [string ]string {
306
- annotations .AnnotationCheckPoint : "foo" ,
307
- annotations .AnnotationCheckPointTimestamp : strconv .FormatInt (time .Now ().Unix (), 10 ),
308
- },
309
- },
310
- },
311
- expectedCheckPoint : aws .String ("foo" ),
312
- },
313
296
{
314
297
name : "eligible" ,
315
298
nsn : types.NamespacedName {
@@ -355,10 +338,12 @@ func TestHandleDeferredItem(t *testing.T) {
355
338
356
339
impl := deferredTargetGroupBindingReconcilerImpl {
357
340
delayQueue : dq ,
358
- syncPeriod : syncPeriod ,
359
341
k8sClient : k8sClient ,
360
342
logger : logr .New (& log.NullLogSink {}),
361
343
344
+ processedTGBCache : cache .NewExpiring (),
345
+ processedTGBCacheTTL : 1 * time .Minute ,
346
+
362
347
delayedReconcileTime : 0 * time .Millisecond ,
363
348
maxJitter : 0 * time .Millisecond ,
364
349
}
0 commit comments