Skip to content

Commit 929a9c0

Browse files
committed
Extend shouldDelegateList testing incorportating state of cacher
1 parent 73f54b6 commit 929a9c0

File tree

3 files changed

+72
-24
lines changed

3 files changed

+72
-24
lines changed

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"k8s.io/apiserver/pkg/endpoints/request"
4343
"k8s.io/apiserver/pkg/features"
4444
"k8s.io/apiserver/pkg/storage"
45+
"k8s.io/apiserver/pkg/storage/cacher/delegator"
4546
"k8s.io/apiserver/pkg/storage/cacher/metrics"
4647
"k8s.io/apiserver/pkg/storage/cacher/progress"
4748
etcdfeature "k8s.io/apiserver/pkg/storage/feature"
@@ -1338,6 +1339,18 @@ func newErrWatcher(err error) *errWatcher {
13381339
return watcher
13391340
}
13401341

1342+
func (c *Cacher) ShouldDelegateExactRV(resourceVersion string, recursive bool) (delegator.Result, error) {
1343+
return delegator.CacheWithoutSnapshots{}.ShouldDelegateExactRV(resourceVersion, recursive)
1344+
}
1345+
1346+
func (c *Cacher) ShouldDelegateContinue(continueToken string, recursive bool) (delegator.Result, error) {
1347+
return delegator.CacheWithoutSnapshots{}.ShouldDelegateContinue(continueToken, recursive)
1348+
}
1349+
1350+
func (c *Cacher) ShouldDelegateConsistentRead() (delegator.Result, error) {
1351+
return delegator.CacheWithoutSnapshots{}.ShouldDelegateConsistentRead()
1352+
}
1353+
13411354
// Implements watch.Interface.
13421355
func (c *errWatcher) ResultChan() <-chan watch.Event {
13431356
return c.result

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import (
4848
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
4949
"k8s.io/apiserver/pkg/features"
5050
"k8s.io/apiserver/pkg/storage"
51-
"k8s.io/apiserver/pkg/storage/cacher/delegator"
5251
"k8s.io/apiserver/pkg/storage/cacher/metrics"
5352
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
5453
etcdfeature "k8s.io/apiserver/pkg/storage/feature"
@@ -245,52 +244,69 @@ func TestShouldDelegateList(t *testing.T) {
245244
},
246245
}
247246
}
247+
oldRV := "80"
248+
cacheRV := "100"
249+
etcdRV := "120"
248250

249251
keyPrefix := "/pods/"
250-
continueOnRev1, err := storage.EncodeContinue(keyPrefix+"foo", keyPrefix, 1)
252+
continueOnOldRV, err := storage.EncodeContinue(keyPrefix+"foo", keyPrefix, int64(mustAtoi(oldRV)))
251253
if err != nil {
252254
t.Fatalf("Unexpected error: %v", err)
253255
}
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+
254266
continueOnNegativeRV, err := storage.EncodeContinue(keyPrefix+"foo", keyPrefix, -1)
255267
if err != nil {
256268
t.Fatalf("Unexpected error: %v", err)
257269
}
258270
testCases := map[opts]bool{}
259271
testCases[opts{}] = true
260272
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
265273
testCases[opts{ResourceVersion: "0"}] = false
266274
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
271275
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = false
272276
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+
}
279293

280294
// Bypass for most requests doesn't depend on Recursive
281295
for opts, expectBypass := range testCases {
282296
opts.Recursive = true
283297
testCases[opts] = expectBypass
284298
}
285299
// 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+
}
290306

291-
for _, rv := range []string{"", "0", "1"} {
307+
for _, rv := range []string{"", "0", oldRV, etcdRV} {
292308
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} {
294310
for _, limit := range []int64{0, 100} {
295311
for _, recursive := range []bool{true, false} {
296312
opt := opts{
@@ -335,7 +351,18 @@ func TestShouldDelegateList(t *testing.T) {
335351
expectBypass = bypass
336352
}
337353
}
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)
339366
if err != nil {
340367
t.Fatal(err)
341368
}
@@ -368,6 +395,14 @@ func TestShouldDelegateList(t *testing.T) {
368395
})
369396
}
370397

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+
371406
func TestConsistentReadFallback(t *testing.T) {
372407
tcs := []struct {
373408
name string

staging/src/k8s.io/apiserver/pkg/storage/cacher/delegator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (c *CacheDelegator) GetList(ctx context.Context, key string, opts storage.L
180180
if err != nil {
181181
return err
182182
}
183-
result, err := shouldDelegateList(opts, delegator.CacheWithoutSnapshots{})
183+
result, err := shouldDelegateList(opts, c.cacher)
184184
if err != nil {
185185
return err
186186
}

0 commit comments

Comments
 (0)