-
Notifications
You must be signed in to change notification settings - Fork 10
/
informer_test.go
414 lines (334 loc) · 11.9 KB
/
informer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package informers_test
// These are largely adapted from the upstream tests for shared informers, with
// a few changes to help test multiple namespaces.
import (
"context"
"fmt"
"strings"
"sync"
"testing"
"time"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
kubefake "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
fcache "k8s.io/client-go/tools/cache/testing"
xnsinformers "github.com/maistra/xns-informer/pkg/informers"
internaltesting "github.com/maistra/xns-informer/pkg/internal/testing"
)
type testListener struct {
lock sync.RWMutex
resyncPeriod time.Duration
expectedItemNames sets.String
receivedItemNames []string
name string
}
func newTestListener(name string, resyncPeriod time.Duration, expected ...string) *testListener {
l := &testListener{
resyncPeriod: resyncPeriod,
expectedItemNames: sets.NewString(expected...),
name: name,
}
return l
}
func (l *testListener) OnAdd(obj interface{}) {
l.handle(obj)
}
func (l *testListener) OnUpdate(old, new interface{}) {
l.handle(new)
}
func (l *testListener) OnDelete(obj interface{}) {
}
func (l *testListener) handle(obj interface{}) {
key, _ := cache.MetaNamespaceKeyFunc(obj)
fmt.Printf("%s: handle: %v\n", l.name, key)
l.lock.Lock()
defer l.lock.Unlock()
objectMeta, _ := meta.Accessor(obj)
l.receivedItemNames = append(l.receivedItemNames, objectMeta.GetName())
}
func (l *testListener) ok() bool {
fmt.Println("polling")
err := wait.PollImmediate(100*time.Millisecond, 2*time.Second, func() (bool, error) {
if l.satisfiedExpectations() {
return true, nil
}
return false, nil
})
if err != nil {
return false
}
// wait just a bit to allow any unexpected stragglers to come in
fmt.Println("sleeping")
time.Sleep(1 * time.Second)
fmt.Println("final check")
return l.satisfiedExpectations()
}
func (l *testListener) satisfiedExpectations() bool {
l.lock.RLock()
defer l.lock.RUnlock()
return sets.NewString(l.receivedItemNames...).Equal(l.expectedItemNames)
}
// create a new NewMultiNamespaceInformer with the given example object and map
// of namespaces to ListWatchers, which are usually a FakeControllerSource here.
func newInformer(obj runtime.Object, lws map[string]cache.ListerWatcher) xnsinformers.MultiNamespaceInformer {
resync := 1 * time.Second
namespaceSet := xnsinformers.NewNamespaceSet()
indexers := cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}
var namespaces []string
for ns := range lws {
namespaces = append(namespaces, ns)
}
namespaceSet.SetNamespaces(namespaces)
return xnsinformers.NewMultiNamespaceInformer(namespaceSet, resync, func(ns string) cache.SharedIndexInformer {
return cache.NewSharedIndexInformer(lws[ns], obj, resync, indexers)
})
}
// verify that https://github.com/kubernetes/kubernetes/issues/59822 is fixed
func TestSharedInformerInitializationRace(t *testing.T) {
source := fcache.NewFakeControllerSource()
informer := newInformer(&v1.Pod{}, map[string]cache.ListerWatcher{
"ns1": source,
})
listener := newTestListener("raceListener", 0)
stop := make(chan struct{})
go informer.AddEventHandlerWithResyncPeriod(listener, listener.resyncPeriod)
go informer.Run(stop)
close(stop)
}
// TestSharedInformerWatchDisruption simulates a watch that was closed
// with updates to the store during that time. We ensure that handlers with
// resync and no resync see the expected state.
func TestSharedInformerWatchDisruption(t *testing.T) {
// source simulates an apiserver object endpoint.
source := fcache.NewFakeControllerSource()
source.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: "pod1", ResourceVersion: "1"}})
source.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod2", UID: "pod2", ResourceVersion: "2"}})
// create the shared informer and resync every 1s
informer := newInformer(&v1.Pod{}, map[string]cache.ListerWatcher{
"ns1": source,
})
// listener, never resync
listenerNoResync := newTestListener("listenerNoResync", 0, "pod1", "pod2")
informer.AddEventHandlerWithResyncPeriod(listenerNoResync, listenerNoResync.resyncPeriod)
listenerResync := newTestListener("listenerResync", 1*time.Second, "pod1", "pod2")
informer.AddEventHandlerWithResyncPeriod(listenerResync, listenerResync.resyncPeriod)
listeners := []*testListener{listenerNoResync, listenerResync}
stop := make(chan struct{})
defer close(stop)
go informer.Run(stop)
for _, listener := range listeners {
if !listener.ok() {
t.Errorf("%s: expected %v, got %v", listener.name, listener.expectedItemNames, listener.receivedItemNames)
}
}
// Add pod3, bump pod2 but don't broadcast it, so that the change will be seen only on relist
source.AddDropWatch(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod3", UID: "pod3", ResourceVersion: "3"}})
source.ModifyDropWatch(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod2", UID: "pod2", ResourceVersion: "4"}})
// Ensure that nobody saw any changes
for _, listener := range listeners {
if !listener.ok() {
t.Errorf("%s: expected %v, got %v", listener.name, listener.expectedItemNames, listener.receivedItemNames)
}
}
for _, listener := range listeners {
listener.lock.Lock()
listener.receivedItemNames = []string{}
listener.lock.Unlock()
}
listenerNoResync.expectedItemNames = sets.NewString("pod2", "pod3")
listenerResync.expectedItemNames = sets.NewString("pod1", "pod2", "pod3")
// This calls shouldSync, which deletes noResync from the list of syncingListeners
time.Sleep(1 * time.Second)
// Simulate a connection loss (or even just a too-old-watch)
source.ResetWatch()
for _, listener := range listeners {
if !listener.ok() {
t.Errorf("%s: expected %v, got %v", listener.name, listener.expectedItemNames, listener.receivedItemNames)
}
}
}
func TestSharedInformerErrorHandling(t *testing.T) {
source1 := fcache.NewFakeControllerSource()
source1.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}})
source1.ListError = fmt.Errorf("Access Denied")
source2 := fcache.NewFakeControllerSource()
source2.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}})
informer := newInformer(&v1.Pod{}, map[string]cache.ListerWatcher{
"ns1": source1,
"ns2": source2,
})
// ns1 source should throw an error.
errCh := make(chan error)
_ = informer.SetWatchErrorHandler(func(_ *cache.Reflector, err error) {
errCh <- err
})
// ns2 source should succeed.
ns2Listener := newTestListener("ns2Listener", 0, "pod2")
informer.AddEventHandlerWithResyncPeriod(ns2Listener, ns2Listener.resyncPeriod)
stop := make(chan struct{})
go informer.Run(stop)
select {
case err := <-errCh:
if !strings.Contains(err.Error(), "Access Denied") {
t.Errorf("Expected 'Access Denied' error. Actual: %v", err)
}
case <-time.After(time.Second):
t.Errorf("Timeout waiting for error handler call")
}
close(stop)
if !ns2Listener.ok() {
t.Errorf("%s: expected %v, got %v",
ns2Listener.name,
ns2Listener.expectedItemNames,
ns2Listener.receivedItemNames,
)
}
}
func TestMultiNamespaceInformerEventHandlers(t *testing.T) {
var err error
ctx := context.TODO()
stopCh := make(chan struct{})
namespaces := []string{"ns1", "ns2"}
cm1 := internaltesting.NewConfigMap(namespaces[0], "cm1", nil)
cm2 := internaltesting.NewConfigMap(namespaces[1], "cm2", nil)
lock := sync.RWMutex{}
addFuncCalled := false
updateFuncCalled := false
deleteFuncCalled := false
// These tests use the fake client instead of a FakeControllerSource.
client := kubefake.NewSimpleClientset()
namespaceSet := xnsinformers.NewNamespaceSet(namespaces...)
informer := xnsinformers.NewMultiNamespaceInformer(namespaceSet, 0, func(namespace string) cache.SharedIndexInformer {
return cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return client.CoreV1().ConfigMaps(namespace).List(ctx, options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return client.CoreV1().ConfigMaps(namespace).Watch(ctx, options)
},
},
&corev1.ConfigMap{},
0,
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
)
})
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(_ interface{}) {
lock.Lock()
addFuncCalled = true
lock.Unlock()
},
UpdateFunc: func(_, _ interface{}) {
lock.Lock()
updateFuncCalled = true
lock.Unlock()
},
DeleteFunc: func(_ interface{}) {
lock.Lock()
deleteFuncCalled = true
lock.Unlock()
},
})
go informer.Run(stopCh)
cache.WaitForCacheSync(stopCh, informer.HasSynced)
// Create the ConfigMap in the first namespace.
_, err = client.CoreV1().ConfigMaps(namespaces[0]).Create(ctx, cm1, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Failed to create ConfigMap: %v", err)
}
// Create the ConfigMap in the second namespace.
_, err = client.CoreV1().ConfigMaps(namespaces[1]).Create(ctx, cm2, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Failed to create ConfigMap: %v", err)
}
// Update the ConfigMap in the first namespace.
_, err = client.CoreV1().ConfigMaps(namespaces[0]).Update(ctx, cm1, metav1.UpdateOptions{})
if err != nil {
t.Fatalf("Failed to update ConfigMap: %v", err)
}
// Delete the ConfigMap in the second namespace.
err = client.CoreV1().ConfigMaps(namespaces[1]).Delete(ctx, "cm2", metav1.DeleteOptions{})
if err != nil {
t.Fatalf("Failed to delete ConfigMap: %v", err)
}
// Wait for all handler functions to be called.
err = wait.PollImmediate(100*time.Millisecond, 1*time.Minute, func() (bool, error) {
lock.RLock()
defer lock.RUnlock()
return addFuncCalled && updateFuncCalled && deleteFuncCalled, nil
})
if err != nil {
t.Fatalf("Handlers not called: %v", err)
}
// Remove first namespace from the set.
deleteFuncCalled = false
informer.RemoveNamespace(namespaces[0])
// Wait for delete handler function to be called again.
err = wait.PollImmediate(100*time.Millisecond, 1*time.Minute, func() (bool, error) {
return deleteFuncCalled, nil
})
if err != nil {
t.Fatalf("Delete handler not called after namespace removal: %v", err)
}
}
func TestMultiNamespaceInformerHasSynced(t *testing.T) {
namespaceSet := xnsinformers.NewNamespaceSet()
hasSynced := false
informer := xnsinformers.NewMultiNamespaceInformer(namespaceSet, 0, func(namespace string) cache.SharedIndexInformer {
return mockInformer{
hasSynced: &hasSynced,
}
})
if informer.HasSynced() {
t.Fatalf("informer is synced, but shouldn't be because namespaces haven't been set yet")
}
namespaceSet.SetNamespaces([]string{"ns1", "ns2"})
if informer.HasSynced() {
t.Fatalf("informer is synced, but shouldn't be because the underlying informers aren't synced")
}
hasSynced = true
if !informer.HasSynced() {
t.Fatalf("expected informer to be synced")
}
}
type mockInformer struct {
hasSynced *bool
}
func (m mockInformer) AddEventHandler(handler cache.ResourceEventHandler) {
panic("not implemented")
}
func (m mockInformer) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) {
panic("not implemented")
}
func (m mockInformer) GetStore() cache.Store {
panic("not implemented")
}
func (m mockInformer) GetController() cache.Controller {
panic("not implemented")
}
func (m mockInformer) Run(stopCh <-chan struct{}) {
panic("not implemented")
}
func (m mockInformer) HasSynced() bool {
return *m.hasSynced
}
func (m mockInformer) LastSyncResourceVersion() string {
panic("not implemented")
}
func (m mockInformer) SetWatchErrorHandler(handler cache.WatchErrorHandler) error {
panic("not implemented")
}
func (m mockInformer) AddIndexers(indexers cache.Indexers) error {
panic("not implemented")
}
func (m mockInformer) GetIndexer() cache.Indexer {
panic("not implemented")
}