@@ -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 {
@@ -248,12 +262,7 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
248
262
// It's akin to MetaNamespaceKeyFunc. It's separate from
249
263
// String to allow keeping the key format easily in sync with
250
264
// 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
-
265
+ func objectKeyToStoreKey (k client.ObjectKey ) string {
257
266
if k .Namespace == "" {
258
267
return k .Name
259
268
}
0 commit comments