@@ -105,6 +105,10 @@ var _ = Describe("Provider Namespace", Ordered, func() {
105
105
}
106
106
return reconcile.Result {}, err
107
107
}
108
+ if cm .GetLabels ()["type" ] != "animal" {
109
+ return reconcile.Result {}, nil
110
+ }
111
+
108
112
cm .Data = map [string ]string {"stomach" : "food" }
109
113
if err := cl .GetClient ().Update (ctx , cm ); err != nil {
110
114
return reconcile.Result {}, err
@@ -114,6 +118,13 @@ var _ = Describe("Provider Namespace", Ordered, func() {
114
118
},
115
119
))
116
120
Expect (err ).NotTo (HaveOccurred ())
121
+
122
+ By ("Adding an index to the provider clusters" , func () {
123
+ err := mgr .GetFieldIndexer ().IndexField (ctx , & corev1.ConfigMap {}, "type" , func (obj client.Object ) []string {
124
+ return []string {obj .GetLabels ()["type" ]}
125
+ })
126
+ Expect (err ).NotTo (HaveOccurred ())
127
+ })
117
128
})
118
129
119
130
By ("Starting the provider, cluster, manager, and controller" , func () {
@@ -129,30 +140,123 @@ var _ = Describe("Provider Namespace", Ordered, func() {
129
140
})
130
141
})
131
142
132
- It ("runs a multi-cluster controller" , func (ctx context.Context ) {
133
- By ("Creating some example namespaces and configmaps" , func () {
134
- runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "zoo" }})))
135
- runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "zoo" , Name : "elephant" , Labels : map [string ]string {"type" : "animal" }}})))
136
- runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "zoo" , Name : "lion" , Labels : map [string ]string {"type" : "animal" }}})))
137
- runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "jungle" }})))
138
- runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "jungle" , Name : "monkey" , Labels : map [string ]string {"type" : "animal" }}})))
139
- runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "island" }})))
140
- runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "island" , Name : "bird" , Labels : map [string ]string {"type" : "animal" }}})))
141
- })
143
+ BeforeAll (func () {
144
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "zoo" }})))
145
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "zoo" , Name : "elephant" , Labels : map [string ]string {"type" : "animal" }}})))
146
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "zoo" , Name : "lion" , Labels : map [string ]string {"type" : "animal" }}})))
147
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "zoo" , Name : "keeper" , Labels : map [string ]string {"type" : "human" }}})))
148
+
149
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "jungle" }})))
150
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "jungle" , Name : "monkey" , Labels : map [string ]string {"type" : "animal" }}})))
151
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "jungle" , Name : "tree" , Labels : map [string ]string {"type" : "thing" }}})))
152
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "jungle" , Name : "tarzan" , Labels : map [string ]string {"type" : "human" }}})))
153
+
154
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "island" }})))
155
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "island" , Name : "bird" , Labels : map [string ]string {"type" : "animal" }}})))
156
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "island" , Name : "stone" , Labels : map [string ]string {"type" : "thing" }}})))
157
+ runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "island" , Name : "crusoe" , Labels : map [string ]string {"type" : "human" }}})))
158
+ })
159
+
160
+ It ("runs the reconciler for existing objects" , func (ctx context.Context ) {
161
+ Eventually (func () string {
162
+ lion := & corev1.ConfigMap {}
163
+ err := cli .Get (ctx , client.ObjectKey {Namespace : "zoo" , Name : "lion" }, lion )
164
+ Expect (err ).NotTo (HaveOccurred ())
165
+ return lion .Data ["stomach" ]
166
+ }, "10s" ).Should (Equal ("food" ))
167
+ })
142
168
169
+ It ("runs the reconciler for new objects" , func (ctx context.Context ) {
143
170
By ("Creating a new configmap" , func () {
144
171
err := cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "zoo" , Name : "tiger" , Labels : map [string ]string {"type" : "animal" }}})
145
172
Expect (err ).NotTo (HaveOccurred ())
146
173
})
147
174
148
175
Eventually (func () string {
149
- cm := & corev1.ConfigMap {}
150
- err := cli .Get (ctx , client.ObjectKey {Namespace : "zoo" , Name : "tiger" }, cm )
176
+ tiger := & corev1.ConfigMap {}
177
+ err := cli .Get (ctx , client.ObjectKey {Namespace : "zoo" , Name : "tiger" }, tiger )
178
+ Expect (err ).NotTo (HaveOccurred ())
179
+ return tiger .Data ["stomach" ]
180
+ }, "10s" ).Should (Equal ("food" ))
181
+ })
182
+
183
+ It ("runs the reconciler for updated objects" , func (ctx context.Context ) {
184
+ updated := & corev1.ConfigMap {}
185
+ By ("Emptying the elephant's stomach" , func () {
186
+ err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
187
+ if err := cli .Get (ctx , client.ObjectKey {Namespace : "zoo" , Name : "elephant" }, updated ); err != nil {
188
+ return err
189
+ }
190
+ updated .Data = map [string ]string {}
191
+ return cli .Update (ctx , updated )
192
+ })
193
+ Expect (err ).NotTo (HaveOccurred ())
194
+ })
195
+ rv , err := strconv .ParseInt (updated .ResourceVersion , 10 , 64 )
196
+ Expect (err ).NotTo (HaveOccurred ())
197
+
198
+ Eventually (func () int64 {
199
+ elephant := & corev1.ConfigMap {}
200
+ err := cli .Get (ctx , client.ObjectKey {Namespace : "zoo" , Name : "elephant" }, elephant )
201
+ Expect (err ).NotTo (HaveOccurred ())
202
+ rv , err := strconv .ParseInt (elephant .ResourceVersion , 10 , 64 )
203
+ Expect (err ).NotTo (HaveOccurred ())
204
+ return rv
205
+ }, "10s" ).Should (BeNumerically (">=" , rv ))
206
+
207
+ Eventually (func () string {
208
+ elephant := & corev1.ConfigMap {}
209
+ err := cli .Get (ctx , client.ObjectKey {Namespace : "zoo" , Name : "elephant" }, elephant )
151
210
Expect (err ).NotTo (HaveOccurred ())
152
- return cm .Data ["stomach" ]
211
+ return elephant .Data ["stomach" ]
153
212
}, "10s" ).Should (Equal ("food" ))
154
213
})
155
214
215
+ It ("queries one cluster via a multi-cluster index" , func () {
216
+ island , err := mgr .GetCluster (ctx , "island" )
217
+ Expect (err ).NotTo (HaveOccurred ())
218
+
219
+ cms := & corev1.ConfigMapList {}
220
+ err = island .GetCache ().List (ctx , cms , client.MatchingFields {"type" : "human" })
221
+ Expect (err ).NotTo (HaveOccurred ())
222
+ Expect (cms .Items ).To (HaveLen (1 ))
223
+ Expect (cms .Items [0 ].Name ).To (Equal ("crusoe" ))
224
+ Expect (cms .Items [0 ].Namespace ).To (Equal ("default" ))
225
+ })
226
+
227
+ It ("queries one cluster via a multi-cluster index with a namespace" , func () {
228
+ island , err := mgr .GetCluster (ctx , "island" )
229
+ Expect (err ).NotTo (HaveOccurred ())
230
+
231
+ cms := & corev1.ConfigMapList {}
232
+ err = island .GetCache ().List (ctx , cms , client .InNamespace ("default" ), client.MatchingFields {"type" : "human" })
233
+ Expect (err ).NotTo (HaveOccurred ())
234
+ Expect (cms .Items ).To (HaveLen (1 ))
235
+ Expect (cms .Items [0 ].Name ).To (Equal ("crusoe" ))
236
+ Expect (cms .Items [0 ].Namespace ).To (Equal ("default" ))
237
+ })
238
+
239
+ It ("queries all clusters via a multi-cluster index" , func () {
240
+ cms := & corev1.ConfigMapList {}
241
+ err := cl .GetCache ().List (ctx , cms , client.MatchingFields {"type" : "human" })
242
+ Expect (err ).NotTo (HaveOccurred ())
243
+ Expect (cms .Items ).To (HaveLen (3 ))
244
+ names := sets .NewString ()
245
+ for _ , cm := range cms .Items {
246
+ names .Insert (cm .Name )
247
+ }
248
+ Expect (names ).To (Equal (sets .NewString ("keeper" , "tarzan" , "crusoe" )))
249
+ })
250
+
251
+ It ("queries all clusters via a multi-cluster index with a namespace" , func () {
252
+ cms := & corev1.ConfigMapList {}
253
+ err := cl .GetCache ().List (ctx , cms , client .InNamespace ("island" ), client.MatchingFields {"type" : "human" })
254
+ Expect (err ).NotTo (HaveOccurred ())
255
+ Expect (cms .Items ).To (HaveLen (1 ))
256
+ Expect (cms .Items [0 ].Name ).To (Equal ("crusoe" ))
257
+ Expect (cms .Items [0 ].Namespace ).To (Equal ("island" ))
258
+ })
259
+
156
260
AfterAll (func () {
157
261
By ("Stopping the provider, cluster, manager, and controller" , func () {
158
262
cancel ()
0 commit comments