Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions deploy/k8s-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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://<cluster-ip>:8000) that collide
# with the CLOCKD_* config env prefix and break startup.
enableServiceLinks: false
securityContext:
runAsNonRoot: true
seccompProfile:
Expand Down
27 changes: 26 additions & 1 deletion src/clockd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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://<cluster-ip>: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:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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://<ip>:<port> 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
Loading