@@ -48,7 +48,6 @@ import (
48
48
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
49
49
"k8s.io/apiserver/pkg/features"
50
50
"k8s.io/apiserver/pkg/storage"
51
- "k8s.io/apiserver/pkg/storage/cacher/delegator"
52
51
"k8s.io/apiserver/pkg/storage/cacher/metrics"
53
52
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
54
53
etcdfeature "k8s.io/apiserver/pkg/storage/feature"
@@ -245,52 +244,69 @@ func TestShouldDelegateList(t *testing.T) {
245
244
},
246
245
}
247
246
}
247
+ oldRV := "80"
248
+ cacheRV := "100"
249
+ etcdRV := "120"
248
250
249
251
keyPrefix := "/pods/"
250
- continueOnRev1 , err := storage .EncodeContinue (keyPrefix + "foo" , keyPrefix , 1 )
252
+ continueOnOldRV , err := storage .EncodeContinue (keyPrefix + "foo" , keyPrefix , int64 ( mustAtoi ( oldRV )) )
251
253
if err != nil {
252
254
t .Fatalf ("Unexpected error: %v" , err )
253
255
}
256
+ continueOnCacheRV , err := storage .EncodeContinue (keyPrefix + "foo" , keyPrefix , int64 (mustAtoi (cacheRV )))
257
+ if err != nil {
258
+ t .Fatalf ("Unexpected error: %v" , err )
259
+ }
260
+ // Continue from different apiserver that is forward in RVs
261
+ continueOnEtcdRV , err := storage .EncodeContinue (keyPrefix + "foo" , keyPrefix , int64 (mustAtoi (etcdRV )))
262
+ if err != nil {
263
+ t .Fatalf ("Unexpected error: %v" , err )
264
+ }
265
+
254
266
continueOnNegativeRV , err := storage .EncodeContinue (keyPrefix + "foo" , keyPrefix , - 1 )
255
267
if err != nil {
256
268
t .Fatalf ("Unexpected error: %v" , err )
257
269
}
258
270
testCases := map [opts ]bool {}
259
271
testCases [opts {}] = true
260
272
testCases [opts {Limit : 100 }] = true
261
- testCases [opts {Continue : continueOnRev1 }] = true
262
- testCases [opts {Limit : 100 , Continue : continueOnRev1 }] = true
263
- testCases [opts {Continue : continueOnNegativeRV }] = true
264
- testCases [opts {Limit : 100 , Continue : continueOnNegativeRV }] = true
265
273
testCases [opts {ResourceVersion : "0" }] = false
266
274
testCases [opts {ResourceVersion : "0" , Limit : 100 }] = false
267
- testCases [opts {ResourceVersion : "0" , Continue : continueOnRev1 }] = true
268
- testCases [opts {ResourceVersion : "0" , Limit : 100 , Continue : continueOnRev1 }] = true
269
- testCases [opts {ResourceVersion : "0" , Continue : continueOnNegativeRV }] = true
270
- testCases [opts {ResourceVersion : "0" , Limit : 100 , Continue : continueOnNegativeRV }] = true
271
275
testCases [opts {ResourceVersion : "0" , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan }] = false
272
276
testCases [opts {ResourceVersion : "0" , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan , Limit : 100 }] = false
273
- testCases [opts {ResourceVersion : "1" }] = false
274
- testCases [opts {ResourceVersion : "1" , Limit : 100 }] = true
275
- testCases [opts {ResourceVersion : "1" , ResourceVersionMatch : metav1 .ResourceVersionMatchExact }] = true
276
- testCases [opts {ResourceVersion : "1" , ResourceVersionMatch : metav1 .ResourceVersionMatchExact , Limit : 100 }] = true
277
- testCases [opts {ResourceVersion : "1" , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan }] = false
278
- testCases [opts {ResourceVersion : "1" , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan , Limit : 100 }] = false
277
+ // Continue
278
+ for _ , continueToken := range []string {continueOnOldRV , continueOnCacheRV , continueOnEtcdRV , continueOnNegativeRV } {
279
+ testCases [opts {Continue : continueToken }] = true
280
+ testCases [opts {Limit : 100 , Continue : continueToken }] = true
281
+ testCases [opts {ResourceVersion : "0" , Continue : continueToken }] = true
282
+ testCases [opts {ResourceVersion : "0" , Limit : 100 , Continue : continueToken }] = true
283
+ }
284
+ // With RV
285
+ for _ , rv := range []string {oldRV , cacheRV , etcdRV } {
286
+ testCases [opts {ResourceVersion : rv }] = false
287
+ testCases [opts {ResourceVersion : rv , Limit : 100 }] = true
288
+ testCases [opts {ResourceVersion : rv , ResourceVersionMatch : metav1 .ResourceVersionMatchExact }] = true
289
+ testCases [opts {ResourceVersion : rv , ResourceVersionMatch : metav1 .ResourceVersionMatchExact , Limit : 100 }] = true
290
+ testCases [opts {ResourceVersion : rv , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan }] = false
291
+ testCases [opts {ResourceVersion : rv , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan , Limit : 100 }] = false
292
+ }
279
293
280
294
// Bypass for most requests doesn't depend on Recursive
281
295
for opts , expectBypass := range testCases {
282
296
opts .Recursive = true
283
297
testCases [opts ] = expectBypass
284
298
}
285
299
// Continue is ignored on non recursive LIST
286
- testCases [opts {ResourceVersion : "1" , Continue : continueOnRev1 }] = true
287
- testCases [opts {ResourceVersion : "1" , Continue : continueOnRev1 , Limit : 100 }] = true
288
- testCases [opts {ResourceVersion : "1" , Continue : continueOnNegativeRV }] = true
289
- testCases [opts {ResourceVersion : "1" , Continue : continueOnNegativeRV , Limit : 100 }] = true
300
+ for _ , rv := range []string {oldRV , etcdRV } {
301
+ for _ , continueToken := range []string {continueOnOldRV , continueOnCacheRV , continueOnEtcdRV , continueOnNegativeRV } {
302
+ testCases [opts {ResourceVersion : rv , Continue : continueToken }] = true
303
+ testCases [opts {ResourceVersion : rv , Continue : continueToken , Limit : 100 }] = true
304
+ }
305
+ }
290
306
291
- for _ , rv := range []string {"" , "0" , "1" } {
307
+ for _ , rv := range []string {"" , "0" , oldRV , etcdRV } {
292
308
for _ , match := range []metav1.ResourceVersionMatch {"" , metav1 .ResourceVersionMatchExact , metav1 .ResourceVersionMatchNotOlderThan } {
293
- for _ , continueKey := range []string {"" , continueOnRev1 , continueOnNegativeRV } {
309
+ for _ , continueKey := range []string {"" , continueOnOldRV , continueOnCacheRV , continueOnEtcdRV , continueOnNegativeRV } {
294
310
for _ , limit := range []int64 {0 , 100 } {
295
311
for _ , recursive := range []bool {true , false } {
296
312
opt := opts {
@@ -335,7 +351,18 @@ func TestShouldDelegateList(t *testing.T) {
335
351
expectBypass = bypass
336
352
}
337
353
}
338
- result , err := shouldDelegateList (toStorageOpts (opt ), delegator.CacheWithoutSnapshots {})
354
+ backingStorage := & dummyStorage {}
355
+ backingStorage .getListFn = func (_ context.Context , _ string , _ storage.ListOptions , listObj runtime.Object ) error {
356
+ podList := listObj .(* example.PodList )
357
+ podList .ListMeta = metav1.ListMeta {ResourceVersion : cacheRV }
358
+ return nil
359
+ }
360
+ cacher , _ , err := newTestCacher (backingStorage )
361
+ if err != nil {
362
+ t .Fatalf ("Couldn't create cacher: %v" , err )
363
+ }
364
+ defer cacher .Stop ()
365
+ result , err := shouldDelegateList (toStorageOpts (opt ), cacher )
339
366
if err != nil {
340
367
t .Fatal (err )
341
368
}
@@ -368,6 +395,14 @@ func TestShouldDelegateList(t *testing.T) {
368
395
})
369
396
}
370
397
398
+ func mustAtoi (s string ) int {
399
+ value , err := strconv .Atoi (s )
400
+ if err != nil {
401
+ panic (err )
402
+ }
403
+ return value
404
+ }
405
+
371
406
func TestConsistentReadFallback (t * testing.T ) {
372
407
tcs := []struct {
373
408
name string
0 commit comments