Startup Checks

The self-checks that a Smarter server runs when it starts.

The settings check the AWS credentials when every process starts (aws_credentials_work()), and warn at once if they don’t work. The checks here do the same for the rest of what a server depends on: the Kubernetes cluster, the database, the Redis cache and the Celery broker, and, in the tool worker, the isolation that ADR-038 promises. A broken dependency is then in a pod’s first log lines, rather than found at the first request, the first deploy or the next run of a periodic task. A check that passes logs one INFO line, one that fails a WARNING that says what to check, or an ERROR when the tool worker can reach what it must not.

run_startup_checks() runs one of the lists below, once per server, and never raises, so that no check can keep a server from starting. It is called from smarter.asgi, in the first of uvicorn’s workers, with WEB_SERVER_CHECKS, and from each Celery worker’s main process, through Celery’s worker_init signal (smarter.lib.celery_conf), with WORKER_CHECKS, or TOOL_WORKER_CHECKS in the tool worker. Celery Beat, manage.py commands, the Docker build and the unit tests don’t check: an AppConfig.ready() would run the checks in all of them.

smarter.apps.infrastructure.startup.TOOL_WORKER_CHECKS: tuple[Callable[[], bool], ...] = (<function check_tool_worker_isolation>,)

The tool worker’s checks.

It has no database, and its pods never reach Kubernetes.

(<function check_tool_worker_isolation at 0x1237ccb40>, )
smarter.apps.infrastructure.startup.WEB_SERVER_CHECKS: tuple[Callable[[], bool], ...] = (<function check_database>, <function check_cache>, <function check_broker>, <function check_kubernetes>)

The web server’s checks.

(
    <function check_database at 0x1237cea30>,
    <function check_cache at 0x1237cf8a0>,
    <function check_broker at 0x1237cfb60>,
    <function check_kubernetes at 0x1237cc7d0>,
)
smarter.apps.infrastructure.startup.WORKER_CHECKS: tuple[Callable[[], bool], ...] = (<function check_database>, <function check_cache>, <function check_kubernetes>)

A Celery worker’s checks.

Celery checks its own broker.

(
    <function check_database at 0x1237cea30>,
    <function check_cache at 0x1237cf8a0>,
    <function check_kubernetes at 0x1237cc7d0>,
)
smarter.apps.infrastructure.startup.can_connect(host, port, timeout=2)[source]

Whether a TCP connection to a host’s port succeeds.

Parameters:
  • host (str) – The host name or address.

  • port (int) – The port.

  • timeout (float) – Seconds to wait.

Return type:

bool

Returns:

True if it connected. A network policy that drops the connection makes this wait the whole timeout, then return False.

smarter.apps.infrastructure.startup.check_broker()[source]

Check that the Celery broker accepts a connection, so that the web server can send tasks.

The workers don’t run this check: Celery connects to the broker as a worker starts, and logs it.

Return type:

bool

Returns:

True if it connected.

smarter.apps.infrastructure.startup.check_cache()[source]

Check that the Redis cache stores a value and returns it.

Return type:

bool

Returns:

True if it returned the value.

smarter.apps.infrastructure.startup.check_database()[source]

Check that the platform’s database answers a query.

The connection is closed afterwards, so that a Celery worker’s pool processes, which fork after this, don’t share it.

Return type:

bool

Returns:

True if it answered.

smarter.apps.infrastructure.startup.check_kubernetes()[source]

Check that kubectl reaches the cluster, and that the environment’s namespace exists.

Return type:

bool

Returns:

True if the cluster is ready.

smarter.apps.infrastructure.startup.check_tool_worker_isolation()[source]

Confirm, one by one, that the tool worker has none of the platform’s credentials, and can’t reach the cluster’s private addresses.

ADR-038 promises that the tool calls, whose code and requests manifest authors, extension authors and the LLM choose, run where there is nothing to steal. Nothing else verifies it: a network policy that is weakened, or a credential that is added to the tool worker’s Secret, leaves every tool call working. So each capability that was removed is tested, and its result logged on a line of its own: INFO that confirms it is absent, or an ERROR when it is present. A last line sums them up.

It runs only in a pod. In docker-compose the tool worker shares .env, and its credentials, with the other services, and there are no network policies, so nothing there could pass.

Return type:

bool

Returns:

True if every removed capability is confirmed absent, or the tool worker isn’t in a pod.

smarter.apps.infrastructure.startup.credential_variables()[source]

Name the environment variables that hold credentials which the tool worker must never have.

Return type:

list[str]

Returns:

Their names, sorted, never their values: those in TOOL_WORKER_FORBIDDEN_VARIABLES, and those whose name has a part of TOOL_WORKER_FORBIDDEN_NAME_PARTS.

smarter.apps.infrastructure.startup.error_description(error)[source]

Describe an error by its type, and its numeric code if it has one, never its message.

A message can hold a cluster’s arn, a host name or a user name. A code is enough to tell “access denied” (MySQL’s 1045) from “can’t connect” (2003).

Parameters:

error (Exception) – The error.

Return type:

str

Returns:

e.g. OperationalError 2003, or RuntimeError.

smarter.apps.infrastructure.startup.kubernetes_credentials()[source]

Describe what kubectl authenticates with.

Return type:

str

Returns:

the pod's ServiceAccount in a pod that has a ServiceAccount token (see in_cluster_server()), otherwise the kubeconfig.

smarter.apps.infrastructure.startup.kubernetes_problem(configured, credentials, namespace)[source]

Explain why kubectl’s Kubernetes service isn’t ready, and what to check.

Parameters:
  • configured (bool) – Whether kubectl is configured for the cluster (configured).

  • credentials (str) – What kubectl authenticates with, from kubernetes_credentials().

  • namespace (str) – The environment’s namespace.

Return type:

str

Returns:

The explanation.

smarter.apps.infrastructure.startup.run_startup_checks(checks)[source]

Run self-checks once per server, each of which logs its own result.

An error in a check is logged by its type, and counts as a failure, so that no check can keep the server from starting.

Parameters:

checks (Sequence[Callable[[], bool]]) – The checks, e.g. WEB_SERVER_CHECKS.

Return type:

dict[str, bool]

Returns:

Whether each check passed, by its name; empty in the unit tests, and in every worker of a server but the first.

smarter.apps.infrastructure.startup.tool_worker_forbidden_targets()[source]

Name what the tool worker must not reach: the instance metadata service, and the Kubernetes api.

Both are in the private ranges that smarter-infrastructure’s smarter-tools-egress network policy denies. MariaDB’s port is not among them: SQL plugins may use it.

Return type:

list[tuple[str, str, int]]

Returns:

Each target’s description, host and port.