feat: enhance service discovery and proxy management

This commit is contained in:
gabdevele
2026-05-05 13:57:46 +02:00 Unverified
parent 93ba8e9ac2
commit a523c2faa3
53 changed files with 3206 additions and 706 deletions
+37 -15
View File
@@ -14,48 +14,64 @@ def get_random_port() -> int:
raise RuntimeError(f"Could not find an available port: {e}") from e
def _get_service_port(mapping: str | int | Mapping[str, object]) -> int | None:
def _get_service_binding(
mapping: str | int | Mapping[str, object],
) -> tuple[int | None, str | None]:
if isinstance(mapping, int):
return mapping
return mapping, None
if isinstance(mapping, Mapping):
published = mapping.get("published")
if published is None:
return None
return None, None
try:
return int(published)
except TypeError, ValueError:
return None
port = int(published)
except TypeError:
return None, None
except ValueError:
return None, None
host = mapping.get("host_ip")
return port, str(host) if host not in {None, ""} else None
parts = str(mapping).split(":")
match parts:
case [""]:
return None
return None, None
case [port] | [port, _]:
try:
return int(port)
return int(port), None
except ValueError:
return None
case ["localhost" | "127.0.0.1", _, _]:
return None
case [_, port, _]:
return None, None
case [host, port, _]:
try:
return int(port)
return int(port), host or None
except ValueError:
return None
return None, None
case _:
return None
return None, None
def _get_service_port(mapping: str | int | Mapping[str, object]) -> int | None:
port, _host = _get_service_binding(mapping)
return port
def _replace_port_in_mapping(
mapping: str | int | Mapping[str, object],
port_from: int,
port_to: int,
host_to: str | None = None,
rewrite_host: bool = False,
) -> str | dict[str, object]:
if isinstance(mapping, Mapping):
updated = dict(mapping)
if _get_service_port(updated) == port_from:
updated["published"] = str(port_to)
if rewrite_host:
if host_to is None:
updated.pop("host_ip", None)
else:
updated["host_ip"] = host_to
return updated
parts = str(mapping).split(":")
@@ -63,8 +79,14 @@ def _replace_port_in_mapping(
case [_]:
return str(port_to)
case [_, container_port]:
if rewrite_host and host_to:
return f"{host_to}:{port_to}:{container_port}"
return f"{port_to}:{container_port}"
case [ip, _, container_port]:
if rewrite_host:
if host_to:
return f"{host_to}:{port_to}:{container_port}"
return f"{port_to}:{container_port}"
return f"{ip}:{port_to}:{container_port}"
case _:
return str(port_to)