-
Notifications
You must be signed in to change notification settings - Fork 66
/
kubernetes_test.go
381 lines (353 loc) · 9.36 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
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
package gateway
import (
"context"
"testing"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
nginx "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
k8s_nginx "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned"
k8s_nginx_fake "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned/fake"
core "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
gatewayapi_v1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapi_v1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayClient "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"
gwFake "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/fake"
)
func TestController(t *testing.T) {
client := fake.NewSimpleClientset()
gwClient := gwFake.NewSimpleClientset()
nginxClient := k8s_nginx_fake.NewSimpleClientset()
ctrl := &KubeController{
client: client,
gwClient: gwClient,
nginxClient: nginxClient,
hasSynced: true,
}
addServices(client)
addIngresses(client)
addGateways(gwClient)
addHTTPRoutes(gwClient)
addTLSRoutes(gwClient)
addGRPCRoutes(gwClient)
addVirtualServers(nginxClient)
gw := newGateway()
gw.Zones = []string{"example.com."}
gw.Next = test.NextHandler(dns.RcodeSuccess, nil)
gw.Controller = ctrl
for index, testObj := range testIngresses {
found, _ := ingressHostnameIndexFunc(testObj)
if !isFound(index, found) {
t.Errorf("Ingress key %s not found in index: %v", index, found)
}
ips := fetchIngressLoadBalancerIPs(testObj.Status.LoadBalancer.Ingress)
if len(ips) != 1 {
t.Errorf("Unexpected number of IPs found %d", len(ips))
}
}
for index, testObj := range testServices {
found, _ := serviceHostnameIndexFunc(testObj)
if !isFound(index, found) {
t.Errorf("Service key %s not found in index: %v", index, found)
}
ips := fetchServiceLoadBalancerIPs(testObj.Status.LoadBalancer.Ingress)
if len(ips) != 1 {
t.Errorf("Unexpected number of IPs found %d", len(ips))
}
}
for index, testObj := range testVirtualServers {
found, _ := virtualServerHostnameIndexFunc(testObj)
if !isFound(index, found) {
t.Errorf("VirtualServer ksy %s not found in index: %v", index, found)
}
}
for index, testObj := range testBadServices {
found, _ := serviceHostnameIndexFunc(testObj)
if isFound(index, found) {
t.Errorf("Unexpected service key %s found in index: %v", index, found)
}
}
for index, testObj := range testHTTPRoutes {
found, _ := httpRouteHostnameIndexFunc(testObj)
if !isFound(index, found) {
t.Errorf("HTTPRoute key %s not found in index: %v", index, found)
}
}
for index, testObj := range testTLSRoutes {
found, _ := tlsRouteHostnameIndexFunc(testObj)
if !isFound(index, found) {
t.Errorf("TLSRoute key %s not found in index: %v", index, found)
}
}
for index, testObj := range testGRPCRoutes {
found, _ := grpcRouteHostnameIndexFunc(testObj)
if !isFound(index, found) {
t.Errorf("GRPC key %s not found in index: %v", index, found)
}
}
for index, testObj := range testGateways {
found, _ := gatewayIndexFunc(testObj)
if !isFound(index, found) {
t.Errorf("Gateway key %s not found in index: %v", index, found)
}
}
}
func isFound(s string, ss []string) bool {
for _, str := range ss {
if str == s {
return true
}
}
return false
}
func addServices(client kubernetes.Interface) {
ctx := context.TODO()
for _, svc := range testServices {
_, err := client.CoreV1().Services("ns1").Create(ctx, svc, meta.CreateOptions{})
if err != nil {
log.Warningf("Failed to Create Service Objects :%s", err)
}
}
}
func addIngresses(client kubernetes.Interface) {
ctx := context.TODO()
for _, ingress := range testIngresses {
_, err := client.NetworkingV1().Ingresses("ns1").Create(ctx, ingress, meta.CreateOptions{})
if err != nil {
log.Warningf("Failed to Create Ingress Objects :%s", err)
}
}
}
func addVirtualServers(client k8s_nginx.Interface) {
ctx := context.TODO()
for _, virtualServer := range testVirtualServers {
_, err := client.K8sV1().VirtualServers("ns1").Create(ctx, virtualServer, meta.CreateOptions{})
if err != nil {
log.Warningf("Failed to Create VirtualServer Objects :%s", err)
}
}
}
func addGateways(client gatewayClient.Interface) {
ctx := context.TODO()
for _, gw := range testGateways {
_, err := client.GatewayV1().Gateways("ns1").Create(ctx, gw, meta.CreateOptions{})
if err != nil {
log.Warningf("Failed to Create a Gateway Object :%s", err)
}
}
}
func addHTTPRoutes(client gatewayClient.Interface) {
ctx := context.TODO()
for _, r := range testHTTPRoutes {
_, err := client.GatewayV1().HTTPRoutes("ns1").Create(ctx, r, meta.CreateOptions{})
if err != nil {
log.Warningf("Failed to Create a HTTPRoute Object :%s", err)
}
}
}
func addTLSRoutes(client gatewayClient.Interface) {
ctx := context.TODO()
for _, r := range testTLSRoutes {
_, err := client.GatewayV1alpha2().TLSRoutes("ns1").Create(ctx, r, meta.CreateOptions{})
if err != nil {
log.Warningf("Failed to Create a TLSRoutes Object :%s", err)
}
}
}
func addGRPCRoutes(client gatewayClient.Interface) {
ctx := context.TODO()
for _, r := range testGRPCRoutes {
_, err := client.GatewayV1alpha2().GRPCRoutes("ns1").Create(ctx, r, meta.CreateOptions{})
if err != nil {
log.Warningf("Failed to Create a GRPC Object :%s", err)
}
}
}
var testIngresses = map[string]*networking.Ingress{
"a.example.org": {
ObjectMeta: meta.ObjectMeta{
Name: "ing1",
Namespace: "ns1",
},
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: "a.example.org",
},
},
},
Status: networking.IngressStatus{
LoadBalancer: networking.IngressLoadBalancerStatus{
Ingress: []networking.IngressLoadBalancerIngress{
{IP: "192.0.0.1"},
},
},
},
},
"example.org": {
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: "example.org",
},
},
},
Status: networking.IngressStatus{
LoadBalancer: networking.IngressLoadBalancerStatus{
Ingress: []networking.IngressLoadBalancerIngress{
{IP: "192.0.0.2"},
},
},
},
},
}
var testServices = map[string]*core.Service{
"svc1.ns1": {
ObjectMeta: meta.ObjectMeta{
Name: "svc1",
Namespace: "ns1",
},
Spec: core.ServiceSpec{
Type: core.ServiceTypeLoadBalancer,
},
Status: core.ServiceStatus{
LoadBalancer: core.LoadBalancerStatus{
Ingress: []core.LoadBalancerIngress{
{IP: "192.0.0.1"},
},
},
},
},
"svc2.ns1": {
ObjectMeta: meta.ObjectMeta{
Name: "svc2",
Namespace: "ns1",
},
Spec: core.ServiceSpec{
Type: core.ServiceTypeLoadBalancer,
},
Status: core.ServiceStatus{
LoadBalancer: core.LoadBalancerStatus{
Ingress: []core.LoadBalancerIngress{
{IP: "192.0.0.2"},
},
},
},
},
"annotation": {
ObjectMeta: meta.ObjectMeta{
Name: "svc3",
Namespace: "ns1",
Annotations: map[string]string{
"coredns.io/hostname": "annotation",
},
},
Spec: core.ServiceSpec{
Type: core.ServiceTypeLoadBalancer,
},
Status: core.ServiceStatus{
LoadBalancer: core.LoadBalancerStatus{
Ingress: []core.LoadBalancerIngress{
{IP: "192.0.0.3"},
},
},
},
},
}
var testVirtualServers = map[string]*nginx.VirtualServer{
"vs1.example.org": {
ObjectMeta: meta.ObjectMeta{
Name: "vs1",
Namespace: "ns1",
},
Spec: nginx.VirtualServerSpec{
Host: "vs1.example.org",
},
Status: nginx.VirtualServerStatus{
ExternalEndpoints: []nginx.ExternalEndpoint{
{IP: "192.0.0.1"},
},
},
},
}
var testGateways = map[string]*gatewayapi_v1.Gateway{
"ns1/gw-1": {
ObjectMeta: meta.ObjectMeta{
Name: "gw-1",
Namespace: "ns1",
},
Spec: gatewayapi_v1.GatewaySpec{},
Status: gatewayapi_v1.GatewayStatus{
Addresses: []gatewayapi_v1.GatewayStatusAddress{
{
Value: "192.0.2.100",
},
},
},
},
"ns1/gw-2": {
ObjectMeta: meta.ObjectMeta{
Name: "gw-2",
Namespace: "ns1",
},
},
}
var testHTTPRoutes = map[string]*gatewayapi_v1.HTTPRoute{
"route-1.gw-1.example.com": {
ObjectMeta: meta.ObjectMeta{
Name: "route-1",
Namespace: "ns1",
},
Spec: gatewayapi_v1.HTTPRouteSpec{
//ParentRefs: []gatewayapi_v1.ParentRef{},
Hostnames: []gatewayapi_v1.Hostname{"route-1.gw-1.example.com"},
},
},
}
var testTLSRoutes = map[string]*gatewayapi_v1alpha2.TLSRoute{
"route-1.gw-1.example.com": {
ObjectMeta: meta.ObjectMeta{
Name: "route-1",
Namespace: "ns1",
},
Spec: gatewayapi_v1alpha2.TLSRouteSpec{
//ParentRefs: []gatewayapi_v1.ParentRef{},
Hostnames: []gatewayapi_v1alpha2.Hostname{
"route-1.gw-1.example.com",
},
},
},
}
var testGRPCRoutes = map[string]*gatewayapi_v1alpha2.GRPCRoute{
"route-1.gw-1.example.com": {
ObjectMeta: meta.ObjectMeta{
Name: "route-1",
Namespace: "ns1",
},
Spec: gatewayapi_v1alpha2.GRPCRouteSpec{
//ParentRefs: []gatewayapi_v1.ParentRef{},
Hostnames: []gatewayapi_v1alpha2.Hostname{"route-1.gw-1.example.com"},
},
},
}
var testBadServices = map[string]*core.Service{
"svc1.ns2": {
ObjectMeta: meta.ObjectMeta{
Name: "svc1",
Namespace: "ns2",
},
Spec: core.ServiceSpec{
Type: core.ServiceTypeClusterIP,
},
Status: core.ServiceStatus{
LoadBalancer: core.LoadBalancerStatus{
Ingress: []core.LoadBalancerIngress{
{IP: "192.0.0.1"},
},
},
},
},
}