From e8a8cd1dce3512d2d73bf2f408fdee2242264bfb Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 24 Aug 2023 21:49:26 +0100 Subject: [PATCH] Extract traefik rule parsing to separate method --- create-nginx-map.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) 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"],