diff --git a/README.md b/README.md index cec4351..b86c3b6 100644 --- a/README.md +++ b/README.md @@ -511,7 +511,7 @@ The namespace and storage behaviour are controlled by these variables: - **Description:** Default size of the persistent volume claim that HaRP creates for each ExApp. - **Default:** `10Gi` - **`HP_K8S_HOST_ALIASES`** - - **Description:** Additional host aliases set on the ExApp pods, as a comma-separated list of `hostname:ip` pairs, e.g. `nextcloud.example.com:10.0.0.5`. Useful when your Nextcloud domain is not resolvable by the cluster DNS. + - **Description:** Additional host aliases set on the ExApp pods, as a comma-separated list of `hostname:ip` pairs, e.g. `nextcloud.example.com:10.0.0.5`. Useful when your Nextcloud domain is not resolvable by the cluster DNS. An IPv6 address may be written with or without brackets, e.g. `nextcloud.example.com:[fd00::5]`. - **Default:** empty How HaRP reaches the API server: diff --git a/haproxy_agent.py b/haproxy_agent.py index 0cdf4c8..2b05210 100644 --- a/haproxy_agent.py +++ b/haproxy_agent.py @@ -2833,9 +2833,10 @@ def _k8s_parse_host_aliases() -> list[dict[str, Any]]: entry = entry.strip() if not entry or ":" not in entry: continue - hostname, ip_addr = entry.rsplit(":", 1) + # A host name has no ':', so the address is everything after the first one + hostname, ip_addr = entry.split(":", 1) hostname = hostname.strip() - ip_addr = ip_addr.strip() + ip_addr = ip_addr.strip().removeprefix("[").removesuffix("]") if hostname and ip_addr: ip_to_hosts.setdefault(ip_addr, []).append(hostname) diff --git a/tests/unit/test_requests.py b/tests/unit/test_requests.py index 82ff6a0..f6fb2b9 100644 --- a/tests/unit/test_requests.py +++ b/tests/unit/test_requests.py @@ -674,6 +674,25 @@ def test_in_cluster_api_server_url(monkeypatch, host, url): assert agent._in_cluster_api_server() == url +@pytest.mark.parametrize( + ("value", "aliases"), + [ + ("nc.example.com:10.0.0.5", [{"ip": "10.0.0.5", "hostnames": ["nc.example.com"]}]), + ("nc.example.com:fd00::5", [{"ip": "fd00::5", "hostnames": ["nc.example.com"]}]), + ( + "nc.example.com:[fd00::5], office.example.com:fd00::5,nc.example.com:10.0.0.5", + [ + {"ip": "fd00::5", "hostnames": ["nc.example.com", "office.example.com"]}, + {"ip": "10.0.0.5", "hostnames": ["nc.example.com"]}, + ], + ), + ], +) +def test_k8s_host_aliases(monkeypatch, value, aliases): + monkeypatch.setattr(agent, "K8S_HOST_ALIASES_RAW", value) + assert agent._k8s_parse_host_aliases() == aliases + + def test_k8s_service_lookup_works_for_any_app_id(monkeypatch): calls = []