Debian Codegraph @

pkg/storage/cacher/delegator/interface.go

raw
1 /*
2 Copyright 2025 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package delegator
18
19 import (
20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21 "k8s.io/apiserver/pkg/features"
22 "k8s.io/apiserver/pkg/storage"
23 etcdfeature "k8s.io/apiserver/pkg/storage/feature"
24 utilfeature "k8s.io/apiserver/pkg/util/feature"
25 )
26
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
66 type Helper interface {
67 ShouldDelegateExactRV(rv string, recursive bool) (Result, error)
68 ShouldDelegateContinue(continueToken string, recursive bool) (Result, error)
69 ShouldDelegateConsistentRead() (Result, error)
70 }
71
72 // Result of delegator decision.
73 type Result struct {
74 // Whether a request cannot be served by cache and should be delegated to etcd.
75 ShouldDelegate bool
76 // Whether a request is a consistent read, used by delegator to decide if it should call GetCurrentResourceVersion to get RV.
77 // Included in interface as only cacher has keyPrefix needed to parse continue token.
78 ConsistentRead bool
79 }
80
81 type CacheWithoutSnapshots struct{}
82
83 var _ Helper = CacheWithoutSnapshots{}
84
85 func (c CacheWithoutSnapshots) ShouldDelegateContinue(continueToken string, recursive bool) (Result, error) {
86 return Result{
87 ShouldDelegate: true,
88 // Continue with negative RV is considered a consistent read, however token cannot be parsed without keyPrefix unavailable in staging/src/k8s.io/apiserver/pkg/util/flow_control/request/list_work_estimator.go.
89 ConsistentRead: false,
90 }, nil
91 }
92
93 func (c CacheWithoutSnapshots) ShouldDelegateExactRV(rv string, recursive bool) (Result, error) {
94 return Result{
95 ShouldDelegate: true,
96 ConsistentRead: false,
97 }, nil
98 }
99
100 func (c CacheWithoutSnapshots) ShouldDelegateConsistentRead() (Result, error) {
101 return Result{
102 ShouldDelegate: !ConsistentReadSupported(),
103 ConsistentRead: true,
104 }, nil
105 }
106
107 // ConsistentReadSupported returns whether cache can be used to serve reads with RV not yet observed by cache, including both consistent reads.
108 // Function is located here to avoid import cycles between staging/src/k8s.io/apiserver/pkg/storage/cacher/delegator.go and staging/src/k8s.io/apiserver/pkg/util/flow_control/request/list_work_estimator.go.
109 func ConsistentReadSupported() bool {
110 consistentListFromCacheEnabled := utilfeature.DefaultFeatureGate.Enabled(features.ConsistentListFromCache)
111 requestWatchProgressSupported := etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
112 return consistentListFromCacheEnabled && requestWatchProgressSupported
113 }