From d167da9d634193ed3de7d101d41021dc65de1eca Mon Sep 17 00:00:00 2001 From: Andi Powers-Holmes Date: Wed, 7 Jan 2026 21:48:35 +1100 Subject: [PATCH] Fix external network name parsing (#152) * fix: external network name parsing Compose network definitions may have a "name" field defining the actual network name, which may differ from the key used in the compose file e.g. when overriding the default compose network, or using a network name containing special characters that are not valid YAML keys. Fix: check for "name" field on definition and use that if present, else fall back to key. * tests: Add test for external network name field parsing Covers the case where a network definition has a "name" field that differs from the YAML key (e.g., default key with name: compose-net). --------- Co-authored-by: Bas Nijholt --- src/compose_farm/compose.py | 5 ++++- tests/test_traefik.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/compose_farm/compose.py b/src/compose_farm/compose.py index e390b2d..ae84894 100644 --- a/src/compose_farm/compose.py +++ b/src/compose_farm/compose.py @@ -280,8 +280,11 @@ def parse_external_networks(config: Config, stack: str) -> list[str]: return [] external_networks: list[str] = [] - for name, definition in networks.items(): + for key, definition in networks.items(): if isinstance(definition, dict) and definition.get("external") is True: + # Networks may have a "name" field, which may differ from the key. + # Use it if present, else fall back to the key. + name = str(definition.get("name", key)) external_networks.append(name) return external_networks diff --git a/tests/test_traefik.py b/tests/test_traefik.py index ae725f1..aa3277c 100644 --- a/tests/test_traefik.py +++ b/tests/test_traefik.py @@ -338,6 +338,26 @@ def test_parse_external_networks_missing_compose(tmp_path: Path) -> None: assert networks == [] +def test_parse_external_networks_with_name_field(tmp_path: Path) -> None: + """Network with 'name' field uses actual name, not key.""" + cfg = Config( + compose_dir=tmp_path, + hosts={"host1": Host(address="192.168.1.10")}, + stacks={"app": "host1"}, + ) + compose_path = tmp_path / "app" / "compose.yaml" + _write_compose( + compose_path, + { + "services": {"app": {"image": "nginx"}}, + "networks": {"default": {"name": "compose-net", "external": True}}, + }, + ) + + networks = parse_external_networks(cfg, "app") + assert networks == ["compose-net"] + + class TestExtractWebsiteUrls: """Test extract_website_urls function."""