@@ -342,7 +342,8 @@ func bindRoutesToListeners(
342342 routes = append (routes , r )
343343 }
344344
345- isolateL7RouteListeners (routes , gw .Listeners )
345+ listenerMap := getListenerHostPortMap (gw .Listeners )
346+ isolateL7RouteListeners (routes , listenerMap )
346347
347348 l4RouteSlice := make ([]* L4Route , 0 , len (l4Routes ))
348349 for _ , r := range l4Routes {
@@ -361,53 +362,72 @@ func bindRoutesToListeners(
361362 bindL4RouteToListeners (r , gw , namespaces , portHostnamesMap )
362363 }
363364
364- isolateL4RouteListeners (l4RouteSlice , gw . Listeners )
365+ isolateL4RouteListeners (l4RouteSlice , listenerMap )
365366}
366367
367- // isolateL7RouteListeners ensures listener isolation for all L7Routes.
368- func isolateL7RouteListeners (routes []* L7Route , listeners []* Listener ) {
369- listenerHostnameMap := make (map [string ]string , len (listeners ))
368+ type hostPort struct {
369+ hostname string
370+ port v1.PortNumber
371+ }
372+
373+ func getListenerHostPortMap (listeners []* Listener ) map [string ]hostPort {
374+ listenerHostPortMap := make (map [string ]hostPort , len (listeners ))
370375 for _ , l := range listeners {
371- listenerHostnameMap [l .Name ] = getHostname (l .Source .Hostname )
376+ listenerHostPortMap [l .Name ] = hostPort {
377+ hostname : getHostname (l .Source .Hostname ),
378+ port : l .Source .Port ,
379+ }
372380 }
381+ return listenerHostPortMap
382+ }
373383
384+ // isolateL7RouteListeners ensures listener isolation for all L7Routes.
385+ func isolateL7RouteListeners (routes []* L7Route , listenerHostPortMap map [string ]hostPort ) {
374386 for _ , route := range routes {
375- isolateHostnamesForParentRefs (route .ParentRefs , listenerHostnameMap )
387+ isolateHostnamesForParentRefs (route .ParentRefs , listenerHostPortMap , false )
376388 }
377389}
378390
379391// isolateL4RouteListeners ensures listener isolation for all L4Routes.
380- func isolateL4RouteListeners (routes []* L4Route , listeners []* Listener ) {
381- listenerHostnameMap := make (map [string ]string , len (listeners ))
382- for _ , l := range listeners {
383- listenerHostnameMap [l .Name ] = getHostname (l .Source .Hostname )
384- }
385-
392+ func isolateL4RouteListeners (routes []* L4Route , listenerHostPortMap map [string ]hostPort ) {
386393 for _ , route := range routes {
387- isolateHostnamesForParentRefs (route .ParentRefs , listenerHostnameMap )
394+ isolateHostnamesForParentRefs (route .ParentRefs , listenerHostPortMap , true )
388395 }
389396}
390397
391398// isolateHostnamesForParentRefs iterates through the parentRefs of a route to identify the list of accepted hostnames
392- // for each listener. If any accepted hostname belongs to another listener,
399+ // for each listener. If any accepted hostname belongs to another listener with the same port, then
393400// it removes those hostnames to ensure listener isolation.
394- func isolateHostnamesForParentRefs (parentRef []ParentRef , listenerHostnameMap map [string ]string ) {
401+ func isolateHostnamesForParentRefs (parentRef []ParentRef , listenerHostnameMap map [string ]hostPort , isL4Route bool ) {
395402 for _ , ref := range parentRef {
396- acceptedHostnames := ref .Attachment .AcceptedHostnames
403+ // when sectionName is nil we allow all listeners to attach to the route
404+ if ref .SectionName == nil {
405+ continue
406+ }
397407
408+ acceptedHostnames := ref .Attachment .AcceptedHostnames
398409 hostnamesToRemoves := make (map [string ]struct {})
399410 for listenerName , hostnames := range acceptedHostnames {
400411 if len (hostnames ) == 0 {
401412 continue
402413 }
403414 for _ , h := range hostnames {
404- for lName , lHostname := range listenerHostnameMap {
415+ for lName , lHostPort := range listenerHostnameMap {
405416 // skip comparison if it is a catch all listener block
406- if lHostname == "" {
417+ if lHostPort . hostname == "" {
407418 continue
408419 }
409- if h == lHostname && listenerName != lName {
410- hostnamesToRemoves [h ] = struct {}{}
420+
421+ // we only compare hostname and listener combination for L4Route,
422+ // since we don't allow the same port and hostname combination for L4Route when attaching to listeners.
423+ if isL4Route {
424+ if h == lHostPort .hostname && listenerName != lName {
425+ hostnamesToRemoves [h ] = struct {}{}
426+ }
427+ } else {
428+ if h == lHostPort .hostname && listenerName != lName && lHostPort .port == ref .Attachment .ListenerPort {
429+ hostnamesToRemoves [h ] = struct {}{}
430+ }
411431 }
412432 }
413433 }
0 commit comments