/
external-service.go
576 lines (522 loc) · 18.9 KB
/
external-service.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 features
import (
"fmt"
"net/http"
"time"
"github.com/apache/apisix-ingress-controller/pkg/id"
v2 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2"
"github.com/apache/apisix-ingress-controller/pkg/providers/translation"
"github.com/apache/apisix-ingress-controller/pkg/types"
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
"github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/assert"
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
)
var _ = ginkgo.Describe("suite-features: external services", func() {
PhaseCreateExternalService := func(s *scaffold.Scaffold, name, externalName string) {
extService := fmt.Sprintf(`
apiVersion: v1
kind: Service
metadata:
name: %s
spec:
type: ExternalName
externalName: %s
`, name, externalName)
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(extService))
}
PhaseCreateApisixRoute := func(s *scaffold.Scaffold, name, upstream string) {
ar := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: %s
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /*
exprs:
- subject:
scope: Header
name: X-Foo
op: Equal
value: bar
upstreams:
- name: %s
`, name, upstream)
assert.Nil(ginkgo.GinkgoT(), s.CreateVersionedApisixResource(ar))
}
PhaseCreateApisixRouteWithHostRewrite := func(s *scaffold.Scaffold, name, upstream, rewriteHost string) {
ar := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: %s
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /*
exprs:
- subject:
scope: Header
name: X-Foo
op: Equal
value: bar
upstreams:
- name: %s
plugins:
- name: proxy-rewrite
enable: true
config:
host: %s
`, name, upstream, rewriteHost)
assert.Nil(ginkgo.GinkgoT(), s.CreateVersionedApisixResource(ar))
}
PhaseCreateApisixUpstream := func(s *scaffold.Scaffold, name string, nodeType v2.ApisixUpstreamExternalType, nodeName string) {
au := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
name: %s
spec:
externalNodes:
- type: %s
name: %s
`, name, nodeType, nodeName)
assert.Nil(ginkgo.GinkgoT(), s.CreateVersionedApisixResource(au))
}
PhaseValidateNoUpstreams := func(s *scaffold.Scaffold) {
ups, err := s.ListApisixUpstreams()
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), ups, 0, "upstream count")
}
PhaseValidateNoRoutes := func(s *scaffold.Scaffold) {
routes, err := s.ListApisixRoutes()
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), routes, 0, "route count")
}
PhaseValidateFirstUpstream := func(s *scaffold.Scaffold, length int, node string, port, weight int) string {
ups, err := s.ListApisixUpstreams()
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), ups, length, "upstream count")
upstream := ups[0]
assert.Len(ginkgo.GinkgoT(), upstream.Nodes, 1)
assert.Equal(ginkgo.GinkgoT(), node, upstream.Nodes[0].Host)
assert.Equal(ginkgo.GinkgoT(), port, upstream.Nodes[0].Port)
assert.Equal(ginkgo.GinkgoT(), weight, upstream.Nodes[0].Weight)
return upstream.ID
}
PhaseValidateRouteAccess := func(s *scaffold.Scaffold, upstreamId string) {
routes, err := s.ListApisixRoutes()
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), routes, 1, "route count")
assert.Equal(ginkgo.GinkgoT(), upstreamId, routes[0].UpstreamId)
_ = s.NewAPISIXClient().GET("/ip").
WithHeader("Host", "httpbin.org").
WithHeader("X-Foo", "bar").
Expect().
Status(http.StatusOK)
}
// PhaseValidateRouteAccessCode := func(s *scaffold.Scaffold, upstreamId string, code int) {
// routes, err := s.ListApisixRoutes()
// assert.Nil(ginkgo.GinkgoT(), err)
// assert.Len(ginkgo.GinkgoT(), routes, 1, "route count")
// assert.Equal(ginkgo.GinkgoT(), upstreamId, routes[0].UpstreamId)
// _ = s.NewAPISIXClient().GET("/ip").
// WithHeader("Host", "httpbin.org").
// WithHeader("X-Foo", "bar").
// Expect().
// Status(code)
// }
PhaseCreateHttpbin := func(s *scaffold.Scaffold, name string) string {
_httpbinDeploymentTemplate := fmt.Sprintf(`
apiVersion: apps/v1
kind: Deployment
metadata:
name: %s
spec:
replicas: 1
selector:
matchLabels:
app: %s
strategy:
rollingUpdate:
maxSurge: 50%%
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
labels:
app: %s
spec:
terminationGracePeriodSeconds: 0
containers:
- livenessProbe:
failureThreshold: 3
initialDelaySeconds: 2
periodSeconds: 5
successThreshold: 1
tcpSocket:
port: 80
timeoutSeconds: 2
readinessProbe:
failureThreshold: 3
initialDelaySeconds: 2
periodSeconds: 5
successThreshold: 1
tcpSocket:
port: 80
timeoutSeconds: 2
image: "localhost:5000/httpbin:dev"
imagePullPolicy: IfNotPresent
name: httpbin
ports:
- containerPort: 80
name: "http"
protocol: "TCP"
`, name, name, name)
_httpService := fmt.Sprintf(`
apiVersion: v1
kind: Service
metadata:
name: %s
spec:
selector:
app: %s
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
type: ClusterIP
`, name, name)
err := s.CreateResourceFromString(s.FormatRegistry(_httpbinDeploymentTemplate))
assert.Nil(ginkgo.GinkgoT(), err, "create temp httpbin deployment")
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(_httpService), "create temp httpbin service")
return fmt.Sprintf("httpbin-temp.%s.svc.cluster.local", s.Namespace())
}
// Cases:
// --- Basic Function ---
// 1. ApisixRoute refers to ApisixUpstream, ApisixUpstream refers to third-party service
// 2. ApisixRoute refers to ApisixUpstream, ApisixUpstream refers to ExternalName service
// 3. ApisixRoute refers to ApisixUpstream, ApisixUpstream refers to multiple third-party or ExternalName services
// 4. ApisixRoute refers to ApisixUpstream and Backends, ApisixUpstream refers to ExternalName service
// --- Update Cases ---
// o 1. ApisixRoute refers to ApisixUpstream, but the ApisixUpstream is created later
// o 2. ApisixRoute refers to ApisixUpstream, but the ExternalName service is created later
// o 3. ApisixRoute refers to ApisixUpstream, but the ApisixUpstream is updated and change to another ExternalName service
// o 4. ApisixRoute refers to ApisixUpstream, the ApisixUpstream doesn't change, but the ExternalName service itself is updated
// --- Delete Cases ---
// 1. ApisixRoute is deleted, the generated resources should be removed
s := scaffold.NewDefaultV2Scaffold()
ginkgo.Describe("basic function: ", func() {
ginkgo.It("should be able to access third-party service", func() {
// -- Data preparation --
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeDomain, "httpbin.org")
PhaseCreateApisixRoute(s, "httpbin-route", "httpbin-upstream")
time.Sleep(time.Second * 6)
// -- validation --
upstreamId := PhaseValidateFirstUpstream(s, 1, "httpbin.org", 80, translation.DefaultWeight)
PhaseValidateRouteAccess(s, upstreamId)
})
ginkgo.It("should be able to access third-party service with plugins", func() {
// -- Data preparation --
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeDomain, "httpbin.org")
// -- update --
PhaseCreateApisixRouteWithHostRewrite(s, "httpbin-route", "httpbin-upstream", "httpbin.org")
time.Sleep(time.Second * 6)
// -- validation --
upstreamId := PhaseValidateFirstUpstream(s, 1, "httpbin.org", 80, translation.DefaultWeight)
PhaseValidateRouteAccess(s, upstreamId)
})
ginkgo.It("should be able to access external domain ExternalName service", func() {
// -- Data preparation --
PhaseCreateExternalService(s, "ext-httpbin", "httpbin.org")
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeService, "ext-httpbin")
PhaseCreateApisixRoute(s, "httpbin-route", "httpbin-upstream")
time.Sleep(time.Second * 6)
// -- validation --
upstreamId := PhaseValidateFirstUpstream(s, 1, "httpbin.org", 80, translation.DefaultWeight)
PhaseValidateRouteAccess(s, upstreamId)
})
ginkgo.It("should be able to access in-cluster ExternalName service", func() {
// -- Data preparation --
fqdn := PhaseCreateHttpbin(s, "httpbin-temp")
time.Sleep(time.Second * 10)
// We are only testing the functionality of the external service and do not care which namespace the service is in.
// The namespace of the external service should be watched.
PhaseCreateExternalService(s, "ext-httpbin", fqdn)
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeService, "ext-httpbin")
PhaseCreateApisixRoute(s, "httpbin-route", "httpbin-upstream")
time.Sleep(time.Second * 6)
// -- validation --
upstreamId := PhaseValidateFirstUpstream(s, 1, fqdn, 80, translation.DefaultWeight)
PhaseValidateRouteAccess(s, upstreamId)
})
})
ginkgo.Describe("complex usage: ", func() {
PhaseCreateApisixUpstreamWithMultipleExternalNodes := func(s *scaffold.Scaffold, name string,
nodeTypeA v2.ApisixUpstreamExternalType, nodeNameA string, nodeTypeB v2.ApisixUpstreamExternalType, nodeNameB string) {
au := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
name: %s
spec:
externalNodes:
- type: %s
name: %s
- type: %s
name: %s
`, name, nodeTypeA, nodeNameA, nodeTypeB, nodeNameB)
assert.Nil(ginkgo.GinkgoT(), s.CreateVersionedApisixResource(au))
}
PhaseCreateApisixRouteWithHostRewriteAndBackend := func(s *scaffold.Scaffold, name, upstream, hostRewrite, serviceName string, servicePort int) {
ar := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: %s
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /*
exprs:
- subject:
scope: Header
name: X-Foo
op: Equal
value: bar
upstreams:
- name: %s
backends:
- serviceName: %s
servicePort: %d
resolveGranularity: service
plugins:
- name: proxy-rewrite
enable: true
config:
host: %s
`, name, upstream, serviceName, servicePort, hostRewrite)
assert.Nil(ginkgo.GinkgoT(), s.CreateVersionedApisixResource(ar))
}
validateHttpbinAndPostmanAreAccessed := func() {
hasEtag := false // postman-echo.com
hasNoEtag := false // httpbin.org
for i := 0; i < 20; i++ {
headers := s.NewAPISIXClient().GET("/ip").
WithHeader("Host", "httpbin.org").
WithHeader("X-Foo", "bar").
Expect().
Status(http.StatusOK).
Headers().Raw()
if _, ok := headers["Etag"]; ok {
hasEtag = true
} else {
hasNoEtag = true
}
if hasEtag && hasNoEtag {
break
}
}
assert.True(ginkgo.GinkgoT(), hasEtag && hasNoEtag, "both httpbin and postman should be accessed at least once")
}
type validateFactor struct {
port int
weight int
}
// Note: expected nodes has unique host
PhaseValidateMultipleNodes := func(s *scaffold.Scaffold, length int, nodes map[string]*validateFactor) {
ups, err := s.ListApisixUpstreams()
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), ups, 1, "upstream count")
upstream := ups[0]
assert.Len(ginkgo.GinkgoT(), upstream.Nodes, length)
for _, node := range upstream.Nodes {
host := node.Host
if factor, ok := nodes[host]; ok {
assert.Equal(ginkgo.GinkgoT(), factor.port, node.Port)
assert.Equal(ginkgo.GinkgoT(), factor.weight, node.Weight)
} else {
err := fmt.Errorf("host %s appear but it shouldn't", host)
assert.Nil(ginkgo.GinkgoT(), err)
}
}
routes, err := s.ListApisixRoutes()
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), routes, 1, "route count")
assert.Equal(ginkgo.GinkgoT(), ups[0].ID, routes[0].UpstreamId)
validateHttpbinAndPostmanAreAccessed()
}
// Note: expected nodes has unique host
PhaseValidateTrafficSplit := func(s *scaffold.Scaffold, length int, upstreamId string, nodes map[string]*validateFactor) {
ups, err := s.ListApisixUpstreams()
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), ups, length, "upstream count")
for _, upstream := range ups {
assert.Len(ginkgo.GinkgoT(), upstream.Nodes, 1)
host := upstream.Nodes[0].Host
if factor, ok := nodes[host]; ok {
assert.Equal(ginkgo.GinkgoT(), factor.port, upstream.Nodes[0].Port)
assert.Equal(ginkgo.GinkgoT(), factor.weight, upstream.Nodes[0].Weight)
} else {
err := fmt.Errorf("host %s appear but it shouldn't", host)
assert.Nil(ginkgo.GinkgoT(), err)
}
}
routes, err := s.ListApisixRoutes()
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), routes, 1, "route count")
assert.Equal(ginkgo.GinkgoT(), upstreamId, routes[0].UpstreamId)
validateHttpbinAndPostmanAreAccessed()
}
ginkgo.It("should be able to access multiple external services", func() {
// -- Data preparation --
PhaseCreateApisixUpstreamWithMultipleExternalNodes(s, "httpbin-upstream",
v2.ExternalTypeDomain, "httpbin.org", v2.ExternalTypeDomain, "postman-echo.com")
PhaseCreateApisixRouteWithHostRewrite(s, "httpbin-route", "httpbin-upstream", "postman-echo.com")
time.Sleep(time.Second * 6)
// -- validation --
PhaseValidateMultipleNodes(s, 2, map[string]*validateFactor{
"httpbin.org": {
port: 80,
weight: translation.DefaultWeight,
},
"postman-echo.com": {
port: 80,
weight: translation.DefaultWeight,
},
})
})
ginkgo.It("should be able to use backends and upstreams together", func() {
// -- Data preparation --
PhaseCreateHttpbin(s, "httpbin-temp")
time.Sleep(time.Second * 10)
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeDomain, "postman-echo.com")
PhaseCreateApisixRouteWithHostRewriteAndBackend(s, "httpbin-route", "httpbin-upstream", "postman-echo.com", "httpbin-temp", 80)
time.Sleep(time.Second * 6)
svc, err := s.GetServiceByName("httpbin-temp")
assert.Nil(ginkgo.GinkgoT(), err, "get httpbin service")
ip := svc.Spec.ClusterIP
upName := apisixv1.ComposeUpstreamName(s.Namespace(), "httpbin-temp", "", 80, types.ResolveGranularity.Service)
upID := id.GenID(upName)
// -- validation --
PhaseValidateTrafficSplit(s, 2, upID, map[string]*validateFactor{
ip: {
port: 80,
weight: translation.DefaultWeight,
},
"postman-echo.com": {
port: 80,
weight: translation.DefaultWeight,
},
})
})
})
ginkgo.Describe("update function: ", func() {
ginkgo.It("should be able to create the ApisixUpstream later", func() {
// -- Data preparation --
PhaseCreateApisixRoute(s, "httpbin-route", "httpbin-upstream")
time.Sleep(time.Second * 6)
PhaseValidateNoUpstreams(s)
// -- Data Update --
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeDomain, "httpbin.org")
time.Sleep(time.Second * 6)
// -- validation --
upstreamId := PhaseValidateFirstUpstream(s, 1, "httpbin.org", 80, translation.DefaultWeight)
PhaseValidateRouteAccess(s, upstreamId)
})
ginkgo.It("should be able to create the ExternalName service later", func() {
// -- Data preparation --
fqdn := PhaseCreateHttpbin(s, "httpbin-temp")
time.Sleep(time.Second * 10)
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeService, "ext-httpbin")
PhaseCreateApisixRoute(s, "httpbin-route", "httpbin-upstream")
time.Sleep(time.Second * 6)
PhaseValidateNoUpstreams(s)
// -- Data update --
PhaseCreateExternalService(s, "ext-httpbin", fqdn)
time.Sleep(time.Second * 6)
// -- validation --
upstreamId := PhaseValidateFirstUpstream(s, 1, fqdn, 80, translation.DefaultWeight)
PhaseValidateRouteAccess(s, upstreamId)
})
ginkgo.It("should be able to update the ApisixUpstream later", func() {
// -- Data preparation --
fqdn := PhaseCreateHttpbin(s, "httpbin-temp")
time.Sleep(time.Second * 10)
PhaseCreateExternalService(s, "ext-httpbin", fqdn)
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeService, "doesnt-exist")
PhaseCreateApisixRoute(s, "httpbin-route", "httpbin-upstream")
time.Sleep(time.Second * 6)
PhaseValidateNoUpstreams(s)
// -- Data update --
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeService, "ext-httpbin")
time.Sleep(time.Second * 6)
// -- validation --
upstreamId := PhaseValidateFirstUpstream(s, 1, fqdn, 80, translation.DefaultWeight)
PhaseValidateRouteAccess(s, upstreamId)
})
ginkgo.It("should be able to update the ExternalName service later", func() {
// -- Data preparation --
PhaseCreateExternalService(s, "ext-httpbin", "unknown.org")
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeService, "ext-httpbin")
PhaseCreateApisixRoute(s, "httpbin-route", "httpbin-upstream")
time.Sleep(time.Second * 6)
PhaseValidateFirstUpstream(s, 1, "unknown.org", 80, translation.DefaultWeight)
// -- Data update --
PhaseCreateExternalService(s, "ext-httpbin", "httpbin.org")
time.Sleep(time.Second * 6)
// -- validation --
upstreamId := PhaseValidateFirstUpstream(s, 1, "httpbin.org", 80, translation.DefaultWeight)
PhaseValidateRouteAccess(s, upstreamId)
})
})
ginkgo.Describe("delete function: ", func() {
ginkgo.It("should be able to delete resources", func() {
// -- Data preparation --
PhaseCreateApisixUpstream(s, "httpbin-upstream", v2.ExternalTypeDomain, "httpbin.org")
PhaseCreateApisixRoute(s, "httpbin-route", "httpbin-upstream")
time.Sleep(time.Second * 6)
// -- validation --
upstreamId := PhaseValidateFirstUpstream(s, 1, "httpbin.org", 80, translation.DefaultWeight)
PhaseValidateRouteAccess(s, upstreamId)
// -- delete --
assert.Nil(ginkgo.GinkgoT(), s.DeleteResource("ar", "httpbin-route"), "delete route")
assert.Nil(ginkgo.GinkgoT(), s.DeleteResource("au", "httpbin-upstream"), "delete upstream")
time.Sleep(time.Second * 6)
// -- validate --
PhaseValidateNoRoutes(s)
PhaseValidateNoUpstreams(s)
})
})
})