Skip to content

Commit 6241fd9

Browse files
committed
UPSTREAM: <squash>: ignore cluster in ctx for non-aware caches, and error for the inverse
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
1 parent cae0049 commit 6241fd9

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

pkg/cache/internal/cache_reader.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,17 @@ func (c *CacheReader) Get(ctx context.Context, key client.ObjectKey, out client.
6262
if c.scopeName == apimeta.RESTScopeNameRoot {
6363
key.Namespace = ""
6464
}
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+
}
6676

6777
// Lookup the object from the indexer cache
6878
obj, exists, err := c.indexer.GetByKey(storeKey)
@@ -120,7 +130,11 @@ func (c *CacheReader) List(ctx context.Context, out client.ObjectList, opts ...c
120130
return fmt.Errorf("continue list option is not supported by the cache")
121131
}
122132

133+
_, isClusterAware := c.indexer.GetIndexers()[kcpcache.ClusterAndNamespaceIndexName]
123134
clusterName, _ := kontext.ClusterFrom(ctx)
135+
if isClusterAware && clusterName.Empty() {
136+
return fmt.Errorf("cluster-aware cache requires a cluster in context")
137+
}
124138

125139
switch {
126140
case listOpts.FieldSelector != nil:
@@ -133,16 +147,16 @@ func (c *CacheReader) List(ctx context.Context, out client.ObjectList, opts ...c
133147
// namespace.
134148
objs, err = byIndexes(c.indexer, listOpts.FieldSelector.Requirements(), clusterName, listOpts.Namespace)
135149
case listOpts.Namespace != "":
136-
if clusterName.Empty() {
137-
objs, err = c.indexer.ByIndex(cache.NamespaceIndex, listOpts.Namespace)
138-
} else {
150+
if isClusterAware {
139151
objs, err = c.indexer.ByIndex(kcpcache.ClusterAndNamespaceIndexName, kcpcache.ClusterAndNamespaceIndexKey(clusterName, listOpts.Namespace))
152+
} else {
153+
objs, err = c.indexer.ByIndex(cache.NamespaceIndex, listOpts.Namespace)
140154
}
141155
default:
142-
if clusterName.Empty() {
143-
objs = c.indexer.List()
144-
} else {
156+
if isClusterAware {
145157
objs, err = c.indexer.ByIndex(kcpcache.ClusterIndexName, kcpcache.ClusterIndexKey(clusterName))
158+
} else {
159+
objs = c.indexer.List()
146160
}
147161
}
148162
if err != nil {
@@ -198,13 +212,14 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
198212
vals []string
199213
)
200214
indexers := indexer.GetIndexers()
215+
_, isClusterAware := indexers[kcpcache.ClusterAndNamespaceIndexName]
201216
for idx, req := range requires {
202217
indexName := FieldIndexName(req.Field)
203218
var indexedValue string
204-
if clusterName.Empty() {
205-
indexedValue = KeyToNamespacedKey(namespace, req.Value)
206-
} else {
219+
if isClusterAware {
207220
indexedValue = KeyToClusteredKey(clusterName.String(), namespace, req.Value)
221+
} else {
222+
indexedValue = KeyToNamespacedKey(namespace, req.Value)
208223
}
209224
if idx == 0 {
210225
// we use first require to get snapshot data
@@ -248,12 +263,7 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
248263
// It's akin to MetaNamespaceKeyFunc. It's separate from
249264
// String to allow keeping the key format easily in sync with
250265
// 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 {
257267
if k.Namespace == "" {
258268
return k.Name
259269
}

0 commit comments

Comments
 (0)