@@ -22,6 +22,7 @@ import (
22
22
. "github.com/onsi/ginkgo/v2"
23
23
. "github.com/onsi/gomega"
24
24
corev1 "k8s.io/api/core/v1"
25
+ "k8s.io/apimachinery/pkg/fields"
25
26
"sigs.k8s.io/controller-runtime/pkg/cache"
26
27
"sigs.k8s.io/controller-runtime/pkg/client"
27
28
"sigs.k8s.io/controller-runtime/pkg/kcp"
@@ -56,6 +57,31 @@ var _ = Describe("KCP cluster-unaware informer cache", func() {
56
57
err = c .Get (ctx , client.ObjectKey {Name : "default" }, ns )
57
58
Expect (err ).NotTo (HaveOccurred ())
58
59
})
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
+ })
59
85
})
60
86
61
87
var _ = Describe ("KCP cluster-aware informer cache" , func () {
@@ -86,4 +112,29 @@ var _ = Describe("KCP cluster-aware informer cache", func() {
86
112
By ("By getting the default namespace with the informer, but cluster-aware key should succeed" )
87
113
err = c .Get (kontext .WithCluster (ctx , "cluster1" ), client.ObjectKey {Name : "default" , Namespace : "cluster1" }, ns )
88
114
})
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
+ })
89
140
})
0 commit comments