@@ -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,6 +130,7 @@ 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 )
124
135
125
136
switch {
@@ -133,16 +144,16 @@ func (c *CacheReader) List(ctx context.Context, out client.ObjectList, opts ...c
133
144
// namespace.
134
145
objs , err = byIndexes (c .indexer , listOpts .FieldSelector .Requirements (), clusterName , listOpts .Namespace )
135
146
case listOpts .Namespace != "" :
136
- if clusterName .Empty () {
137
- objs , err = c .indexer .ByIndex (cache .NamespaceIndex , listOpts .Namespace )
138
- } else {
147
+ if isClusterAware && ! clusterName .Empty () {
139
148
objs , err = c .indexer .ByIndex (kcpcache .ClusterAndNamespaceIndexName , kcpcache .ClusterAndNamespaceIndexKey (clusterName , listOpts .Namespace ))
149
+ } else {
150
+ objs , err = c .indexer .ByIndex (cache .NamespaceIndex , listOpts .Namespace )
140
151
}
141
152
default :
142
- if clusterName .Empty () {
143
- objs = c .indexer .List ()
144
- } else {
153
+ if isClusterAware && ! clusterName .Empty () {
145
154
objs , err = c .indexer .ByIndex (kcpcache .ClusterIndexName , kcpcache .ClusterIndexKey (clusterName ))
155
+ } else {
156
+ objs = c .indexer .List ()
146
157
}
147
158
}
148
159
if err != nil {
@@ -198,13 +209,14 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
198
209
vals []string
199
210
)
200
211
indexers := indexer .GetIndexers ()
212
+ _ , isClusterAware := indexers [kcpcache .ClusterAndNamespaceIndexName ]
201
213
for idx , req := range requires {
202
214
indexName := FieldIndexName (req .Field )
203
215
var indexedValue string
204
- if clusterName .Empty () {
205
- indexedValue = KeyToNamespacedKey (namespace , req .Value )
206
- } else {
216
+ if isClusterAware {
207
217
indexedValue = KeyToClusteredKey (clusterName .String (), namespace , req .Value )
218
+ } else {
219
+ indexedValue = KeyToNamespacedKey (namespace , req .Value )
208
220
}
209
221
if idx == 0 {
210
222
// we use first require to get snapshot data
@@ -248,12 +260,7 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
248
260
// It's akin to MetaNamespaceKeyFunc. It's separate from
249
261
// String to allow keeping the key format easily in sync with
250
262
// 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
-
263
+ func objectKeyToStoreKey (k client.ObjectKey ) string {
257
264
if k .Namespace == "" {
258
265
return k .Name
259
266
}
0 commit comments