diff --git a/create-nginx-map.py b/create-nginx-map.py index 46bdcaa..8403447 100644 --- a/create-nginx-map.py +++ b/create-nginx-map.py @@ -1,6 +1,7 @@ import requests from typing import NamedTuple import re +import sys TRAEFIK_HOST_RE = re.compile(r"Host\(`([a-z0-9\.-]+)`\)") @@ -10,18 +11,26 @@ class Route(NamedTuple): hostname: str +def parse_traefik_rule(rule: str) -> list[str]: + if "Host(" not in rule: + return [] + + return TRAEFIK_HOST_RE.findall(rule) + + def get_traefik_routes(traefik_host: str, traefik_route: str): api_response = requests.get(f"{traefik_host}/api/http/routers").json() routes = set() for router in api_response: - if "Host(" not in router["rule"]: - continue - - hosts = TRAEFIK_HOST_RE.findall(router["rule"]) + hosts = parse_traefik_rule(router["rule"]) if not hosts: - raise ValueError(f"Failed to find host for {router['rule']}") + 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"],