@@ -21,6 +21,7 @@ import (
21
21
"fmt"
22
22
"reflect"
23
23
"sort"
24
+ "strconv"
24
25
25
26
. "github.com/onsi/ginkgo"
26
27
. "github.com/onsi/ginkgo/extensions/table"
@@ -73,6 +74,33 @@ func createPodWithLabels(name, namespace string, restartPolicy corev1.RestartPol
73
74
return pod
74
75
}
75
76
77
+ func createSvc (name , namespace string , cl client.Client ) client.Object {
78
+ svc := & corev1.Service {
79
+ ObjectMeta : metav1.ObjectMeta {
80
+ Name : name ,
81
+ Namespace : namespace ,
82
+ },
83
+ Spec : corev1.ServiceSpec {
84
+ Ports : []corev1.ServicePort {{Port : 1 }},
85
+ },
86
+ }
87
+ err := cl .Create (context .Background (), svc )
88
+ Expect (err ).NotTo (HaveOccurred ())
89
+ return svc
90
+ }
91
+
92
+ func createSA (name , namespace string , cl client.Client ) client.Object {
93
+ sa := & corev1.ServiceAccount {
94
+ ObjectMeta : metav1.ObjectMeta {
95
+ Name : name ,
96
+ Namespace : namespace ,
97
+ },
98
+ }
99
+ err := cl .Create (context .Background (), sa )
100
+ Expect (err ).NotTo (HaveOccurred ())
101
+ return sa
102
+ }
103
+
76
104
func createPod (name , namespace string , restartPolicy corev1.RestartPolicy ) client.Object {
77
105
return createPodWithLabels (name , namespace , restartPolicy , nil )
78
106
}
@@ -93,6 +121,76 @@ var _ = Describe("Multi-Namespace Informer Cache", func() {
93
121
var _ = Describe ("Informer Cache without DeepCopy" , func () {
94
122
CacheTest (cache .New , cache.Options {UnsafeDisableDeepCopyByObject : cache.DisableDeepCopyByObject {cache.ObjectAll {}: true }})
95
123
})
124
+ var _ = Describe ("Cache with selectors" , func () {
125
+ defer GinkgoRecover ()
126
+ var (
127
+ informerCache cache.Cache
128
+ informerCacheCtx context.Context
129
+ informerCacheCancel context.CancelFunc
130
+ )
131
+
132
+ BeforeEach (func () {
133
+ informerCacheCtx , informerCacheCancel = context .WithCancel (context .Background ())
134
+ Expect (cfg ).NotTo (BeNil ())
135
+ cl , err := client .New (cfg , client.Options {})
136
+ Expect (err ).NotTo (HaveOccurred ())
137
+ err = ensureNamespace (testNamespaceOne , cl )
138
+ Expect (err ).NotTo (HaveOccurred ())
139
+ err = ensureNamespace (testNamespaceTwo , cl )
140
+ Expect (err ).NotTo (HaveOccurred ())
141
+ for idx , namespace := range []string {testNamespaceOne , testNamespaceTwo } {
142
+ _ = createSA ("test-sa-" + strconv .Itoa (idx ), namespace , cl )
143
+ _ = createSvc ("test-svc-" + strconv .Itoa (idx ), namespace , cl )
144
+ }
145
+
146
+ opts := cache.Options {
147
+ SelectorsByObject : cache.SelectorsByObject {
148
+ & corev1.ServiceAccount {}: {Field : fields .OneTermEqualSelector ("metadata.namespace" , testNamespaceOne )},
149
+ },
150
+ DefaultSelector : cache.ObjectSelector {Field : fields .OneTermEqualSelector ("metadata.namespace" , testNamespaceTwo )},
151
+ }
152
+
153
+ By ("creating the informer cache" )
154
+ informerCache , err = cache .New (cfg , opts )
155
+ Expect (err ).NotTo (HaveOccurred ())
156
+ By ("running the cache and waiting for it to sync" )
157
+ // pass as an arg so that we don't race between close and re-assign
158
+ go func (ctx context.Context ) {
159
+ defer GinkgoRecover ()
160
+ Expect (informerCache .Start (ctx )).To (Succeed ())
161
+ }(informerCacheCtx )
162
+ Expect (informerCache .WaitForCacheSync (informerCacheCtx )).To (BeTrue ())
163
+ })
164
+
165
+ AfterEach (func () {
166
+ ctx := context .Background ()
167
+ cl , err := client .New (cfg , client.Options {})
168
+ Expect (err ).NotTo (HaveOccurred ())
169
+ for idx , namespace := range []string {testNamespaceOne , testNamespaceTwo } {
170
+ err = cl .Delete (ctx , & corev1.ServiceAccount {ObjectMeta : metav1.ObjectMeta {Namespace : namespace , Name : "test-sa-" + strconv .Itoa (idx )}})
171
+ Expect (err ).NotTo (HaveOccurred ())
172
+ err = cl .Delete (ctx , & corev1.Service {ObjectMeta : metav1.ObjectMeta {Namespace : namespace , Name : "test-svc-" + strconv .Itoa (idx )}})
173
+ Expect (err ).NotTo (HaveOccurred ())
174
+ }
175
+ informerCacheCancel ()
176
+ })
177
+
178
+ It ("Should list serviceaccounts and find exactly one in namespace " + testNamespaceOne , func () {
179
+ var sas corev1.ServiceAccountList
180
+ err := informerCache .List (informerCacheCtx , & sas )
181
+ Expect (err ).NotTo (HaveOccurred ())
182
+ Expect (len (sas .Items )).To (Equal (1 ))
183
+ Expect (sas .Items [0 ].Namespace ).To (Equal (testNamespaceOne ))
184
+ })
185
+
186
+ It ("Should list services and find exactly one in namespace " + testNamespaceTwo , func () {
187
+ var svcs corev1.ServiceList
188
+ err := informerCache .List (informerCacheCtx , & svcs )
189
+ Expect (err ).NotTo (HaveOccurred ())
190
+ Expect (len (svcs .Items )).To (Equal (1 ))
191
+ Expect (svcs .Items [0 ].Namespace ).To (Equal (testNamespaceTwo ))
192
+ })
193
+ })
96
194
97
195
func CacheTest (createCacheFunc func (config * rest.Config , opts cache.Options ) (cache.Cache , error ), opts cache.Options ) {
98
196
Describe ("Cache test" , func () {
0 commit comments