Skip to content

Commit 8fdd6fe

Browse files
committed
Unify should delegate list
1 parent aa35eff commit 8fdd6fe

File tree

4 files changed

+44
-63
lines changed

4 files changed

+44
-63
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ 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"
5152
"k8s.io/apiserver/pkg/storage/cacher/metrics"
5253
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
5354
etcdfeature "k8s.io/apiserver/pkg/storage/feature"
@@ -365,7 +366,7 @@ func TestShouldDelegateList(t *testing.T) {
365366
if snapshotAvailable {
366367
cacher.watchCache.snapshots.Add(uint64(mustAtoi(oldRV)), fakeOrderedLister{})
367368
}
368-
result, err := shouldDelegateList(toStorageOpts(opt), cacher)
369+
result, err := delegator.ShouldDelegateList(toStorageOpts(opt), cacher)
369370
if err != nil {
370371
t.Fatal(err)
371372
}

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

+1-30
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, c.cacher)
183+
result, err := delegator.ShouldDelegateList(opts, c.cacher)
184184
if err != nil {
185185
return err
186186
}
@@ -249,35 +249,6 @@ func shouldDelegateListOnNotReadyCache(opts storage.ListOptions) bool {
249249
return noLabelSelector && noFieldSelector && hasLimit
250250
}
251251

252-
// NOTICE: Keep in sync with shouldDelegateList function in
253-
//
254-
// staging/src/k8s.io/apiserver/pkg/util/flowcontrol/request/list_work_estimator.go
255-
func shouldDelegateList(opts storage.ListOptions, cache delegator.Helper) (delegator.Result, error) {
256-
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
257-
switch opts.ResourceVersionMatch {
258-
case metav1.ResourceVersionMatchExact:
259-
return cache.ShouldDelegateExactRV(opts.ResourceVersion, opts.Recursive)
260-
case metav1.ResourceVersionMatchNotOlderThan:
261-
return delegator.Result{ShouldDelegate: false}, nil
262-
case "":
263-
// Continue
264-
if len(opts.Predicate.Continue) > 0 {
265-
return cache.ShouldDelegateContinue(opts.Predicate.Continue, opts.Recursive)
266-
}
267-
// Legacy exact match
268-
if opts.Predicate.Limit > 0 && len(opts.ResourceVersion) > 0 && opts.ResourceVersion != "0" {
269-
return cache.ShouldDelegateExactRV(opts.ResourceVersion, opts.Recursive)
270-
}
271-
// Consistent Read
272-
if opts.ResourceVersion == "" {
273-
return cache.ShouldDelegateConsistentRead()
274-
}
275-
return delegator.Result{ShouldDelegate: false}, nil
276-
default:
277-
return delegator.Result{ShouldDelegate: true}, nil
278-
}
279-
}
280-
281252
func (c *CacheDelegator) GuaranteedUpdate(ctx context.Context, key string, destination runtime.Object, ignoreNotFound bool, preconditions *storage.Preconditions, tryUpdate storage.UpdateFunc, cachedExistingObject runtime.Object) error {
282253
// Ignore the suggestion and try to pass down the current version of the object
283254
// read from cache.

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

+40
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,52 @@ limitations under the License.
1717
package delegator
1818

1919
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021
"k8s.io/apiserver/pkg/features"
2122
"k8s.io/apiserver/pkg/storage"
2223
etcdfeature "k8s.io/apiserver/pkg/storage/feature"
2324
utilfeature "k8s.io/apiserver/pkg/util/feature"
2425
)
2526

