Skip to content

feat(k8s): the LiteLLM gateway keeps virtual keys in an in-cluster Postgres, so devops-cli can mint scoped, expiring gateway keys #1635

Description

@dan-petty

Friction

Every client of the LLM gateway holds its master key. The master key is LiteLLM's proxy admin key: whoever holds it can mint and delete keys and call every model without limit. The cluster Jobs get it as DEVOPS_CLI_AI_API_KEY. An agent session can only get a gateway credential by a person pasting the master key, which breaks the owner's rule that agents never handle credential values.

LiteLLM's virtual keys solve this. A virtual key has an expiry, request and token limits, a parallel-request cap, a model list, an alias and metadata, and it can be revoked on its own. But LiteLLM keeps virtual keys only in a Postgres database, reached through DATABASE_URL, and the gateway has none. Without it, #1154's agent grant (devops agent env) cannot be built, and no cluster workload can move off the master key.

Evidence

Checked against main at 3dd7fa6:

  • The gateway's general_settings hold only master_key, cancel_on_disconnect, store_model_in_db: false and proxy_batch_write_at (k8s/llm/gateway/configmap.yaml:187-191). There is no database setting.
  • The Deployment's environment is LITELLM_MASTER_KEY, LITELLM_LOCAL_MODEL_COST_MAP, LITELLM_TELEMETRY, LITELLM_REDIS_HOST and LITELLM_REDIS_PORT, with no DATABASE_URL (k8s/llm/gateway/deployment.yaml:47-63).
  • The container runs with readOnlyRootFilesystem: true (deployment.yaml:36), and only /tmp is writable, as an emptyDir (:72-73, :98-99). The image is pinned to LiteLLM v1.103.0 (:32), and the Deployment runs one replica (:12).
  • The gateway's NetworkPolicy allows egress only to the Ollama pods on 11434, Valkey on 6379 and DNS (k8s/llm/gateway/networkpolicy.yaml:27-52). Its peers are pod selectors, never ipBlock (:25-26).
  • No manifest under k8s/ runs Postgres.
  • The cluster Secret table, CLUSTER_SECRETS (src/devops_cli/k8s/cluster_secrets.py:161), generates the master key for llm/llm-gateway-secrets (:162-180). It also copies the key into devops/devops-cli as DEVOPS_CLI_AI_API_KEY for every cluster Job (:223-248, the key at :230).
  • Every Secret in that table must be an ignored orphan in the homelab Argo CD project (tests/test_k8s_argocd_apps.py:177-211). The llm stack's manifests are listed in _MANIFESTS_BY_STACK["llm"] (src/devops_cli/commands/k8s/stack_lifecycle.py:181-190) and k8s/llm/kustomization.yaml.
  • LiteLLM's documentation, as recorded in feat(agent): devops agent env gives an agent session its own expiring gateway key, run-index access and a read-only kube identity, written to its settings file or a child process and never printed, with --revoke and an audit of every grant #1154 on 2026-10-04:
    • Virtual keys (/key/generate) need DATABASE_URL pointing at Postgres.
    • The proxy runs prisma migrate deploy at start-up. Under a read-only root filesystem, its production guide sets LITELLM_MIGRATION_DIR to a writable directory.
    • LITELLM_SALT_KEY is set before the first key is stored, and it is never changed afterwards.
    • /health/readiness reports the database's state.
    • general_settings.allow_requests_on_db_unavailable chooses what happens when the database is down.

Remedy

