Skip to content

Commit 20e1944

Browse files
authored
Merge pull request kubernetes#129439 from serathius/refactor-delegate-2
Refactor shouldDelegateList
2 parents db1da72 + e5a3bdb commit 20e1944

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -785,22 +785,30 @@ func (c *Cacher) Get(ctx context.Context, key string, opts storage.GetOptions, o
785785
//
786786
// staging/src/k8s.io/apiserver/pkg/util/flowcontrol/request/list_work_estimator.go
787787
func shouldDelegateList(opts storage.ListOptions) bool {
788-
resourceVersion := opts.ResourceVersion
789-
pred := opts.Predicate
790-
match := opts.ResourceVersionMatch
791-
consistentListFromCacheEnabled := utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache)
792-
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
793-
794-
// Serve consistent reads from storage if ConsistentListFromCache is disabled
795-
consistentReadFromStorage := resourceVersion == "" && !(consistentListFromCacheEnabled && requestWatchProgressSupported)
796-
// Watch cache doesn't support continuations, so serve them from etcd.
797-
hasContinuation := len(pred.Continue) > 0
798-
// Watch cache only supports ResourceVersionMatchNotOlderThan (default).
799788
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
800-
isLegacyExactMatch := opts.Predicate.Limit > 0 && match == "" && len(resourceVersion) > 0 && resourceVersion != "0"
801-
unsupportedMatch := match != "" && match != metav1.ResourceVersionMatchNotOlderThan || isLegacyExactMatch
802-
803-
return consistentReadFromStorage || hasContinuation || unsupportedMatch
789+
switch opts.ResourceVersionMatch {
790+
case metav1.ResourceVersionMatchExact:
791+
return true
792+
case metav1.ResourceVersionMatchNotOlderThan:
793+
case "":
794+
// Legacy exact match
795+
if opts.Predicate.Limit > 0 && len(opts.ResourceVersion) > 0 && opts.ResourceVersion != "0" {
796+
return true
797+
}
798+
default:
799+
return true
800+
}
801+
// Continue
802+
if len(opts.Predicate.Continue) > 0 {
803+
return true
804+
}
805+
// Consistent Read
806+
if opts.ResourceVersion == "" {
807+
consistentListFromCacheEnabled := utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache)
808+
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
809+
return !consistentListFromCacheEnabled || !requestWatchProgressSupported
810+
}
811+
return false
804812
}
805813

806814
// computeListLimit determines whether the cacher should

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,28 @@ func key(requestInfo *apirequest.RequestInfo) string {
164164
//
165165
// staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go
166166
func shouldListFromStorage(query url.Values, opts *metav1.ListOptions) bool {
167-
resourceVersion := opts.ResourceVersion
168-
match := opts.ResourceVersionMatch
169-
consistentListFromCacheEnabled := utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache)
170-
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
171-
172-
// Serve consistent reads from storage if ConsistentListFromCache is disabled
173-
consistentReadFromStorage := resourceVersion == "" && !(consistentListFromCacheEnabled && requestWatchProgressSupported)
174-
// Watch cache doesn't support continuations, so serve them from etcd.
175-
hasContinuation := len(opts.Continue) > 0
176-
// Watch cache only supports ResourceVersionMatchNotOlderThan (default).
177167
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
178-
isLegacyExactMatch := opts.Limit > 0 && match == "" && len(resourceVersion) > 0 && resourceVersion != "0"
179-
unsupportedMatch := match != "" && match != metav1.ResourceVersionMatchNotOlderThan || isLegacyExactMatch
180-
181-
return consistentReadFromStorage || hasContinuation || unsupportedMatch
168+
switch opts.ResourceVersionMatch {
169+
case metav1.ResourceVersionMatchExact:
170+
return true
171+
case metav1.ResourceVersionMatchNotOlderThan:
172+
case "":
173+
// Legacy exact match
174+
if opts.Limit > 0 && len(opts.ResourceVersion) > 0 && opts.ResourceVersion != "0" {
175+
return true
176+
}
177+
default:
178+
return true
179+
}
180+
// Continue
181+
if len(opts.Continue) > 0 {
182+
return true
183+
}
184+
// Consistent Read
185+
if opts.ResourceVersion == "" {
186+
consistentListFromCacheEnabled := utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache)
187+
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
188+
return !consistentListFromCacheEnabled || !requestWatchProgressSupported
189+
}
190+
return false
182191
}

0 commit comments

Comments
 (0)