27+
func ShouldDelegateListMeta(opts *metav1.ListOptions, cache Helper) (Result, error) {
28+
return ShouldDelegateList(
29+
storage.ListOptions{
30+
ResourceVersionMatch: opts.ResourceVersionMatch,
31+
ResourceVersion: opts.ResourceVersion,
32+
Predicate: storage.SelectionPredicate{
33+
Continue: opts.Continue,
34+
Limit: opts.Limit,
35+
},
36+
Recursive: true,
37+
}, cache)
38+
}
39+
40+
func ShouldDelegateList(opts storage.ListOptions, cache Helper) (Result, error) {
41+
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
42+
switch opts.ResourceVersionMatch {
43+
case metav1.ResourceVersionMatchExact:
44+
return cache.ShouldDelegateExactRV(opts.ResourceVersion, opts.Recursive)
45+
case metav1.ResourceVersionMatchNotOlderThan:
46+
return Result{ShouldDelegate: false}, nil
47+
case "":
48+
// Continue
49+
if len(opts.Predicate.Continue) > 0 {
50+
return cache.ShouldDelegateContinue(opts.Predicate.Continue, opts.Recursive)
51+
}
52+
// Legacy exact match
53+
if opts.Predicate.Limit > 0 && len(opts.ResourceVersion) > 0 && opts.ResourceVersion != "0" {
54+
return cache.ShouldDelegateExactRV(opts.ResourceVersion, opts.Recursive)
55+
}
56+
// Consistent Read
57+
if opts.ResourceVersion == "" {
58+
return cache.ShouldDelegateConsistentRead()
59+
}
60+
return Result{ShouldDelegate: false}, nil
61+
default:
62+
return Result{ShouldDelegate: true}, nil
63+
}
64+
}
65+
2666
type Helper interface {
2767
ShouldDelegateExactRV(rv string, recursive bool) (Result, error)
2868
ShouldDelegateContinue(continueToken string, recursive bool) (Result, error)

staging/src/k8s.io/apiserver/pkg/util/flowcontrol/request/list_work_estimator.go

+1-32
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (e *listWorkEstimator) estimate(r *http.Request, flowSchemaName, priorityLe
8383
}
8484
}
8585
// TODO: Check whether watchcache is enabled.
86-
result, err := shouldDelegateList(&listOptions, delegator.CacheWithoutSnapshots{})
86+
result, err := delegator.ShouldDelegateListMeta(&listOptions, delegator.CacheWithoutSnapshots{})
8787
if err != nil {
8888
return WorkEstimate{InitialSeats: maxSeats}
8989
}
@@ -160,34 +160,3 @@ func key(requestInfo *apirequest.RequestInfo) string {
160160
}
161161
return groupResource.String()
162162
}
163-
164-
// NOTICE: Keep in sync with shouldDelegateList function in
165-
//
166-
// staging/src/k8s.io/apiserver/pkg/storage/cacher/delegator.go
167-
func shouldDelegateList(opts *metav1.ListOptions, cache delegator.Helper) (delegator.Result, error) {
168-
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
169-
switch opts.ResourceVersionMatch {
170-
case metav1.ResourceVersionMatchExact:
171-
return cache.ShouldDelegateExactRV(opts.ResourceVersion, defaultRecursive)
172-
case metav1.ResourceVersionMatchNotOlderThan:
173-
return delegator.Result{ShouldDelegate: false}, nil
174-
case "":
175-
// Continue
176-
if len(opts.Continue) > 0 {
177-
return cache.ShouldDelegateContinue(opts.Continue, defaultRecursive)
178-
}
179-
// Legacy exact match
180-
if opts.Limit > 0 && len(opts.ResourceVersion) > 0 && opts.ResourceVersion != "0" {
181-
return cache.ShouldDelegateExactRV(opts.ResourceVersion, defaultRecursive)
182-
}
183-
// Consistent Read
184-
if opts.ResourceVersion == "" {
185-
return cache.ShouldDelegateConsistentRead()
186-
}
187-
return delegator.Result{ShouldDelegate: false}, nil
188-
default:
189-
return delegator.Result{ShouldDelegate: true}, nil
190-
}
191-
}
192-
193-
var defaultRecursive = true

0 commit comments

Comments
 (0)