Handle wildcard hosts
This commit is contained in:
parent
91d0df1985
commit
e1eacb5512
|
@ -11,7 +11,8 @@ import argparse
|
|||
import json
|
||||
import subprocess
|
||||
|
||||
TRAEFIK_HOST_RE = re.compile(r"Host\(`([a-z0-9\.-]+)`\)")
|
||||
TRAEFIK_RULE_RE = re.compile(r"([a-zA-Z]+)\(`(.+?)`\)")
|
||||
SUBDOMAIN_WILDCARD_PREFIX = "{subdomain:[a-z]+}"
|
||||
|
||||
|
||||
class Route(NamedTuple):
|
||||
|
@ -31,10 +32,19 @@ class Config(TypedDict):
|
|||
|
||||
|
||||
def parse_traefik_rule(rule: str) -> list[str]:
|
||||
if "Host(" not in rule:
|
||||
return []
|
||||
rules = defaultdict(list)
|
||||
for (rule_fn, rule_arg) in TRAEFIK_RULE_RE.findall(rule):
|
||||
rules[rule_fn].append(rule_arg)
|
||||
|
||||
return TRAEFIK_HOST_RE.findall(rule)
|
||||
if host_rules := rules.get("Host"):
|
||||
return host_rules[0]
|
||||
|
||||
elif regex_rules := rules.get("HostRegexp"):
|
||||
regex_rule = regex_rules[0]
|
||||
if regex_rule.startswith(f"{SUBDOMAIN_WILDCARD_PREFIX}."):
|
||||
return regex_rule.replace(SUBDOMAIN_WILDCARD_PREFIX, "*", 1)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_traefik_routes(traefik_host: str, traefik_route: str):
|
||||
|
@ -44,19 +54,16 @@ def get_traefik_routes(traefik_host: str, traefik_route: str):
|
|||
routes = set()
|
||||
|
||||
for router in api_response:
|
||||
hosts = parse_traefik_rule(router["rule"])
|
||||
host = parse_traefik_rule(router["rule"])
|
||||
|
||||
if not hosts:
|
||||
if not host:
|
||||
print(f"Failed to find host for {router['rule']}", file=sys.stderr)
|
||||
continue
|
||||
|
||||
if len(hosts) > 1:
|
||||
print(f"WARNING: Found multiple hosts for rule: {router['rule']}", file=sys.stderr)
|
||||
|
||||
routes.add(Route(
|
||||
router["service"],
|
||||
traefik_route,
|
||||
hosts[0]
|
||||
host
|
||||
))
|
||||
|
||||
return routes
|
||||
|
|
Reference in New Issue