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