Skip to content

Commit 57d9cb3

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 9eb4667 commit 57d9cb3

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

pkg/cache/internal/cache_reader.go

Lines changed: 22 additions & 13 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 {
@@ -248,12 +262,7 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
248262
// It's akin to MetaNamespaceKeyFunc. It's separate from
249263
// String to allow keeping the key format easily in sync with
250264
// 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 {
257266
if k.Namespace == "" {
258267
return k.Name
259268
}

0 commit comments

Comments
 (0)