Fix detecting consistent read when watchcache starts handling continue

Kubernetes-commit: 8f83f2446a5e2f11eb751fb56067c663b51cfd12
This commit is contained in:
Marek Siarkowicz 2025-03-12 18:37:10 +01:00 committed by Kubernetes Publisher
parent d88392f1f6
commit 3cb2448d98
2 changed files with 24 additions and 22 deletions

View File

@ -176,7 +176,8 @@ func (c *CacheDelegator) Get(ctx context.Context, key string, opts storage.GetOp
}
func (c *CacheDelegator) GetList(ctx context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
if shouldDelegateList(opts) {
shouldDelegate, consistentRead := shouldDelegateList(opts)
if shouldDelegate {
return c.storage.GetList(ctx, key, opts, listObj)
}
@ -198,8 +199,6 @@ func (c *CacheDelegator) GetList(ctx context.Context, key string, opts storage.L
return c.storage.GetList(ctx, key, opts, listObj)
}
}
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
consistentRead := opts.ResourceVersion == "" && utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache) && requestWatchProgressSupported
if consistentRead {
listRV, err = c.storage.GetCurrentResourceVersion(ctx)
if err != nil {
@ -243,31 +242,33 @@ func shouldDelegateListOnNotReadyCache(opts storage.ListOptions) bool {
// NOTICE: Keep in sync with shouldListFromStorage function in
//
// staging/src/k8s.io/apiserver/pkg/util/flowcontrol/request/list_work_estimator.go
func shouldDelegateList(opts storage.ListOptions) bool {
func shouldDelegateList(opts storage.ListOptions) (shouldDeletage, consistentRead bool) {
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
consistentRead = false
switch opts.ResourceVersionMatch {
case metav1.ResourceVersionMatchExact:
return true
return true, consistentRead
case metav1.ResourceVersionMatchNotOlderThan:
return false
return false, consistentRead
case "":
// Legacy exact match
if opts.Predicate.Limit > 0 && len(opts.ResourceVersion) > 0 && opts.ResourceVersion != "0" {
return true
return true, consistentRead
}
// Continue
if len(opts.Predicate.Continue) > 0 {
return true
return true, consistentRead
}
// Consistent Read
if opts.ResourceVersion == "" {
consistentRead = true
consistentListFromCacheEnabled := utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache)
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
return !consistentListFromCacheEnabled || !requestWatchProgressSupported
return !consistentListFromCacheEnabled || !requestWatchProgressSupported, consistentRead
}
return false
return false, consistentRead
default:
return true
return true, consistentRead
}
}

View File

@ -19,7 +19,6 @@ package request
import (
"math"
"net/http"
"net/url"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
@ -86,8 +85,8 @@ func (e *listWorkEstimator) estimate(r *http.Request, flowSchemaName, priorityLe
return WorkEstimate{InitialSeats: e.config.MinimumSeats}
}
}
isListFromCache := requestInfo.Verb == "watch" || !shouldListFromStorage(query, &listOptions)
listFromStorage, _ := shouldListFromStorage(&listOptions)
isListFromCache := requestInfo.Verb == "watch" || !listFromStorage
numStored, err := e.countGetterFn(key(requestInfo))
switch {
@ -163,30 +162,32 @@ func key(requestInfo *apirequest.RequestInfo) string {
// NOTICE: Keep in sync with shouldDelegateList function in
//
// staging/src/k8s.io/apiserver/pkg/storage/cacher/delegator.go
func shouldListFromStorage(query url.Values, opts *metav1.ListOptions) bool {
func shouldListFromStorage(opts *metav1.ListOptions) (shouldDeletage, consistentRead bool) {
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
consistentRead = false
switch opts.ResourceVersionMatch {
case metav1.ResourceVersionMatchExact:
return true
return true, consistentRead
case metav1.ResourceVersionMatchNotOlderThan:
return false
return false, consistentRead
case "":
// Legacy exact match
if opts.Limit > 0 && len(opts.ResourceVersion) > 0 && opts.ResourceVersion != "0" {
return true
return true, consistentRead
}
// Continue
if len(opts.Continue) > 0 {
return true
return true, consistentRead
}
// Consistent Read
if opts.ResourceVersion == "" {
consistentRead = true
consistentListFromCacheEnabled := utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache)
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
return !consistentListFromCacheEnabled || !requestWatchProgressSupported
return !consistentListFromCacheEnabled || !requestWatchProgressSupported, consistentRead
}
return false
return false, consistentRead
default:
return true
return true, consistentRead
}
}