@@ -62,7 +62,17 @@ func (c *CacheReader) Get(ctx context.Context, key client.ObjectKey, out client.
62
62
if c .scopeName == apimeta .RESTScopeNameRoot {
63
63
key .Namespace = ""
64
64
}
65
- storeKey := objectKeyToStoreKey (ctx , key )
65
+ storeKey := objectKeyToStoreKey (key )
66
+
67
+ // create cluster-aware key for KCP
68
+ _ , isClusterAware := c .indexer .GetIndexers ()[kcpcache .ClusterAndNamespaceIndexName ]
69
+ clusterName , _ := kontext .ClusterFrom (ctx )
70
+ if isClusterAware && clusterName .Empty () {
71
+ return fmt .Errorf ("cluster-aware cache requires a cluster in context" )
72
+ }
73
+ if isClusterAware {
74
+ storeKey = clusterName .String () + "|" + storeKey
75
+ }
66
76
67
77
// Lookup the object from the indexer cache
68
78
obj , exists , err := c .indexer .GetByKey (storeKey )
@@ -120,7 +130,11 @@ func (c *CacheReader) List(ctx context.Context, out client.ObjectList, opts ...c
120
130
return fmt .Errorf ("continue list option is not supported by the cache" )
121
131
}
122
132
133
+ _ , isClusterAware := c .indexer .GetIndexers ()[kcpcache .ClusterAndNamespaceIndexName ]
123
134
clusterName , _ := kontext .ClusterFrom (ctx )
135
+ if isClusterAware && clusterName .Empty () {
136
+ return fmt .Errorf ("cluster-aware cache requires a cluster in context" )
137
+ }
124
138
125
139
switch {
126
140
case listOpts .FieldSelector != nil :
@@ -133,16 +147,16 @@ func (c *CacheReader) List(ctx context.Context, out client.ObjectList, opts ...c
133
147
// namespace.
134
148
objs , err = byIndexes (c .indexer , listOpts .FieldSelector .Requirements (), clusterName , listOpts .Namespace )
135
149
case listOpts .Namespace != "" :
136
- if clusterName .Empty () {
137
- objs , err = c .indexer .ByIndex (cache .NamespaceIndex , listOpts .Namespace )
138
- } else {
150
+ if isClusterAware {
139
151
objs , err = c .indexer .ByIndex (kcpcache .ClusterAndNamespaceIndexName , kcpcache .ClusterAndNamespaceIndexKey (clusterName , listOpts .Namespace ))
152
+ } else {
153
+ objs , err = c .indexer .ByIndex (cache .NamespaceIndex , listOpts .Namespace )
140
154
}
141
155
default :
142
- if clusterName .Empty () {
143
- objs = c .indexer .List ()
144
- } else {
156
+ if isClusterAware {
145
157
objs , err = c .indexer .ByIndex (kcpcache .ClusterIndexName , kcpcache .ClusterIndexKey (clusterName ))
158
+ } else {
159
+ objs = c .indexer .List ()
146
160
}
147
161
}
148
162
if err != nil {
@@ -198,13 +212,14 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
198
212
vals []string
199
213
)
200
214
indexers := indexer .GetIndexers ()
215
+ _ , isClusterAware := indexers [kcpcache .ClusterAndNamespaceIndexName ]
201
216
for idx , req := range requires {
202
217
indexName := FieldIndexName (req .Field )
203
218
var indexedValue string
204
- if clusterName .Empty () {
205
- indexedValue = KeyToNamespacedKey (namespace , req .Value )
206
- } else {
219
+ if isClusterAware {
207
220
indexedValue = KeyToClusteredKey (clusterName .String (), namespace , req .Value )
221
+ } else {
222
+ indexedValue = KeyToNamespacedKey (namespace , req .Value )
208
223
}
209
224
if idx == 0 {
210
225
// we use first require to get snapshot data
@@ -248,12 +263,7 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
248
263
// It's akin to MetaNamespaceKeyFunc. It's separate from
249
264
// String to allow keeping the key format easily in sync with
250
265
// MetaNamespaceKeyFunc.
251
- func objectKeyToStoreKey (ctx context.Context , k client.ObjectKey ) string {
252
- cluster , ok := kontext .ClusterFrom (ctx )
253
- if ok {
254
- return kcpcache .ToClusterAwareKey (cluster .String (), k .Namespace , k .Name )
255
- }
256
-
266
+ func objectKeyToStoreKey (k client.ObjectKey ) string {
257
267
if k .Namespace == "" {
258
268
return k .Name
259
269
}
0 commit comments