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