Skip to content

Commit 54e7d27

Browse files
authored
Merge pull request kubernetes#130775 from serathius/watchcache-consistent-read
Fix detecting consistent read when watchcache starts handling continue
2 parents 396c3eb + 8f83f24 commit 54e7d27

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ func (c *CacheDelegator) Get(ctx context.Context, key string, opts storage.GetOp
176176
}
177177

178178
func (c *CacheDelegator) GetList(ctx context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
179-
if shouldDelegateList(opts) {
179+
shouldDelegate, consistentRead := shouldDelegateList(opts)
180+
if shouldDelegate {
180181
return c.storage.GetList(ctx, key, opts, listObj)
181182
}
182183

@@ -198,8 +199,6 @@ func (c *CacheDelegator) GetList(ctx context.Context, key string, opts storage.L
198199
return c.storage.GetList(ctx, key, opts, listObj)
199200
}
200201
}
201-
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
202-
consistentRead := opts.ResourceVersion == "" && utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache) && requestWatchProgressSupported
203202
if consistentRead {
204203
listRV, err = c.storage.GetCurrentResourceVersion(ctx)
205204
if err != nil {
@@ -243,31 +242,33 @@ func shouldDelegateListOnNotReadyCache(opts storage.ListOptions) bool {
243242
// NOTICE: Keep in sync with shouldListFromStorage function in
244243
//
245244
// staging/src/k8s.io/apiserver/pkg/util/flowcontrol/request/list_work_estimator.go
246-
func shouldDelegateList(opts storage.ListOptions) bool {
245+
func shouldDelegateList(opts storage.ListOptions) (shouldDeletage, consistentRead bool) {
247246
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
247+
consistentRead = false
248248
switch opts.ResourceVersionMatch {
249249
case metav1.ResourceVersionMatchExact:
250-
return true
250+
return true, consistentRead
251251
case metav1.ResourceVersionMatchNotOlderThan:
252-
return false
252+
return false, consistentRead
253253
case "":
254254
// Legacy exact match
255255
if opts.Predicate.Limit > 0 && len(opts.ResourceVersion) > 0 && opts.ResourceVersion != "0" {
256-
return true
256+
return true, consistentRead
257257
}
258258
// Continue
259259
if len(opts.Predicate.Continue) > 0 {
260-
return true
260+
return true, consistentRead
261261
}
262262
// Consistent Read
263263
if opts.ResourceVersion == "" {
264+
consistentRead = true
264265
consistentListFromCacheEnabled := utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache)
265266
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
266-
return !consistentListFromCacheEnabled || !requestWatchProgressSupported
267+
return !consistentListFromCacheEnabled || !requestWatchProgressSupported, consistentRead
267268
}
268-
return false
269+
return false, consistentRead
269270
default:
270-
return true
271+
return true, consistentRead
271272
}
272273
}
273274

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package request
1919
import (
2020
"math"
2121
"net/http"
22-
"net/url"
2322

2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2524
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -86,8 +85,8 @@ func (e *listWorkEstimator) estimate(r *http.Request, flowSchemaName, priorityLe
8685
return WorkEstimate{InitialSeats: e.config.MinimumSeats}
8786
}
8887
}
89-
90-
isListFromCache := requestInfo.Verb == "watch" || !shouldListFromStorage(query, &listOptions)
88+
listFromStorage, _ := shouldListFromStorage(&listOptions)
89+
isListFromCache := requestInfo.Verb == "watch" || !listFromStorage
9190

9291
numStored, err := e.countGetterFn(key(requestInfo))
9392
switch {
@@ -163,30 +162,32 @@ func key(requestInfo *apirequest.RequestInfo) string {
163162
// NOTICE: Keep in sync with shouldDelegateList function in
164163
//
165164
// staging/src/k8s.io/apiserver/pkg/storage/cacher/delegator.go
166-
func shouldListFromStorage(query url.Values, opts *metav1.ListOptions) bool {
165+
func shouldListFromStorage(opts *metav1.ListOptions) (shouldDeletage, consistentRead bool) {
167166
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
167+
consistentRead = false
168168
switch opts.ResourceVersionMatch {
169169
case metav1.ResourceVersionMatchExact:
170-
return true
170+
return true, consistentRead
171171
case metav1.ResourceVersionMatchNotOlderThan:
172-
return false
172+
return false, consistentRead
173173
case "":
174174
// Legacy exact match
175175
if opts.Limit > 0 && len(opts.ResourceVersion) > 0 && opts.ResourceVersion != "0" {
176-
return true
176+
return true, consistentRead
177177
}
178178
// Continue
179179
if len(opts.Continue) > 0 {
180-
return true
180+
return true, consistentRead
181181
}
182182
// Consistent Read
183183
if opts.ResourceVersion == "" {
184+
consistentRead = true
184185
consistentListFromCacheEnabled := utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache)
185186
requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
186-
return !consistentListFromCacheEnabled || !requestWatchProgressSupported
187+
return !consistentListFromCacheEnabled || !requestWatchProgressSupported, consistentRead
187188
}
188-
return false
189+
return false, consistentRead
189190
default:
190-
return true
191+
return true, consistentRead
191192
}
192193
}

0 commit comments

Comments
 (0)