Repository navigation
refactor(BA-8675): add TraefikRoutingConfigFactory for ingresses
#16018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jopemachine
merged 13 commits into
main
from
refactor/BA-8675-traefik-routing-config-factory
Oct 26, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8c63cc8
refactor(BA-8675): add TraefikRoutingConfigFactory for ingresses
jopemachine a5baaad
changelog: add news fragment for PR #16018
jopemachine 5a17e64
refactor(BA-8675): hold the TraefikConfig in TraefikRoutingConfigFactory
jopemachine c163494
refactor(BA-8675): move TraefikWorker to clients/worker/types.py
jopemachine 729a953
refactor(BA-8675): rename TraefikWorker to WorkerFrontendSettings
jopemachine 58e3b76
refactor(BA-8675): pass the worker hostname, wildcard domain and tls_…
jopemachine cebf64e
refactor(BA-8675): take TraefikRoutingConfigFactoryArgs in TraefikRou…
jopemachine 4d4e3a8
refactor(BA-8675): rename routing_config_etcd_keys to all_routing_con…
jopemachine 1c75eaa
refactor(BA-8675): type the Traefik routing configs as TypedDicts
jopemachine 795c677
refactor(BA-8675): drop the Traefik prefix from the routing config types
jopemachine 9cd5953
refactor(BA-8675): split the Traefik routing configs into a traefik p…
jopemachine a18f2cc
test(BA-8675): build the routing config factory from real config inst…
jopemachine ef7a93b
test(BA-8675): cover all_routing_config_etcd_keys
jopemachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Build the Traefik router, service and middleware configs of an AppProxy ingress through `TraefikRoutingConfigFactory` |
Empty file.
285 changes: 285 additions & 0 deletions
285
src/ai/backend/appproxy/coordinator/clients/worker/traefik/routing_config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,285 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Final | ||
|
|
||
| from ai.backend.appproxy.common.config import PermitHashConfig, SecretConfig | ||
| from ai.backend.appproxy.common.defs import PERMIT_COOKIE_NAME | ||
| from ai.backend.appproxy.coordinator.clients.worker.traefik.types import ( | ||
| AuthPluginMiddlewareConfig, | ||
| HttpMiddlewareConfig, | ||
| HttpRouterConfig, | ||
| HttpServer, | ||
| HttpServiceConfig, | ||
| IpAllowList, | ||
| IpAllowListMiddlewareConfig, | ||
| RoutingConfig, | ||
| TcpRouterConfig, | ||
| TcpServer, | ||
| TcpServiceConfig, | ||
| ) | ||
| from ai.backend.appproxy.coordinator.config import TraefikConfig | ||
| from ai.backend.appproxy.coordinator.errors.ingress import TraefikWildcardDomainMissingError | ||
| from ai.backend.common.dto.appproxy.v2.ingress.types import ( | ||
| Ingress, | ||
| IngressAccess, | ||
| IngressAuthority, | ||
| IngressBackend, | ||
| IngressFrontend, | ||
| IngressFrontendKind, | ||
| IngressKey, | ||
| IngressProtocol, | ||
| IngressTarget, | ||
| ) | ||
|
|
||
| _CORS_HEADERS_MIDDLEWARE_NAME: Final[str] = "CORSHeaders" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class TraefikRoutingConfigFactoryArgs: | ||
| traefik_config: TraefikConfig | ||
| secrets: SecretConfig | ||
| permit_hash: PermitHashConfig | ||
|
|
||
|
|
||
| class TraefikRoutingConfigFactory: | ||
| """Builds the Traefik router, middleware and service configs of an ingress and the etcd keys | ||
| Traefik reads them from.""" | ||
|
|
||
| _traefik_config: TraefikConfig | ||
| _secrets: SecretConfig | ||
| _permit_hash: PermitHashConfig | ||
|
|
||
| def __init__(self, args: TraefikRoutingConfigFactoryArgs) -> None: | ||
| self._traefik_config = args.traefik_config | ||
| self._secrets = args.secrets | ||
| self._permit_hash = args.permit_hash | ||
|
|
||
| @classmethod | ||
| def all_routing_config_etcd_keys(cls, ingress_key: IngressKey) -> list[str]: | ||
| """Every routing config key an ingress can own, for clearing the ones it no longer has.""" | ||
| return [ | ||
| cls._router_etcd_key(ingress_key, IngressProtocol.HTTP), | ||
| cls._service_etcd_key(ingress_key, IngressProtocol.HTTP), | ||
| cls._http_middleware_etcd_key( | ||
| ingress_key.authority, cls._ip_allowlist_middleware_name(ingress_key) | ||
| ), | ||
| cls._http_middleware_etcd_key( | ||
| ingress_key.authority, cls._auth_plugin_middleware_name(ingress_key) | ||
| ), | ||
| cls._router_etcd_key(ingress_key, IngressProtocol.TCP), | ||
| cls._service_etcd_key(ingress_key, IngressProtocol.TCP), | ||
| ] | ||
|
|
||
| def routing_configs_by_etcd_key( | ||
| self, ingress: Ingress, *, hostname: str, wildcard_domain: str | None, tls_listen: bool | ||
| ) -> dict[str, RoutingConfig]: | ||
| """Router, service and middleware configs of the ingress, by the etcd key Traefik reads.""" | ||
| ingress_key = ingress.key() | ||
| frontend_protocol = ingress.frontend.protocol | ||
| router_etcd_key = self._router_etcd_key(ingress_key, frontend_protocol) | ||
| service_etcd_key = self._service_etcd_key(ingress_key, frontend_protocol) | ||
| match frontend_protocol: | ||
| case IngressProtocol.TCP: | ||
| return { | ||
| router_etcd_key: self._tcp_router_config(ingress), | ||
| service_etcd_key: self._tcp_service_config(ingress.backend), | ||
| } | ||
| case IngressProtocol.HTTP | IngressProtocol.HTTP2: | ||
| return { | ||
| router_etcd_key: self._http_router_config( | ||
| ingress, | ||
| host_rule=self._host_rule( | ||
| ingress, hostname=hostname, wildcard_domain=wildcard_domain | ||
| ), | ||
| tls_listen=tls_listen, | ||
| ), | ||
| service_etcd_key: self._http_service_config(ingress.backend), | ||
| **self._http_middleware_configs(ingress), | ||
| } | ||
|
|
||
| @classmethod | ||
| def _router_etcd_key(cls, ingress_key: IngressKey, protocol: IngressProtocol) -> str: | ||
| traefik_protocol_key = cls._traefik_protocol_key(protocol) | ||
| return ( | ||
| f"worker_{ingress_key.authority}/{traefik_protocol_key}/routers/" | ||
| f"{cls._router_name(ingress_key)}" | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _service_etcd_key(cls, ingress_key: IngressKey, protocol: IngressProtocol) -> str: | ||
| traefik_protocol_key = cls._traefik_protocol_key(protocol) | ||
| return ( | ||
| f"worker_{ingress_key.authority}/{traefik_protocol_key}/services/" | ||
| f"{cls._service_name(ingress_key)}" | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _http_router_config( | ||
| cls, ingress: Ingress, *, host_rule: str, tls_listen: bool | ||
| ) -> HttpRouterConfig: | ||
| ingress_key = ingress.key() | ||
| # The ipAllowList middleware must precede the auth plugin so that disallowed | ||
| # clients are rejected before any credential handling. | ||
| middleware_names = [_CORS_HEADERS_MIDDLEWARE_NAME] | ||
| if ingress.access.allowed_client_ips: | ||
| middleware_names.append(cls._ip_allowlist_middleware_name(ingress_key)) | ||
| middleware_names.append(cls._auth_plugin_middleware_name(ingress_key)) | ||
| router_config: HttpRouterConfig = { | ||
| "rule": host_rule, | ||
| "service": cls._service_name(ingress_key), | ||
| "entrypoints": [cls._entrypoint_name(ingress.frontend)], | ||
| "middlewares": middleware_names, | ||
| } | ||
| if tls_listen: | ||
| router_config["tls"] = "" | ||
| return router_config | ||
|
|
||
| @classmethod | ||
| def _tcp_router_config(cls, ingress: Ingress) -> TcpRouterConfig: | ||
| return { | ||
| "rule": cls._client_ip_rule(ingress.access), | ||
| "service": cls._service_name(ingress.key()), | ||
| "entrypoints": [cls._entrypoint_name(ingress.frontend)], | ||
| } | ||
|
|
||
| def _http_middleware_configs(self, ingress: Ingress) -> dict[str, HttpMiddlewareConfig]: | ||
| ingress_key = ingress.key() | ||
| cors_headers_etcd_key = self._http_middleware_etcd_key( | ||
| ingress.authority, _CORS_HEADERS_MIDDLEWARE_NAME | ||
| ) | ||
| middleware_configs: dict[str, HttpMiddlewareConfig] = { | ||
| cors_headers_etcd_key: { | ||
| "headers": { | ||
| "accessControlAllowHeaders": "*", | ||
| "accessControlAllowOriginList": ["*"], | ||
| }, | ||
| }, | ||
| } | ||
| if ingress.access.allowed_client_ips: | ||
| ip_allowlist_etcd_key = self._http_middleware_etcd_key( | ||
| ingress.authority, self._ip_allowlist_middleware_name(ingress_key) | ||
| ) | ||
| middleware_configs[ip_allowlist_etcd_key] = self._ip_allowlist_middleware_config( | ||
| ingress.access | ||
| ) | ||
| auth_plugin_etcd_key = self._http_middleware_etcd_key( | ||
| ingress.authority, self._auth_plugin_middleware_name(ingress_key) | ||
| ) | ||
| middleware_configs[auth_plugin_etcd_key] = self._auth_plugin_middleware_config(ingress) | ||
| return middleware_configs | ||
|
|
||
| def _ip_allowlist_middleware_config(self, access: IngressAccess) -> IpAllowListMiddlewareConfig: | ||
| ip_allowlist: IpAllowList = { | ||
| "sourceRange": [str(network) for network in access.allowed_client_ips], | ||
| } | ||
| client_ip_strategy = self._traefik_config.client_ip_strategy | ||
| if client_ip_strategy is not None: | ||
| if client_ip_strategy.depth is not None: | ||
| ip_allowlist["ipStrategy"] = {"depth": client_ip_strategy.depth} | ||
| elif client_ip_strategy.excluded_ips: | ||
| ip_allowlist["ipStrategy"] = { | ||
| "excludedIPs": [str(network) for network in client_ip_strategy.excluded_ips], | ||
| } | ||
| return {"ipAllowList": ip_allowlist} | ||
|
|
||
| def _auth_plugin_middleware_config(self, ingress: Ingress) -> AuthPluginMiddlewareConfig: | ||
| return { | ||
| "plugin": { | ||
| "appproxy-traefik-plugin": { | ||
| # etcd holds strings only, so the ingress travels as one JSON value. | ||
| "ingress": ingress.model_dump_json(), | ||
| "jwt_secret": self._secrets.jwt_secret, | ||
| "permit_hash_secret": self._permit_hash.secret.decode(), | ||
| "permit_cookie_name": PERMIT_COOKIE_NAME, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| @classmethod | ||
| def _http_service_config(cls, backend: IngressBackend) -> HttpServiceConfig: | ||
| servers: list[HttpServer] = [ | ||
| {"url": f"http://{target.host}:{target.port}/"} | ||
| for target in cls._targets_receiving_traffic(backend) | ||
| ] | ||
| if not servers: | ||
| return {} | ||
| return {"loadBalancer": {"servers": servers}} | ||
|
|
||
| @classmethod | ||
| def _tcp_service_config(cls, backend: IngressBackend) -> TcpServiceConfig: | ||
| servers: list[TcpServer] = [ | ||
| {"address": f"{target.host}:{target.port}"} | ||
| for target in cls._targets_receiving_traffic(backend) | ||
| ] | ||
| if not servers: | ||
| return {} | ||
| return {"loadBalancer": {"servers": servers}} | ||
|
|
||
| # Frontend keys hold no '_', so no router, service or middleware name of one ingress is a | ||
| # prefix of another's. | ||
| @staticmethod | ||
| def _router_name(ingress_key: IngressKey) -> str: | ||
| return f"bai_ingress_{ingress_key.frontend_key}_router" | ||
|
|
||
| @staticmethod | ||
| def _service_name(ingress_key: IngressKey) -> str: | ||
| return f"bai_ingress_{ingress_key.frontend_key}_service" | ||
|
|
||
| @staticmethod | ||
| def _ip_allowlist_middleware_name(ingress_key: IngressKey) -> str: | ||
| return f"bai_ingress_{ingress_key.frontend_key}_ipallowlist" | ||
|
|
||
| @staticmethod | ||
| def _auth_plugin_middleware_name(ingress_key: IngressKey) -> str: | ||
| return f"bai_ingress_{ingress_key.frontend_key}_auth" | ||
|
|
||
| @staticmethod | ||
| def _traefik_protocol_key(protocol: IngressProtocol) -> str: | ||
| # Traefik keeps HTTP/2 routing under its http configuration. | ||
| match protocol: | ||
| case IngressProtocol.HTTP | IngressProtocol.HTTP2: | ||
| return "http" | ||
| case IngressProtocol.TCP: | ||
| return "tcp" | ||
|
|
||
| @staticmethod | ||
| def _http_middleware_etcd_key(authority: IngressAuthority, middleware_name: str) -> str: | ||
| return f"worker_{authority}/http/middlewares/{middleware_name}" | ||
|
|
||
| @staticmethod | ||
| def _host_rule(ingress: Ingress, *, hostname: str, wildcard_domain: str | None) -> str: | ||
| match ingress.frontend.kind: | ||
| case IngressFrontendKind.PORT: | ||
| return f"Host(`{hostname}`)" | ||
| case IngressFrontendKind.WILDCARD: | ||
| if wildcard_domain is None: | ||
| raise TraefikWildcardDomainMissingError( | ||
| extra_data={"authority": ingress.authority} | ||
| ) | ||
| return f"Host(`{ingress.frontend.key()}{wildcard_domain}`)" | ||
|
|
||
| @staticmethod | ||
| def _client_ip_rule(access: IngressAccess) -> str: | ||
| allowed_client_ip_ranges = [str(network) for network in access.allowed_client_ips] | ||
| if not allowed_client_ip_ranges: | ||
| return "ClientIP(`0.0.0.0/0`)" | ||
| # Traefik v3 rule matchers take one value each, so the ranges are joined with ||. | ||
| return " || ".join(f"ClientIP(`{cidr}`)" for cidr in allowed_client_ip_ranges) | ||
|
|
||
| @staticmethod | ||
| def _entrypoint_name(frontend: IngressFrontend) -> str: | ||
| match frontend.kind: | ||
| case IngressFrontendKind.PORT: | ||
| return f"portproxy_{frontend.key()}" | ||
| case IngressFrontendKind.WILDCARD: | ||
| return "domainproxy" | ||
|
|
||
| @staticmethod | ||
| def _targets_receiving_traffic(backend: IngressBackend) -> list[IngressTarget]: | ||
| return [ | ||
| target | ||
| for group in backend.target_groups | ||
| if group.weight > 0 | ||
| for target in group.targets | ||
| ] | ||
90 changes: 90 additions & 0 deletions
90
src/ai/backend/appproxy/coordinator/clients/worker/traefik/types.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import NotRequired, TypedDict | ||
|
|
||
|
|
||
| class HttpRouterConfig(TypedDict): | ||
| rule: str | ||
| service: str | ||
| entrypoints: list[str] | ||
| middlewares: list[str] | ||
| tls: NotRequired[str] | ||
|
|
||
|
|
||
| class TcpRouterConfig(TypedDict): | ||
| rule: str | ||
| service: str | ||
| entrypoints: list[str] | ||
|
|
||
|
|
||
| class HttpServer(TypedDict): | ||
| url: str | ||
|
|
||
|
|
||
| class HttpLoadBalancer(TypedDict): | ||
| servers: list[HttpServer] | ||
|
|
||
|
|
||
| class HttpServiceConfig(TypedDict): | ||
| # Left out when no target receives traffic, which clears the service. | ||
| loadBalancer: NotRequired[HttpLoadBalancer] | ||
|
|
||
|
|
||
| class TcpServer(TypedDict): | ||
| address: str | ||
|
|
||
|
|
||
| class TcpLoadBalancer(TypedDict): | ||
| servers: list[TcpServer] | ||
|
|
||
|
|
||
| class TcpServiceConfig(TypedDict): | ||
| # Left out when no target receives traffic, which clears the service. | ||
| loadBalancer: NotRequired[TcpLoadBalancer] | ||
|
|
||
|
|
||
| class CorsHeaders(TypedDict): | ||
| accessControlAllowHeaders: str | ||
| accessControlAllowOriginList: list[str] | ||
|
|
||
|
|
||
| class CorsHeadersMiddlewareConfig(TypedDict): | ||
| headers: CorsHeaders | ||
|
|
||
|
|
||
| class IpStrategy(TypedDict, total=False): | ||
| depth: int | ||
| excludedIPs: list[str] | ||
|
|
||
|
|
||
| class IpAllowList(TypedDict): | ||
| sourceRange: list[str] | ||
| ipStrategy: NotRequired[IpStrategy] | ||
|
|
||
|
|
||
| class IpAllowListMiddlewareConfig(TypedDict): | ||
| ipAllowList: IpAllowList | ||
|
|
||
|
|
||
| class AuthPluginOptions(TypedDict): | ||
| ingress: str | ||
| jwt_secret: str | ||
| permit_hash_secret: str | ||
| permit_cookie_name: str | ||
|
|
||
|
|
||
| AuthPluginOptionsByName = TypedDict( | ||
| "AuthPluginOptionsByName", {"appproxy-traefik-plugin": AuthPluginOptions} | ||
| ) | ||
|
|
||
|
|
||
| class AuthPluginMiddlewareConfig(TypedDict): | ||
| plugin: AuthPluginOptionsByName | ||
|
|
||
|
|
||
| type HttpMiddlewareConfig = ( | ||
| CorsHeadersMiddlewareConfig | IpAllowListMiddlewareConfig | AuthPluginMiddlewareConfig | ||
| ) | ||
| type RoutingConfig = ( | ||
| HttpRouterConfig | TcpRouterConfig | HttpServiceConfig | TcpServiceConfig | HttpMiddlewareConfig | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.