@@ -218,7 +218,8 @@ def _get_gateway_annotations(self, route: Dict[str, Any]) -> List[Dict[str, str]
218218 def _apply_annotations (
219219 self ,
220220 services : List [Dict [str , Any ]],
221- server_names : List [str ],
221+ service_ids : List [str ],
222+ all_hosts : List [str ],
222223 annotations : Dict [str , str ],
223224 ) -> None :
224225 if not annotations :
@@ -234,12 +235,12 @@ def _apply_annotations(
234235 continue
235236
236237 setting = annotation .replace ("bunkerweb.io/" , "" , 1 )
237- prefixed_targets = [name for name in server_names if setting .startswith (f"{ name } _" )]
238- if prefixed_targets and server_name not in prefixed_targets :
238+ prefixed_targets = [name for name in all_hosts if setting .startswith (f"{ name } _" )]
239+ if prefixed_targets and not setting . startswith ( f" { server_name } _" ) :
239240 continue
240241 success , _ = self ._db .is_valid_setting (setting , value = value , multisite = True )
241242 if success and not setting .startswith (f"{ server_name } _" ):
242- if any (setting .startswith (f"{ s } _" ) for s in server_names ):
243+ if any (setting .startswith (f"{ s } _" ) for s in all_hosts ):
243244 continue
244245 if setting == "SERVER_NAME" :
245246 self ._logger .warning ("Variable SERVER_NAME can't be set globally via annotations, ignoring it" )
@@ -457,7 +458,6 @@ def _to_services(self, controller_service) -> List[dict]:
457458 server_name_override = annotations .pop ("bunkerweb.io/SERVER_NAME" ).strip ().split ()[0 ]
458459
459460 services : List [Dict [str , Any ]] = []
460- server_names : set = set ()
461461 rules = spec .get ("rules" ) or []
462462
463463 if route_kind == "httproute" :
@@ -471,28 +471,23 @@ def _to_services(self, controller_service) -> List[dict]:
471471 self ._logger .warning (f"Ignoring HTTPRoute { namespace } /{ name } without hostnames" )
472472 return []
473473
474- server_names = set (hostnames )
475- for host in hostnames :
476- service : Dict [str , Any ] = {"SERVER_NAME" : host }
477- location = self ._reverse_proxy_suffix_start
478- listener_protocol = self ._get_listener_protocol (controller_service , host , allowed_protocols = ["HTTP" , "HTTPS" , "TLS" ])
479- if not listener_protocol :
480- self ._logger .warning (f"Ignoring HTTPRoute { namespace } /{ name } host { host } : no compatible HTTP/HTTPS/TLS listener found" )
481- continue
482- reverse_proxy_scheme = "tcp" if listener_protocol == "TCP" else listener_protocol .lower ()
483-
484- if listener_protocol == "TCP" :
485- service ["SERVER_TYPE" ] = "stream"
486- listener_port = self ._get_listener_port (controller_service , hostname = host , protocols = ["TCP" ])
487- if listener_port :
488- service ["LISTEN_STREAM_PORT" ] = str (listener_port )
489- service ["USE_TCP" ] = "yes"
490- service ["USE_UDP" ] = "no"
491-
492- if not rules :
493- services .append (service )
494- continue
474+ service : Dict [str , Any ] = {"SERVER_NAME" : " " .join (hostnames )}
475+ location = self ._reverse_proxy_suffix_start
476+ listener_protocol = self ._get_listener_protocol (controller_service , hostnames [0 ], allowed_protocols = ["HTTP" , "HTTPS" , "TLS" ])
477+ if not listener_protocol :
478+ self ._logger .warning (f"Ignoring HTTPRoute { namespace } /{ name } : no compatible HTTP/HTTPS/TLS listener found" )
479+ return []
480+ reverse_proxy_scheme = "tcp" if listener_protocol == "TCP" else listener_protocol .lower ()
481+
482+ if listener_protocol == "TCP" :
483+ service ["SERVER_TYPE" ] = "stream"
484+ listener_port = self ._get_listener_port (controller_service , hostname = hostnames [0 ], protocols = ["TCP" ])
485+ if listener_port :
486+ service ["LISTEN_STREAM_PORT" ] = str (listener_port )
487+ service ["USE_TCP" ] = "yes"
488+ service ["USE_UDP" ] = "no"
495489
490+ if rules :
496491 for rule in rules :
497492 matches = rule .get ("matches" ) or [None ]
498493 backend_ref = self ._get_backend_ref (rule .get ("backendRefs" ) or [], namespace , name or "unknown" , "HTTPRoute" )
@@ -523,7 +518,7 @@ def _to_services(self, controller_service) -> List[dict]:
523518 )
524519 location += 1
525520
526- services .append (service )
521+ services .append (service )
527522 elif route_kind == "tlsroute" :
528523 hostnames = spec .get ("hostnames" ) or []
529524 if server_name_override :
@@ -540,18 +535,14 @@ def _to_services(self, controller_service) -> List[dict]:
540535 hostnames = [default_name ]
541536 self ._logger .warning (f"TLSRoute { namespace } /{ name } has no hostnames, using { default_name } " )
542537
543- server_names = set (hostnames )
544- for host in hostnames :
545- service = {"SERVER_NAME" : host , "SERVER_TYPE" : "stream" , "USE_TCP" : "yes" , "USE_UDP" : "no" }
546- listener_port = self ._get_listener_port (controller_service , hostname = host , protocols = ["TLS" ])
547- if listener_port :
548- service ["LISTEN_STREAM_PORT_SSL" ] = str (listener_port )
549-
550- if not rules :
551- self ._logger .warning (f"Ignoring TLSRoute { namespace } /{ name } without rules" )
552- services .append (service )
553- continue
538+ service = {"SERVER_NAME" : " " .join (hostnames ), "SERVER_TYPE" : "stream" , "USE_TCP" : "yes" , "USE_UDP" : "no" }
539+ listener_port = self ._get_listener_port (controller_service , hostname = hostnames [0 ], protocols = ["TLS" ])
540+ if listener_port :
541+ service ["LISTEN_STREAM_PORT_SSL" ] = str (listener_port )
554542
543+ if not rules :
544+ self ._logger .warning (f"Ignoring TLSRoute { namespace } /{ name } without rules" )
545+ else :
555546 for rule_index , rule in enumerate (rules ):
556547 if rule_index > 0 :
557548 self ._logger .warning (f"TLSRoute { namespace } /{ name } has multiple rules, using only the first one" )
@@ -572,22 +563,18 @@ def _to_services(self, controller_service) -> List[dict]:
572563 "REVERSE_PROXY_URL" : "/" ,
573564 }
574565 )
575- services .append (service )
566+ services .append (service )
576567 elif route_kind in ("tcproute" , "udproute" ):
577568 protocol = "UDP" if route_kind == "udproute" else "TCP"
578569 default_name = self ._route_default_server_name (protocol .lower (), namespace , name )
579570 if server_name_override :
580- default_name = server_name_override
571+ hostnames = [ server_name_override ]
581572 else :
582573 listener_hostnames = self ._get_listener_hostnames (controller_service , protocols = [protocol ])
583- if listener_hostnames :
584- default_name = listener_hostnames [0 ]
585- if len (listener_hostnames ) > 1 :
586- self ._logger .warning (f"{ protocol } Route { namespace } /{ name } has multiple listener hostnames, using { default_name } " )
587- server_names = {default_name }
574+ hostnames = listener_hostnames or [default_name ]
588575 listener_port = self ._get_listener_port (controller_service , protocols = [protocol ])
589576 service = {
590- "SERVER_NAME" : default_name ,
577+ "SERVER_NAME" : " " . join ( hostnames ) ,
591578 "SERVER_TYPE" : "stream" ,
592579 "USE_TCP" : "yes" if protocol == "TCP" else "no" ,
593580 "USE_UDP" : "yes" if protocol == "UDP" else "no" ,
@@ -625,10 +612,20 @@ def _to_services(self, controller_service) -> List[dict]:
625612 return []
626613
627614 if services :
628- server_names = [service ["SERVER_NAME" ].strip ().split (" " )[0 ] for service in services if service .get ("SERVER_NAME" )]
615+ service_ids = []
616+ all_hosts = []
617+ for service in services :
618+ if not service .get ("SERVER_NAME" ):
619+ continue
620+ hosts = service ["SERVER_NAME" ].strip ().split ()
621+ if hosts :
622+ service_ids .append (hosts [0 ])
623+ all_hosts .extend (hosts )
624+ service_ids = list (dict .fromkeys (service_ids ))
625+ all_hosts = list (dict .fromkeys (all_hosts ))
629626 for gw_annotations in gateway_annotations :
630- self ._apply_annotations (services , server_names , gw_annotations )
631- self ._apply_annotations (services , server_names , annotations )
627+ self ._apply_annotations (services , service_ids , all_hosts , gw_annotations )
628+ self ._apply_annotations (services , service_ids , all_hosts , annotations )
632629
633630 for service in services :
634631 for server_name in service ["SERVER_NAME" ].strip ().split ():
0 commit comments