Skip to content

Commit c85c6fa

Browse files
committed
UPSTREAM: SQUASH: IndexField: only call cluster-aware keyFunc for cluster-aware cache
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
1 parent edf9683 commit c85c6fa

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

pkg/cache/cache.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"net/http"
23+
"strings"
2324
"time"
2425

2526
"golang.org/x/exp/maps"
@@ -390,6 +391,7 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
390391
NewInformer: opts.NewInformerFunc,
391392
}),
392393
readerFailOnMissingInformer: opts.ReaderFailOnMissingInformer,
394+
clusterIndexes: strings.HasSuffix(restConfig.Host, "/clusters/*"),
393395
}
394396
}
395397
}

pkg/cache/informer_cache.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type informerCache struct {
7070
scheme *runtime.Scheme
7171
*internal.Informers
7272
readerFailOnMissingInformer bool
73+
clusterIndexes bool
7374
}
7475

7576
// Get implements Reader.
@@ -219,10 +220,10 @@ func (ic *informerCache) IndexField(ctx context.Context, obj client.Object, fiel
219220
if err != nil {
220221
return err
221222
}
222-
return indexByField(informer, field, extractValue)
223+
return indexByField(informer, field, extractValue, ic.clusterIndexes)
223224
}
224225

225-
func indexByField(informer Informer, field string, extractValue client.IndexerFunc) error {
226+
func indexByField(informer Informer, field string, extractValue client.IndexerFunc, clusterIndexes bool) error {
226227
indexFunc := func(objRaw interface{}) ([]string, error) {
227228
// TODO(directxman12): check if this is the correct type?
228229
obj, isObj := objRaw.(client.Object)
@@ -236,7 +237,7 @@ func indexByField(informer Informer, field string, extractValue client.IndexerFu
236237
ns := meta.GetNamespace()
237238

238239
keyFunc := internal.KeyToNamespacedKey
239-
if clusterName := logicalcluster.From(obj); !clusterName.Empty() {
240+
if clusterName := logicalcluster.From(obj); clusterIndexes && !clusterName.Empty() {
240241
keyFunc = func(ns, val string) string {
241242
return internal.KeyToClusteredKey(clusterName.String(), ns, val)
242243
}

pkg/cache/kcp_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
. "github.com/onsi/ginkgo/v2"
2323
. "github.com/onsi/gomega"
2424
corev1 "k8s.io/api/core/v1"
25+
"k8s.io/apimachinery/pkg/fields"
2526
"sigs.k8s.io/controller-runtime/pkg/cache"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728
"sigs.k8s.io/controller-runtime/pkg/kcp"
@@ -56,6 +57,31 @@ var _ = Describe("KCP cluster-unaware informer cache", func() {
5657
err = c.Get(ctx, client.ObjectKey{Name: "default"}, ns)
5758
Expect(err).NotTo(HaveOccurred())
5859
})
60+
61+
It("should support indexes with cluster-less keys", func() {
62+
ctx, cancel := context.WithCancel(context.Background())
63+
defer cancel()
64+
65+
c, err := cache.New(cfg, cache.Options{})
66+
Expect(err).NotTo(HaveOccurred())
67+
68+
By("Indexing the default namespace by name")
69+
err = c.IndexField(ctx, &corev1.Namespace{}, "my-name", func(obj client.Object) []string {
70+
return []string{"my-key-" + obj.GetName()}
71+
})
72+
Expect(err).NotTo(HaveOccurred())
73+
74+
go c.Start(ctx) //nolint:errcheck // Start is blocking, and error not relevant here.
75+
c.WaitForCacheSync(ctx)
76+
77+
By("By getting the default namespace via the custom index")
78+
nss := &corev1.NamespaceList{}
79+
err = c.List(ctx, nss, client.MatchingFieldsSelector{
80+
Selector: fields.OneTermEqualSelector("my-name", "my-key-default"),
81+
})
82+
Expect(err).NotTo(HaveOccurred())
83+
Expect(nss.Items).To(HaveLen(1))
84+
})
5985
})
6086

6187
var _ = Describe("KCP cluster-aware informer cache", func() {
@@ -86,4 +112,29 @@ var _ = Describe("KCP cluster-aware informer cache", func() {
86112
By("By getting the default namespace with the informer, but cluster-aware key should succeed")
87113
err = c.Get(kontext.WithCluster(ctx, "cluster1"), client.ObjectKey{Name: "default", Namespace: "cluster1"}, ns)
88114
})
115+
116+
It("should support indexes with cluster-aware keys", func() {
117+
ctx, cancel := context.WithCancel(context.Background())
118+
defer cancel()
119+
120+
c, err := cache.New(cfg, cache.Options{})
121+
Expect(err).NotTo(HaveOccurred())
122+
123+
By("Indexing the default namespace by name")
124+
err = c.IndexField(ctx, &corev1.Namespace{}, "my-name", func(obj client.Object) []string {
125+
return []string{"my-key-" + obj.GetName()}
126+
})
127+
Expect(err).NotTo(HaveOccurred())
128+
129+
go c.Start(ctx) //nolint:errcheck // Start is blocking, and error not relevant here.
130+
c.WaitForCacheSync(ctx)
131+
132+
By("By getting the default namespace via the custom index")
133+
nss := &corev1.NamespaceList{}
134+
err = c.List(ctx, nss, client.MatchingFieldsSelector{
135+
Selector: fields.OneTermEqualSelector("my-name", "my-key-default"),
136+
})
137+
Expect(err).NotTo(HaveOccurred())
138+
Expect(nss.Items).To(HaveLen(1))
139+
})
89140
})

0 commit comments

Comments
 (0)