Skip to content

Commit d7fbe44

Browse files
authored
test: add multi gateway case (#98)
Signed-off-by: ashing <[email protected]>
1 parent 92c1519 commit d7fbe44

File tree

5 files changed

+399
-71
lines changed

5 files changed

+399
-71
lines changed

config/samples/config.yaml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
log_level: "debug" # The log level of the API7 Ingress Controller.
1+
log_level: "info" # The log level of the API7 Ingress Controller.
22
# the default value is "info".
33

44
controller_name: gateway.api7.io/api7-ingress-controller # The controller name of the API7 Ingress Controller,
@@ -15,16 +15,3 @@ leader_election:
1515
retry_period: 2s # retry_period is the time in seconds that the acting controller
1616
# will wait between tries of actions with the controller.
1717
disable: false # Whether to disable leader election.
18-
19-
# ingress_class: api7 # The ingress class name of the API7 Ingress Controller.
20-
# ingress_publish_service: "" # The service name of the ingress publish service.
21-
# ingress_status_address: [] # The status address of the ingress.
22-
# gateway_configs: # The configuration of the API7 Gateway.
23-
# - name: api7 # The name of the Gateway in the Gateway API.
24-
# control_plane:
25-
# admin_key: "${ADMIN_KEY}" # The admin key of the control plane.
26-
# endpoints:
27-
# - ${ENDPOINT} # The endpoint of the control plane.
28-
# tls_verify: false
29-
# addresses: # record the status address of the gateway-api gateway
30-
# - "172.18.0.4" # The LB IP of the gateway service.

internal/controller/config/config.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,6 @@ func (c *Config) Validate() error {
8989
return nil
9090
}
9191

92-
//nolint:unused
93-
func (c *Config) validateGatewayConfig(gc *GatewayConfig) error {
94-
95-
if gc.Name == "" {
96-
return fmt.Errorf("control_planesp[].gateway_name is required")
97-
}
98-
if gc.ControlPlane.AdminKey == "" {
99-
return fmt.Errorf("control_planes[].admin_api.admin_key is required")
100-
}
101-
if len(gc.ControlPlane.Endpoints) == 0 {
102-
return fmt.Errorf("control_planes[].admin_api.endpoints is required")
103-
}
104-
if gc.ControlPlane.TLSVerify == nil {
105-
gc.ControlPlane.TLSVerify = new(bool)
106-
*gc.ControlPlane.TLSVerify = true
107-
}
108-
109-
return nil
110-
}
111-
11292
func GetControllerName() string {
11393
return ControllerConfig.ControllerName
11494
}

test/e2e/gatewayapi/httproute.go

Lines changed: 158 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ spec:
3333
value: "%s"
3434
`
3535

36-
var defautlGatewayClass = `
36+
var gatewayClassYaml = `
3737
apiVersion: gateway.networking.k8s.io/v1
3838
kind: GatewayClass
3939
metadata:
@@ -42,7 +42,7 @@ spec:
4242
controllerName: %s
4343
`
4444

45-
var defautlGateway = `
45+
var defaultGateway = `
4646
apiVersion: gateway.networking.k8s.io/v1
4747
kind: Gateway
4848
metadata:
@@ -59,7 +59,7 @@ spec:
5959
kind: GatewayProxy
6060
name: api7-proxy-config
6161
`
62-
var defautlGatewayHTTPS = `
62+
var defaultGatewayHTTPS = `
6363
apiVersion: gateway.networking.k8s.io/v1
6464
kind: Gateway
6565
metadata:
@@ -111,7 +111,7 @@ spec:
111111

112112
By("create GatewayClass")
113113
gatewayClassName := fmt.Sprintf("api7-%d", time.Now().Unix())
114-
err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGatewayClass, gatewayClassName, s.GetControllerName()), "")
114+
err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(gatewayClassYaml, gatewayClassName, s.GetControllerName()), "")
115115
Expect(err).NotTo(HaveOccurred(), "creating GatewayClass")
116116
time.Sleep(5 * time.Second)
117117

@@ -122,7 +122,7 @@ spec:
122122
Expect(gcyaml).To(ContainSubstring("message: the gatewayclass has been accepted by the api7-ingress-controller"), "checking GatewayClass condition message")
123123

124124
By("create Gateway")
125-
err = s.CreateResourceFromString(fmt.Sprintf(defautlGateway, gatewayClassName))
125+
err = s.CreateResourceFromString(fmt.Sprintf(defaultGateway, gatewayClassName))
126126
Expect(err).NotTo(HaveOccurred(), "creating Gateway")
127127
time.Sleep(5 * time.Second)
128128

@@ -144,7 +144,7 @@ spec:
144144
createSecret(s, secretName)
145145
By("create GatewayClass")
146146
gatewayClassName := fmt.Sprintf("api7-%d", time.Now().Unix())
147-
err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGatewayClass, gatewayClassName, s.GetControllerName()), "")
147+
err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(gatewayClassYaml, gatewayClassName, s.GetControllerName()), "")
148148
Expect(err).NotTo(HaveOccurred(), "creating GatewayClass")
149149
time.Sleep(5 * time.Second)
150150

@@ -155,7 +155,7 @@ spec:
155155
Expect(gcyaml).To(ContainSubstring("message: the gatewayclass has been accepted by the api7-ingress-controller"), "checking GatewayClass condition message")
156156

157157
By("create Gateway")
158-
err = s.CreateResourceFromString(fmt.Sprintf(defautlGatewayHTTPS, gatewayClassName))
158+
err = s.CreateResourceFromString(fmt.Sprintf(defaultGatewayHTTPS, gatewayClassName))
159159
Expect(err).NotTo(HaveOccurred(), "creating Gateway")
160160
time.Sleep(5 * time.Second)
161161

@@ -211,6 +211,157 @@ spec:
211211
})
212212
})
213213

214+
Context("HTTPRoute with Multiple Gateway", func() {
215+
var additionalGatewayGroupID string
216+
var additionalNamespace string
217+
var additionalGatewayClassName string
218+
219+
var additionalGatewayProxyYaml = `
220+
apiVersion: gateway.apisix.io/v1alpha1
221+
kind: GatewayProxy
222+
metadata:
223+
name: additional-proxy-config
224+
spec:
225+
provider:
226+
type: ControlPlane
227+
controlPlane:
228+
endpoints:
229+
- %s
230+
auth:
231+
type: AdminKey
232+
adminKey:
233+
value: "%s"
234+
`
235+
236+
var additionalGateway = `
237+
apiVersion: gateway.networking.k8s.io/v1
238+
kind: Gateway
239+
metadata:
240+
name: additional-gateway
241+
spec:
242+
gatewayClassName: %s
243+
listeners:
244+
- name: http-additional
245+
protocol: HTTP
246+
port: 80
247+
allowedRoutes:
248+
namespaces:
249+
from: All
250+
infrastructure:
251+
parametersRef:
252+
group: gateway.apisix.io
253+
kind: GatewayProxy
254+
name: additional-proxy-config
255+
`
256+
257+
// HTTPRoute that references both gateways
258+
var multiGatewayHTTPRoute = `
259+
apiVersion: gateway.networking.k8s.io/v1
260+
kind: HTTPRoute
261+
metadata:
262+
name: multi-gateway-route
263+
spec:
264+
parentRefs:
265+
- name: api7ee
266+
namespace: %s
267+
- name: additional-gateway
268+
namespace: %s
269+
hostnames:
270+
- httpbin.example
271+
- httpbin-additional.example
272+
rules:
273+
- matches:
274+
- path:
275+
type: Exact
276+
value: /get
277+
backendRefs:
278+
- name: httpbin-service-e2e-test
279+
port: 80
280+
`
281+
282+
BeforeEach(func() {
283+
beforeEachHTTP()
284+
285+
By("Create additional gateway group")
286+
var err error
287+
additionalGatewayGroupID, additionalNamespace, err = s.CreateAdditionalGatewayGroup("multi-gw")
288+
Expect(err).NotTo(HaveOccurred(), "creating additional gateway group")
289+
290+
By("Create additional GatewayProxy")
291+
// Get admin key for the additional gateway group
292+
resources, exists := s.GetAdditionalGatewayGroup(additionalGatewayGroupID)
293+
Expect(exists).To(BeTrue(), "additional gateway group should exist")
294+
295+
By("Create additional GatewayClass")
296+
additionalGatewayClassName = fmt.Sprintf("api7-%d", time.Now().Unix())
297+
err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(gatewayClassYaml, additionalGatewayClassName, s.GetControllerName()), "")
298+
Expect(err).NotTo(HaveOccurred(), "creating additional GatewayClass")
299+
time.Sleep(5 * time.Second)
300+
By("Check additional GatewayClass condition")
301+
gcyaml, err := s.GetResourceYaml("GatewayClass", additionalGatewayClassName)
302+
Expect(err).NotTo(HaveOccurred(), "getting additional GatewayClass yaml")
303+
Expect(gcyaml).To(ContainSubstring(`status: "True"`), "checking additional GatewayClass condition status")
304+
Expect(gcyaml).To(ContainSubstring("message: the gatewayclass has been accepted by the api7-ingress-controller"), "checking additional GatewayClass condition message")
305+
306+
additionalGatewayProxy := fmt.Sprintf(additionalGatewayProxyYaml, framework.DashboardTLSEndpoint, resources.AdminAPIKey)
307+
err = s.CreateResourceFromStringWithNamespace(additionalGatewayProxy, additionalNamespace)
308+
Expect(err).NotTo(HaveOccurred(), "creating additional GatewayProxy")
309+
310+
By("Create additional Gateway")
311+
err = s.CreateResourceFromStringWithNamespace(
312+
fmt.Sprintf(additionalGateway, additionalGatewayClassName),
313+
additionalNamespace,
314+
)
315+
Expect(err).NotTo(HaveOccurred(), "creating additional Gateway")
316+
time.Sleep(5 * time.Second)
317+
})
318+
319+
It("HTTPRoute should be accessible through both gateways", func() {
320+
By("Create HTTPRoute referencing both gateways")
321+
multiGatewayRoute := fmt.Sprintf(multiGatewayHTTPRoute, s.Namespace(), additionalNamespace)
322+
ResourceApplied("HTTPRoute", "multi-gateway-route", multiGatewayRoute, 1)
323+
324+
By("Access through default gateway")
325+
s.NewAPISIXClient().
326+
GET("/get").
327+
WithHost("httpbin.example").
328+
Expect().
329+
Status(http.StatusOK)
330+
331+
By("Access through additional gateway")
332+
client, err := s.NewAPISIXClientForGatewayGroup(additionalGatewayGroupID)
333+
Expect(err).NotTo(HaveOccurred(), "creating client for additional gateway")
334+
335+
client.
336+
GET("/get").
337+
WithHost("httpbin-additional.example").
338+
Expect().
339+
Status(http.StatusOK)
340+
341+
By("Delete Additional Gateway")
342+
err = s.DeleteResourceFromStringWithNamespace(fmt.Sprintf(additionalGateway, additionalGatewayClassName), additionalNamespace)
343+
Expect(err).NotTo(HaveOccurred(), "deleting additional Gateway")
344+
time.Sleep(5 * time.Second)
345+
346+
By("HTTPRoute should still be accessible through default gateway")
347+
s.NewAPISIXClient().
348+
GET("/get").
349+
WithHost("httpbin.example").
350+
Expect().
351+
Status(http.StatusOK)
352+
353+
By("HTTPRoute should not be accessible through additional gateway")
354+
client, err = s.NewAPISIXClientForGatewayGroup(additionalGatewayGroupID)
355+
Expect(err).NotTo(HaveOccurred(), "creating client for additional gateway")
356+
357+
client.
358+
GET("/get").
359+
WithHost("httpbin-additional.example").
360+
Expect().
361+
Status(http.StatusNotFound)
362+
})
363+
})
364+
214365
Context("HTTPRoute Base", func() {
215366
var exactRouteByGet = `
216367
apiVersion: gateway.networking.k8s.io/v1

test/e2e/scaffold/dp.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package scaffold
1616

1717
import (
18-
"github.com/gruntwork-io/terratest/modules/k8s"
1918
. "github.com/onsi/gomega"
2019

2120
"github.com/api7/api7-ingress-controller/test/e2e/framework"
@@ -43,36 +42,13 @@ func (s *Scaffold) deployDataplane() {
4342
}
4443

4544
func (s *Scaffold) newAPISIXTunnels() error {
46-
var (
47-
httpNodePort int
48-
httpsNodePort int
49-
httpPort int
50-
httpsPort int
51-
serviceName = "api7ee3-apisix-gateway-mtls"
52-
)
53-
54-
svc := s.dataplaneService
55-
for _, port := range svc.Spec.Ports {
56-
if port.Name == "http" {
57-
httpNodePort = int(port.NodePort)
58-
httpPort = int(port.Port)
59-
} else if port.Name == "https" {
60-
httpsNodePort = int(port.NodePort)
61-
httpsPort = int(port.Port)
62-
}
63-
}
64-
s.apisixHttpTunnel = k8s.NewTunnel(s.kubectlOptions, k8s.ResourceTypeService, serviceName,
65-
httpNodePort, httpPort)
66-
s.apisixHttpsTunnel = k8s.NewTunnel(s.kubectlOptions, k8s.ResourceTypeService, serviceName,
67-
httpsNodePort, httpsPort)
68-
69-
if err := s.apisixHttpTunnel.ForwardPortE(s.t); err != nil {
45+
serviceName := "api7ee3-apisix-gateway-mtls"
46+
httpTunnel, httpsTunnel, err := s.createDataplaneTunnels(s.dataplaneService, s.kubectlOptions, serviceName)
47+
if err != nil {
7048
return err
7149
}
72-
s.addFinalizers(s.apisixHttpTunnel.Close)
73-
if err := s.apisixHttpsTunnel.ForwardPortE(s.t); err != nil {
74-
return err
75-
}
76-
s.addFinalizers(s.apisixHttpsTunnel.Close)
50+
51+
s.apisixHttpTunnel = httpTunnel
52+
s.apisixHttpsTunnel = httpsTunnel
7753
return nil
7854
}

0 commit comments

Comments
 (0)