@@ -1711,6 +1711,102 @@ spec:
17111711 })
17121712 })
17131713
1714+ Context ("Test HTTPRoute Load Balancing" , func () {
1715+ BeforeEach (beforeEachHTTP )
1716+ It ("Test load balancing with ExternalName services" , func () {
1717+ const servicesSpec = `
1718+ apiVersion: v1
1719+ kind: Service
1720+ metadata:
1721+ name: httpbin-external-domain
1722+ spec:
1723+ type: ExternalName
1724+ externalName: httpbin.org
1725+ ---
1726+ apiVersion: v1
1727+ kind: Service
1728+ metadata:
1729+ name: mockapi7-external-domain
1730+ spec:
1731+ type: ExternalName
1732+ externalName: mock.api7.ai
1733+ ---
1734+ apiVersion: apisix.apache.org/v1alpha1
1735+ kind: BackendTrafficPolicy
1736+ metadata:
1737+ name: passhost-node
1738+ spec:
1739+ targetRefs:
1740+ - name: httpbin-external-domain
1741+ kind: Service
1742+ group: ""
1743+ - name: mockapi7-external-domain
1744+ kind: Service
1745+ group: ""
1746+ passHost: node
1747+ scheme: https
1748+ ---
1749+ apiVersion: gateway.networking.k8s.io/v1
1750+ kind: HTTPRoute
1751+ metadata:
1752+ name: lb-route
1753+ spec:
1754+ parentRefs:
1755+ - name: apisix
1756+ rules:
1757+ - matches:
1758+ - path:
1759+ type: Exact
1760+ value: /headers
1761+ backendRefs:
1762+ - name: httpbin-external-domain
1763+ port: 443
1764+ weight: 1
1765+ - name: mockapi7-external-domain
1766+ port: 443
1767+ weight: 1
1768+ `
1769+
1770+ By ("apply services and HTTPRoute" )
1771+ err := s .CreateResourceFromString (servicesSpec )
1772+ Expect (err ).ShouldNot (HaveOccurred (), "apply services and HTTPRoute" )
1773+ time .Sleep (5 * time .Second )
1774+
1775+ By ("verify load balancing works" )
1776+ // Test multiple requests to verify load balancing
1777+ upstreamHosts := make (map [string ]int )
1778+ totalRequests := 20
1779+
1780+ for i := 0 ; i < totalRequests ; i ++ {
1781+ resp := s .NewAPISIXClient ().GET ("/headers" ).Expect ().Status (http .StatusOK )
1782+
1783+ // Parse JSON response to get the Host header
1784+ var responseBody map [string ]any
1785+ resp .JSON ().Decode (& responseBody )
1786+
1787+ if headers , ok := responseBody ["headers" ].(map [string ]any ); ok {
1788+ var host string
1789+ if host , ok = headers ["Host" ].(string ); ! ok {
1790+ host , ok = headers ["host" ].(string )
1791+ }
1792+ if ok && host != "" {
1793+ upstreamHosts [host ]++
1794+ }
1795+ Expect (ok ).To (BeTrue (), "Host header should be present" )
1796+ Expect (host ).Should (Or (Equal ("httpbin.org" ), Equal ("mock.api7.ai" )))
1797+ }
1798+ time .Sleep (100 * time .Millisecond ) // Small delay between requests
1799+ }
1800+
1801+ By ("verify both upstreams received requests" )
1802+ Expect (upstreamHosts ).Should (HaveLen (2 ))
1803+
1804+ for host , count := range upstreamHosts {
1805+ Expect (count ).Should (BeNumerically (">" , 0 ), fmt .Sprintf ("upstream %s should receive requests" , host ))
1806+ }
1807+ })
1808+ })
1809+
17141810 /*
17151811 Context("HTTPRoute Status Updated", func() {
17161812 })
0 commit comments