-
Notifications
You must be signed in to change notification settings - Fork 5
/
kubernetes_test.go
284 lines (246 loc) · 7.07 KB
/
kubernetes_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
/*
Copyright 2017 Tuenti Technologies S.L. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/pkg/api/v1"
)
type testWatcher struct {
resultChan chan watch.Event
}
func newTestWatcher() *testWatcher {
return &testWatcher{make(chan watch.Event)}
}
func (*testWatcher) Stop() {}
func (w *testWatcher) ResultChan() <-chan watch.Event {
return w.resultChan
}
type testNotifier struct {
waitChan chan struct{}
}
func newTestNotifier() *testNotifier {
return &testNotifier{make(chan struct{}, 10)}
}
func (n *testNotifier) Notify(context.Context) error {
n.waitChan <- struct{}{}
return nil
}
func (n *testNotifier) Wait() error {
select {
case <-n.waitChan:
return nil
case <-time.After(2 * time.Second):
return fmt.Errorf("notify timeout")
}
}
type dummyTemplate struct {
executionCount int
lastExecutedWith *ClusterInformation
}
func (t *dummyTemplate) Execute(info *ClusterInformation) error {
t.executionCount++
t.lastExecutedWith = info
return nil
}
// An updater that doesn't call the updater function but register
// if it has been signaled
type dummyUpdater struct {
Signaled bool
F UpdaterFunc
}
func (*dummyUpdater) Run(ctx context.Context) {
<-ctx.Done()
}
func (u *dummyUpdater) Signal() {
u.Signaled = true
}
func (u *dummyUpdater) Build(f UpdaterFunc) Updater {
u.F = f
return u
}
func TestKubernetesWatch(t *testing.T) {
nodeWatcher := newTestWatcher()
serviceWatcher := newTestWatcher()
endpointsWatcher := newTestWatcher()
updater := dummyUpdater{}
eventForwarderChan := make(chan struct{}, 100)
consumeForwardedEvent := func() {
select {
case <-eventForwarderChan:
case <-time.After(10 * time.Millisecond):
t.Fatal("event consumption timeout")
}
}
// Setup client
client := &KubernetesClient{
nodeWatcher: nodeWatcher,
serviceWatcher: serviceWatcher,
endpointsWatcher: endpointsWatcher,
domain: "kube2lb.test",
updaterBuilder: updater.Build,
eventForwarder: func(watch.Event) {
eventForwarderChan <- struct{}{}
},
}
notifier := newTestNotifier()
client.AddNotifier(notifier)
template := &dummyTemplate{}
client.AddTemplate(template)
ctx := context.Background()
go client.Watch(ctx)
// Send events to watchers
nodeEvents := []watch.Event{
watch.Event{
Type: watch.Added,
Object: &v1.Node{
ObjectMeta: meta_v1.ObjectMeta{SelfLink: "/node/1", Name: "node1", UID: "1"},
},
},
watch.Event{
Type: watch.Added,
Object: &v1.Node{
ObjectMeta: meta_v1.ObjectMeta{SelfLink: "/node/2", Name: "node2", UID: "2"},
},
},
watch.Event{
Type: watch.Deleted,
Object: &v1.Node{
ObjectMeta: meta_v1.ObjectMeta{SelfLink: "/node/1", Name: "node1", UID: "1"},
},
},
}
serviceEvents := []watch.Event{
watch.Event{
Type: watch.Added,
Object: &v1.Service{
ObjectMeta: meta_v1.ObjectMeta{SelfLink: "/service/1", Name: "service1", Namespace: "test", ResourceVersion: "3"},
Spec: v1.ServiceSpec{
Type: v1.ServiceTypeNodePort,
Ports: []v1.ServicePort{
{
Name: "http", Port: 80, TargetPort: intstr.FromInt(80),
},
},
},
},
},
watch.Event{
Type: watch.Added,
Object: &v1.Service{
ObjectMeta: meta_v1.ObjectMeta{SelfLink: "/service/2", Name: "service2", Namespace: "test", ResourceVersion: "4"},
Spec: v1.ServiceSpec{Type: v1.ServiceTypeClusterIP},
},
},
}
endpointsEvents := []watch.Event{
watch.Event{
Type: watch.Added,
Object: &v1.Endpoints{
ObjectMeta: meta_v1.ObjectMeta{SelfLink: "/endpoints/1", Name: "service1", Namespace: "test", ResourceVersion: "5"},
Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{{IP: "10.0.0.1"}, {IP: "10.0.0.2"}},
Ports: []v1.EndpointPort{{Name: "http", Port: 80}},
},
},
},
},
watch.Event{
Type: watch.Modified,
Object: &v1.Endpoints{
ObjectMeta: meta_v1.ObjectMeta{SelfLink: "/endpoints/1", Name: "service1", Namespace: "test", ResourceVersion: "6"},
Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{{IP: "10.0.0.1"}},
Ports: []v1.EndpointPort{{Name: "http", Port: 80}},
},
},
},
},
}
eventCount := 0
for _, event := range nodeEvents {
nodeWatcher.resultChan <- event
eventCount++
}
for _, event := range serviceEvents {
serviceWatcher.resultChan <- event
eventCount++
}
for _, event := range endpointsEvents {
endpointsWatcher.resultChan <- event
eventCount++
}
for i := 0; i < eventCount; i++ {
consumeForwardedEvent()
}
// Check effects
assert.Equal(t, updater.Signaled, true, "Updater should have been signaled")
updater.F(ctx)
if template.executionCount == 0 {
t.Fatal("template not executed")
}
info := template.lastExecutedWith
if assert.NotNil(t, info, "template executed without cluster information?") {
if assert.Equal(t, len(info.Nodes), 1, "expected number of nodes") {
assert.Equal(t, info.Nodes[0], "node2", "expected name of first node")
}
if assert.Equal(t, len(info.Services), 1, "expected number of services") {
if assert.Equal(t, len(info.Services[0].Endpoints), 1, "expected number of endpoints on first service") {
assert.Equal(t, info.Services[0].Endpoints[0].IP, "10.0.0.1", "endpoint for first service")
}
}
}
// Send an event that wouldn't modify the state and check that updater is not signaled
updater.Signaled = false
nodeWatcher.resultChan <- watch.Event{
Type: watch.Modified,
Object: &v1.Node{
ObjectMeta: meta_v1.ObjectMeta{SelfLink: "/node/2", Name: "node2", UID: "2"},
},
}
consumeForwardedEvent()
assert.Equal(t, updater.Signaled, false, "Updater shouldn't have been signaled on repeated modification")
// Adding an annotation to a service should signal updater
updater.Signaled = false
serviceWatcher.resultChan <- watch.Event{
Type: watch.Modified,
Object: &v1.Service{
ObjectMeta: meta_v1.ObjectMeta{
SelfLink: "/service/1",
Name: "service1",
Namespace: "test",
ResourceVersion: "7",
Annotations: map[string]string{ExternalDomainsAnnotation: "service1.example.com"},
},
Spec: v1.ServiceSpec{
Type: v1.ServiceTypeNodePort,
Ports: []v1.ServicePort{
{
Name: "http", Port: 80, TargetPort: intstr.FromInt(80),
},
},
},
},
}
consumeForwardedEvent()
assert.Equal(t, updater.Signaled, true, "Updater should have been signaled when adding annotation to service")
updater.F(ctx)
}