The gateway gets its own single-instance Postgres in llm, under GitOps and deploy-stack:

  • Database. Run the official postgres image at a pinned tag, as a StatefulSet llm-gateway-db with one replica and a Service on 5432.
    • Use the restricted security context the gateway uses: non-root, RuntimeDefault seccomp, no capabilities, and a read-only root filesystem with emptyDirs for the socket and temporary directories.
    • Data lives on a local-path claim marked Prune=false,Delete=false.
    • The manifests join k8s/llm/kustomization.yaml (Argo CD's llm Application) and _MANIFESTS_BY_STACK["llm"], ahead of the gateway's Deployment.
  • Secrets. Add a new CLUSTER_SECRETS entry, llm/llm-gateway-db, with a generated password and a generated salt-key, both kept in the keyring and adopted like the master key.
    • Postgres reads POSTGRES_PASSWORD from password.
    • The gateway builds DATABASE_URL from the same key with Kubernetes $(VAR) interpolation, so no full URL is stored anywhere. It reads LITELLM_SALT_KEY from salt-key.
    • push-secrets --rotate never rotates salt-key, and the table's change note says why.
    • The orphan-ignore test then requires llm-gateway-db in the homelab project's list.
  • Network. The gateway's policy gains one egress rule to the database pods on 5432. The database gets its own perimeter, admitting only the gateway's pods on 5432 and allowing no egress except DNS. Both use pod-selector peers.
  • Gateway. Add LITELLM_MIGRATION_DIR on an emptyDir, and keep readOnlyRootFilesystem: true. The PR checks whether the pinned image writes anywhere else at start-up and mounts only what it must.
    • allow_requests_on_db_unavailable stays at LiteLLM's default (false). The gateway is reachable from outside the cluster, so a key it cannot check is refused.
    • store_model_in_db stays false, so the models stay in git.
  • Backups. Per the owner's rule that PVC data has backups, add a daily pg_dump CronJob from the same image into its own claim, keeping 7 dumps. k8s/README.md gets a restore section.
  • Tests (in the gate, no cluster):
    • the database manifests' security context, claim protection and perimeter, in both directions;
    • the new Secret table rows, their generators, and that salt-key is never rotated;
    • the manifest order in _MANIFESTS_BY_STACK["llm"];
    • that the gateway Deployment sets DATABASE_URL, LITELLM_SALT_KEY and LITELLM_MIGRATION_DIR without any literal value.
  • Pending a person, after the sync:
    • the gateway's /health/readiness reports a connected database;
    • a person mints a one-hour test key with the master key from their keyring, calls one model with it, deletes it, and sees the next call answer 401;
    • the database claim is Bound;
    • one backup file exists after the first CronJob run.

Out of scope: the agent grant command (#1154's follow-up item), switching the cluster Jobs' DEVOPS_CLI_AI_API_KEY to a virtual key, and Open WebUI's connection, which keeps its own stored key.

Unblocks: the agent grant item drafted with #1154, and a later item that gives the cluster Jobs a virtual key instead of the master key.

Related: #1154, #741 (the Secret table), #755 (GitOps), #1562 (roadmap-service suspended, so a person files this item).

Delivery

One pull request delivers this issue (see the answer in the comments). It differs from the Remedy above in these points:

  • Database. A CloudNativePG Cluster llm-gateway-db in llm, in the form of the Mattermost database, not a hand-written StatefulSet. It has one instance on local-path (Prune=false,Delete=false) and its own perimeter.
  • Secrets. The operator creates Secret llm-gateway-db-app at run time. There is no CLUSTER_SECRETS row, no generated password and no push. The gateway reads DATABASE_URL from its uri key.
  • Salt key. No LITELLM_SALT_KEY: with store_model_in_db: false the database holds nothing that LiteLLM encrypts, and a virtual key is a SHA-256 hash. The README states when to set one.
  • Network. As in the Remedy, with one addition: the database also reaches the Kubernetes API, which the instance manager needs.
  • Gateway. As in the Remedy, plus a startupProbe so that the liveness probe does not kill a pod that migrates.
  • Order. The cloudnative-pg release moves to stack infra. Stack llm applies the database after the Helm releases.
  • Moved: backups (the daily pg_dump CronJob and the restore section) to dan-petty/homelab#332. The backup must go off the node, and feat(k8s): Add scheduled off-node backup and restore for Gitea #2144 chooses the target for Gitea.

Proposed design

Problem statement

The LiteLLM gateway uses a master key for all clients. This breaks the security rule that agents should never handle credential values. Virtual keys in LiteLLM require a Postgres database, but the gateway has no database connection.

Acceptance criteria

  • Criterion 1: A new Postgres StatefulSet is created for the gateway with proper security context and data persistence
    Verification: The k8s/llm/gateway-db/statefulset.yaml manifest exists with restricted security context, local-path PVC with Prune=false,Delete=false, and one replica
  • Criterion 2: The gateway Deployment is updated to include DATABASE_URL, LITELLM_SALT_KEY, and LITELLM_MIGRATION_DIR environment variables
    Verification: The k8s/llm/gateway/deployment.yaml includes env vars LITELLM_MIGRATION_DIR, DATABASE_URL (from secret interpolation), and LITELLM_SALT_KEY (from secret)
  • Criterion 3: A new cluster Secret is added for the gateway database with generated password and salt-key
    Verification: src/devops_cli/k8s/cluster_secrets.py includes a new CLUSTER_SECRETS entry for llm/llm-gateway-db with password and salt-key generators
  • Criterion 4: The gateway's NetworkPolicy allows egress to the database pods on port 5432
    Verification: k8s/llm/gateway/networkpolicy.yaml includes an egress rule to podSelector matching 'app.kubernetes.io/name: llm-gateway-db' on port 5432
  • Criterion 5: The database's NetworkPolicy only allows ingress from the gateway pods and egress to DNS
    Verification: k8s/llm/gateway-db/networkpolicy.yaml includes ingress rule from gateway podSelector and egress rule only to kube-system namespace on DNS ports
  • Criterion 6: The manifest order in _MANIFESTS_BY_STACK["llm"] places the database before the gateway deployment
    Verification: src/devops_cli/commands/k8s/stack_lifecycle.py shows llm-gateway-db resources listed before llm-gateway deployment in the "llm" stack manifest list

Key questions

  • What is the required Postgres image tag and version?: The issue description does not specify a particular Postgres version, but it references that the gateway uses LiteLLM v1.103.0 and suggests using a pinned tag for consistency with other components. [sources: k8s/llm/gateway/deployment.yaml:32]
  • How should the LITELLM_SALT_KEY be handled to ensure it's never rotated?: The salt-key should be generated once and kept in the keyring, with a note explaining why it's never rotated to prevent breaking existing virtual keys. (proposed) [sources: k8s/llm/gateway/deployment.yaml:32]
  • What is the expected database URL format for LiteLLM?: The DATABASE_URL should be constructed using Kubernetes $(VAR) interpolation to avoid storing full URLs in manifests, with the format postgresql://:@host`:port/database. [sources: k8s/llm/gateway/deployment.yaml:32]
  • How should the gateway's readiness probe respond when the database is unavailable?: The gateway should refuse requests when the database is down, since it's reachable from outside the cluster, and allow_requests_on_db_unavailable should stay at LiteLLM's default (false). (proposed) [sources: k8s/llm/gateway/deployment.yaml:32]

Fit

Needs splitting into 5 items:

  1. Create the Postgres StatefulSet for the gateway
  2. Update cluster secrets table with database credentials
  3. Modify gateway Deployment to include database connection variables
  4. Update network policies for database connectivity
  5. Add database manifests to stack lifecycle order

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-01: not-applicable.
Tool: none found.

Activity

  1. added
    source/agent
    type/feature
    on Oct 18, 2026
  2. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Type: type/feature. The candidate proposes adding a new feature to support scoped, expiring gateway keys using an in-cluster Postgres database.
    • Priority: P1-High. This feature is required to enable the agent grant functionality described in issue dan-petty/devops-cli#1154 and to move off the master key for cluster workloads.
    • Value: High. This change enables secure credential management for LLM gateway access, supporting agent automation and improving security posture.
    • Effort: High. The implementation requires setting up a new Postgres database, integrating it with the gateway, managing secrets, and updating deployment configurations.
  3. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    Proposal needs splitting into 5 items at 25e2cda (see proposed design in issue body).

  4. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

    The proposal at 085ca36 needs splitting into 5 items. A person splits the item into the items below, or replies in a comment or edits the body to keep one item, and the next refine round looks at the item again.

    1. Create the Postgres StatefulSet for the gateway
    2. Update cluster secrets table with database credentials
    3. Modify gateway Deployment to include database connection variables
    4. Update network policies for database connectivity
    5. Add database manifests to stack lifecycle order
  5. added
    priority/p1-high
    status/new
    effort/high
    value/high
    scope/k8s
    on Oct 28, 2026
  6. devops-cli-service commented on Nov 1, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Tool: none found

    Checked on 2026-11-01. A person decides what this means for the item. The check changes no label and closes no issue.

  7. added
    owner/agent
    on Nov 6, 2026
  8. dan-petty commented on Nov 6, 2026

    @dan-petty
    OwnerAuthor

    Answer from project standards, under the owner's instruction of 2026-11-05.

    Question 1: the refine round says to split this item in 5. Is that right?
    Answer: No. It is one pull request. The 5 parts (database, secrets, gateway variables, network policies, stack order) are one change: none works alone, and they edit the same files. A split would give 5 pull requests that each leave the gateway in a state that does not work.
    Rule: AGENTS.md rule 1 (one issue, one pull request).

    Question 2: StatefulSet and a generated password in CLUSTER_SECRETS, or something else?
    Answer: A CloudNativePG Cluster. The cluster already runs the operator for Mattermost (k8s/mattermost/database.yaml, k8s/argocd/apps/cloudnative-pg.yaml). A Cluster that names no bootstrap.initdb.secret makes the operator create the user, the database and Secret llm-gateway-db-app at run time, with a generated password and a ready uri key (CloudNativePG documents, applications.md, "Secrets"). The gateway reads DATABASE_URL from that key. So no secret is in git, CLUSTER_SECRETS gets no row, and no person pushes a value.
    Rule: AGENTS.md rule 3 (use an open tool before you build), k8s/README.md (the form of the existing database), SECURITY.md (no secret in git).

    Question 3: is LITELLM_SALT_KEY needed?
    Answer: Not now. LiteLLM uses it only to encrypt values that it stores in the database, such as provider keys of models added at run time, and falls back to the master key. A virtual key is stored as a SHA-256 hash and does not use it (LiteLLM source at v1.103.0, encrypt_decrypt_utils.py and hash_token). The gateway keeps store_model_in_db: false, so the database holds nothing to encrypt. A generated salt key would add a keyring row and a person's push before the gateway can start. k8s/README.md states the rule: set a salt key before the first change that stores a credential in the database.
    Rule: AGENTS.md rule 3 (use an open tool before you build): LiteLLM already has a fallback for this case.

    Question 4: no egress except DNS for the database?
    Answer: The database also needs the Kubernetes API. The instance manager calls the API server (CloudNativePG documents, security.md). The policy uses the form of mattermost-db-api. It admits only the gateway and the operator on 5432.

    Question 5: the backup CronJob.
    Answer: Moved to dan-petty/homelab#332. A dump into a claim of the same node is not a backup off the node, and #2144 is choosing the target for Gitea, so the cluster gets one backup design.
    Rule: the rule in this issue that PVC data has backups, and the partial-delivery rule (move a separate criterion to its own issue).

    Rollout (the merge applies at once through Argo CD). The gateway has no downtime, and no person plans a window. Argo CD creates the policy and the database, and starts a new gateway pod beside the old one (one replica: maxSurge 1, maxUnavailable 0). The new pod waits for the Secret, restarts a few times until the database accepts connections, runs the migrations on the empty database, and passes its readiness probe. A startupProbe gives it ten minutes so that the liveness probe does not kill it during the migration. Then Kubernetes stops the old pod. The old pod answers every request until then. The change adds no push-secrets step, and no person runs a command.

    What only a person can do: merge, and after the sync check /health/readiness of the gateway and mint, use and delete one test key (these steps are in the change's tasks.md).

  9. added
    status/in-progress
    status/in-review
    and removed
    status/new
    status/in-progress
    on Nov 6, 2026
  10. removed
    status/in-review
    owner/agent
    on Nov 6, 2026
  11. added a commit that references this issue on Nov 6, 2026
  12. devops-cli-service commented on Nov 6, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-06 07:13 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/highpriority/p1-highscope/k8ssource/agenttype/featurevalue/high

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions