From bc5f1f78703a42b9e92c89e437c3de723fc341c5 Mon Sep 17 00:00:00 2001 From: Nathan V Date: Mon, 13 Jul 2026 17:09:31 -0700 Subject: [PATCH] Ignore Kubernetes service-link CLOCKD_PORT env var A Service named "clockd" makes Kubernetes inject legacy Docker-link vars (CLOCKD_PORT=tcp://:8000) into every pod in the namespace. Since env vars now override YAML config, this collided with the CLOCKD_ prefix and crash-looped the pod at startup with a port validation error. - Filter tcp:// values for the port field out of the env settings source - Set enableServiceLinks: false in the example k8s manifest - Add regression tests; real integer CLOCKD_PORT values still override --- deploy/k8s-example.yaml | 4 ++++ src/clockd/config.py | 27 ++++++++++++++++++++++++++- tests/test_config.py | 24 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/deploy/k8s-example.yaml b/deploy/k8s-example.yaml index 920147b..439ea1d 100644 --- a/deploy/k8s-example.yaml +++ b/deploy/k8s-example.yaml @@ -96,6 +96,10 @@ spec: labels: app: clockd spec: + # Without this, Kubernetes injects legacy service-link env vars for the + # "clockd" Service (CLOCKD_PORT=tcp://:8000) that collide + # with the CLOCKD_* config env prefix and break startup. + enableServiceLinks: false securityContext: runAsNonRoot: true seccompProfile: diff --git a/src/clockd/config.py b/src/clockd/config.py index b4324ce..8b74de3 100644 --- a/src/clockd/config.py +++ b/src/clockd/config.py @@ -222,7 +222,12 @@ def settings_customise_sources( # at deploy time (e.g. from Kubernetes Secrets) without living in the # config file. Nested fields use "__", e.g. # CLOCKD_EVENT_SOURCES__HOME_NVR__UNIFI__PASSWORD. - return (env_settings, init_settings, dotenv_settings, file_secret_settings) + return ( + _ignore_k8s_service_links(env_settings), + init_settings, + dotenv_settings, + file_secret_settings, + ) host: str = "0.0.0.0" port: int = 8000 @@ -252,6 +257,26 @@ def validate_model(cls, v: str) -> str: event_sources: dict[str, UnifiEventSourceConfig] = {} +def _ignore_k8s_service_links(env_source): + """Wrap the env settings source to drop Kubernetes service-link values. + + A Service named "clockd" makes Kubernetes inject legacy Docker-link vars + like CLOCKD_PORT=tcp://:8000 into every pod in the namespace, + which collides with the CLOCKD_ env prefix and would fail port validation + at startup. Prefer enableServiceLinks: false in the pod spec; this guard + keeps startup working either way. + """ + + def _filtered() -> dict: + values = env_source() + port = values.get("port") + if isinstance(port, str) and port.startswith("tcp://"): + values.pop("port") + return values + + return _filtered + + def load_server_config(path: str = "configs/server.yaml") -> ServerConfig: if os.path.exists(path): with open(path) as f: diff --git a/tests/test_config.py b/tests/test_config.py index c8372b2..0831965 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -77,3 +77,27 @@ def test_nested_env_secrets_merge_with_yaml(tmp_path, monkeypatch): assert cfg.metrics.influxdb_v2.url == "http://influx:8086" assert cfg.event_sources["home_nvr"].enabled is True assert cfg.event_sources["home_nvr"].unifi.host == "10.0.0.1" + + +def test_k8s_service_link_port_ignored(tmp_path, monkeypatch): + # Kubernetes injects CLOCKD_PORT=tcp://: when a Service named + # "clockd" shares the namespace; it must not clobber the YAML port. + path = tmp_path / "server.yaml" + path.write_text(yaml.dump({"port": 9000})) + monkeypatch.setenv("CLOCKD_PORT", "tcp://10.108.181.189:8000") + cfg = load_server_config(str(path)) + assert cfg.port == 9000 + + +def test_k8s_service_link_port_ignored_without_yaml(tmp_path, monkeypatch): + monkeypatch.setenv("CLOCKD_PORT", "tcp://10.108.181.189:8000") + cfg = load_server_config(str(tmp_path / "missing.yaml")) + assert cfg.port == 8000 + + +def test_real_env_port_still_wins(tmp_path, monkeypatch): + path = tmp_path / "server.yaml" + path.write_text(yaml.dump({"port": 9000})) + monkeypatch.setenv("CLOCKD_PORT", "9100") + cfg = load_server_config(str(path)) + assert cfg.port == 9100