Skip to content

Commit a29f17f

Browse files
authored
Merge pull request #49 from sttts/sttts-check-cluster-aware
Ignore cluster in ctx for non-aware caches, and error for the inverse
2 parents e8b4ff6 + 34979e8 commit a29f17f

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

pkg/cache/internal/cache_reader.go

Lines changed: 23 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,6 +130,7 @@ 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)
124135

125136
switch {
@@ -133,16 +144,16 @@ func (c *CacheReader) List(ctx context.Context, out client.ObjectList, opts ...c
133144
// namespace.
134145
objs, err = byIndexes(c.indexer, listOpts.FieldSelector.Requirements(), clusterName, listOpts.Namespace)
135146
case listOpts.Namespace != "":
136-
if clusterName.Empty() {
137-
objs, err = c.indexer.ByIndex(cache.NamespaceIndex, listOpts.Namespace)
138-
} else {
147+
if isClusterAware && !clusterName.Empty() {
139148
objs, err = c.indexer.ByIndex(kcpcache.ClusterAndNamespaceIndexName, kcpcache.ClusterAndNamespaceIndexKey(clusterName, listOpts.Namespace))
149+
} else {
150+
objs, err = c.indexer.ByIndex(cache.NamespaceIndex, listOpts.Namespace)
140151
}
141152
default:
142-
if clusterName.Empty() {
143-
objs = c.indexer.List()
144-
} else {
153+
if isClusterAware && !clusterName.Empty() {
145154
objs, err = c.indexer.ByIndex(kcpcache.ClusterIndexName, kcpcache.ClusterIndexKey(clusterName))
155+
} else {
156+
objs = c.indexer.List()
146157
}
147158
}
148159
if err != nil {
@@ -198,13 +209,14 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
198209
vals []string
199210
)
200211
indexers := indexer.GetIndexers()
212+
_, isClusterAware := indexers[kcpcache.ClusterAndNamespaceIndexName]
201213
for idx, req := range requires {
202214
indexName := FieldIndexName(req.Field)
203215
var indexedValue string
204-
if clusterName.Empty() {
205-
indexedValue = KeyToNamespacedKey(namespace, req.Value)
206-
} else {
216+
if isClusterAware {
207217
indexedValue = KeyToClusteredKey(clusterName.String(), namespace, req.Value)
218+
} else {
219+
indexedValue = KeyToNamespacedKey(namespace, req.Value)
208220
}
209221
if idx == 0 {
210222
// we use first require to get snapshot data
@@ -248,12 +260,7 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, clusterName
248260
// It's akin to MetaNamespaceKeyFunc. It's separate from
249261
// String to allow keeping the key format easily in sync with
250262
// 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 {
257264
if k.Namespace == "" {
258265
return k.Name
259266
}

0 commit comments

Comments
 (0)