@@ -210,20 +210,12 @@ func buildL4Servers(logger logr.Logger, gateway *graph.Gateway, protocol v1.Prot
210210 continue
211211 }
212212
213- // For single backend, use direct upstream name
214- // For multiple backends, we'll create a combined upstream name based on the route
215- var upstreamName string
216- if len (backendRefs ) == 1 {
217- upstreamName = backendRefs [0 ].ServicePortReference ()
218- } else {
219- // For multiple backends, create a group upstream name
220- // Format: protocol_namespace_routename
221- upstreamName = fmt .Sprintf ("%s_%s_%s" ,
222- protocolName ,
223- r .Source .GetNamespace (),
224- r .Source .GetName (),
225- )
226- }
213+ // Use unified upstream naming: protocol_namespace_routename
214+ upstreamName := fmt .Sprintf ("%s_%s_%s" ,
215+ protocolName ,
216+ r .Source .GetNamespace (),
217+ r .Source .GetName (),
218+ )
227219
228220 server := Layer4VirtualServer {
229221 Hostname : "" , // Layer4 doesn't use hostnames
@@ -324,122 +316,96 @@ func buildL4Upstreams(
324316 protocol v1.ProtocolType ,
325317) []Upstream {
326318 uniqueUpstreams := make (map [string ]Upstream )
327-
328319 protocolName := string (protocol )
329320 gatewayNSName := client .ObjectKeyFromObject (gateway .Source )
321+ allowedAddressType := getAllowedAddressType (ipFamily )
330322
331323 for _ , l := range gateway .Listeners {
332324 if ! l .Valid || l .Source .Protocol != protocol {
333325 continue
334326 }
327+ processListenerRoutes (ctx , logger , l .L4Routes , protocolName , gatewayNSName ,
328+ allowedAddressType , serviceResolver , uniqueUpstreams )
329+ }
335330
336- for _ , route := range l .L4Routes {
337- if ! route .Valid {
338- continue
339- }
340-
341- // Use helper method to get all backend references
342- backendRefs := route .Spec .GetBackendRefs ()
343-
344- if len (backendRefs ) == 0 {
345- continue
346- }
347-
348- // For single backend: create one upstream with service name
349- // For multiple backends: create individual upstreams + one combined upstream with weighted endpoints
350- if len (backendRefs ) == 1 {
351- br := backendRefs [0 ]
352- if ! br .Valid {
353- continue
354- }
355-
356- if _ , ok := br .InvalidForGateways [gatewayNSName ]; ok {
357- continue
358- }
359-
360- upstreamName := br .ServicePortReference ()
361-
362- if _ , exist := uniqueUpstreams [upstreamName ]; exist {
363- continue
364- }
365-
366- var errMsg string
367- allowedAddressType := getAllowedAddressType (ipFamily )
331+ if len (uniqueUpstreams ) == 0 {
332+ return nil
333+ }
334+ upstreams := make ([]Upstream , 0 , len (uniqueUpstreams ))
335+ for _ , up := range uniqueUpstreams {
336+ upstreams = append (upstreams , up )
337+ }
368338
369- eps , err := serviceResolver .Resolve (ctx , logger , br .SvcNsName , br .ServicePort , allowedAddressType )
370- if err != nil {
371- errMsg = err .Error ()
372- }
339+ return upstreams
340+ }
373341
374- uniqueUpstreams [upstreamName ] = Upstream {
375- Name : upstreamName ,
376- Endpoints : eps ,
377- ErrorMsg : errMsg ,
378- }
379- } else {
380- // Multiple backends: create a combined upstream with weighted endpoints
381- combinedUpstreamName := fmt .Sprintf ("%s_%s_%s" ,
382- protocolName ,
383- route .Source .GetNamespace (),
384- route .Source .GetName (),
385- )
342+ func processListenerRoutes (
343+ ctx context.Context ,
344+ logger logr.Logger ,
345+ routes map [graph.L4RouteKey ]* graph.L4Route ,
346+ protocolName string ,
347+ gatewayNSName types.NamespacedName ,
348+ allowedAddressType []discoveryV1.AddressType ,
349+ serviceResolver resolver.ServiceResolver ,
350+ uniqueUpstreams map [string ]Upstream ,
351+ ) {
352+ for _ , route := range routes {
353+ if ! route .Valid {
354+ continue
355+ }
386356
387- if _ , exist := uniqueUpstreams [combinedUpstreamName ]; exist {
388- continue
389- }
357+ backendRefs := route .Spec .GetBackendRefs ()
358+ if len (backendRefs ) == 0 {
359+ continue
360+ }
390361
391- var combinedEndpoints []resolver.Endpoint
392- var errMsgs []string
393- allowedAddressType := getAllowedAddressType (ipFamily )
362+ upstreamName := fmt .Sprintf ("%s_%s_%s" ,
363+ protocolName ,
364+ route .Source .GetNamespace (),
365+ route .Source .GetName (),
366+ )
394367
395- // Collect endpoints from all backends with their weights
396- for _ , br := range backendRefs {
397- if ! br .Valid {
398- continue
399- }
368+ if _ , exist := uniqueUpstreams [upstreamName ]; exist {
369+ continue
370+ }
400371
401- if _ , ok := br .InvalidForGateways [gatewayNSName ]; ok {
402- continue
403- }
372+ var endpoints []resolver.Endpoint
373+ var errMsgs []string
404374
405- eps , err := serviceResolver . Resolve ( ctx , logger , br . SvcNsName , br . ServicePort , allowedAddressType )
406- if err != nil {
407- errMsgs = append ( errMsgs , err . Error ())
408- continue
409- }
375+ // Collect endpoints from all backends with their weights
376+ for _ , br := range backendRefs {
377+ if ! br . Valid {
378+ continue
379+ }
410380
411- // Add weight to each endpoint
412- for _ , ep := range eps {
413- ep .Weight = br .Weight
414- combinedEndpoints = append (combinedEndpoints , ep )
415- }
416- }
381+ if _ , ok := br .InvalidForGateways [gatewayNSName ]; ok {
382+ continue
383+ }
417384
418- var errMsg string
419- if len (errMsgs ) > 0 {
420- errMsg = fmt .Sprintf ("some backends failed: %v" , errMsgs )
421- }
385+ eps , err := serviceResolver .Resolve (ctx , logger , br .SvcNsName , br .ServicePort , allowedAddressType )
386+ if err != nil {
387+ errMsgs = append (errMsgs , err .Error ())
388+ continue
389+ }
422390
423- uniqueUpstreams [combinedUpstreamName ] = Upstream {
424- Name : combinedUpstreamName ,
425- Endpoints : combinedEndpoints ,
426- ErrorMsg : errMsg ,
427- }
391+ // Add weight to each endpoint
392+ for _ , ep := range eps {
393+ ep .Weight = br .Weight
394+ endpoints = append (endpoints , ep )
428395 }
429396 }
430- }
431-
432- if len (uniqueUpstreams ) == 0 {
433- return nil
434- }
435397
436- upstreams := make ([]Upstream , 0 , len (uniqueUpstreams ))
398+ var errMsg string
399+ if len (errMsgs ) > 0 {
400+ errMsg = fmt .Sprintf ("some backends failed: %v" , errMsgs )
401+ }
437402
438- for _ , up := range uniqueUpstreams {
439- upstreams = append (upstreams , up )
403+ uniqueUpstreams [upstreamName ] = Upstream {
404+ Name : upstreamName ,
405+ Endpoints : endpoints ,
406+ ErrorMsg : errMsg ,
407+ }
440408 }
441-
442- return upstreams
443409}
444410
445411// buildSSLKeyPairs builds the SSLKeyPairs from the Secrets. It will only include Secrets that are referenced by
0 commit comments