Upgrades and migrations#

Move the gateway to a new release with its data intact, and check it before clients return.

Shell
helm upgrade tokedex ./helm/tokedex -n tokedex -f release-values.yaml \
  --set replicaCount=0 --set autoscaling.enabled=false --wait --timeout 10m &&
kubectl -n tokedex wait --for=delete pod --timeout=10m \
  -l app.kubernetes.io/instance=tokedex,app.kubernetes.io/component=gateway

After both commands succeed, take a complete backup. The next commands run the migration and start the new release.

Shell
(
  set -o pipefail
  for action in apply check; do
    migration=$(helm template tokedex ./helm/tokedex -n tokedex -f next-values.yaml \
      --set replicaCount=0 --set autoscaling.enabled=false --set "migrationJob.action=$action" \
      --show-only templates/migration-job.yaml | kubectl -n tokedex create -f - -o name) || exit 1
    deadline=$((SECONDS + 600))
    while :; do
      conditions=$(kubectl -n tokedex get "$migration" --request-timeout=10s \
        -o jsonpath='{range .status.conditions[?(@.status=="True")]}{.type}{" "}{end}') || exit 1
      if [[ " $conditions " == *" Failed "* ]] || (( SECONDS >= deadline )); then
        kubectl -n tokedex logs "$migration" || true
        exit 1
      fi
      [[ " $conditions " == *" Complete "* ]] && break
      sleep 2
    done
    kubectl -n tokedex logs "$migration" || exit 1
  done
  helm upgrade tokedex ./helm/tokedex -n tokedex -f next-values.yaml --wait --timeout 10m || exit 1
  helm test tokedex -n tokedex
)

The second Job's log ends with no migration pending:

JSON
{
  "schemaVersion": "tokedex-gateway.admin.v1",
  "command": "migrate",
  "data": {
    "migrations": {
      "mode": "check",
      "applied": ["0001_state_tables", "…", "alerts/0003_model_budgets"],
      "pending": []
    },
    "preview": null
  }
}

release-values.yaml holds the running release's values and next-values.yaml a copy with the new image.tag. Run the commands from the new release's chart. The script starts the new release after both Jobs succeed. It stops and prints the Job log as soon as Kubernetes marks a Job Failed, or after ten minutes without completion. Keep the gateway stopped after a failure. Keep the release name, namespace, Secret references and claim unchanged in next-values.yaml.

You need: kubectl, Helm 3.17 or later, whose built-in Kubernetes version meets the chart's minimum, a release with Postgres and postgresql.migrationMode: check in next-values.yaml, a maintenance window, and a complete backup taken with the release you run now.

Prepare an upgrade#

  1. Read what the new release changes.
  2. Rehearse on a backup restored into a clean place: upgrade it, then test the client APIs and features your applications use, including a streamed response and a follow-up turn.
  3. Block new client traffic before you stop the gateway.

Time the migration Job in your rehearsal, so you know how long a maintenance window to book. Pin image.digest in next-values.yaml as well as the tag, so production runs the exact build you rehearsed.

Database migrations#

postgresql.migrationMode decides what a starting gateway does with pending migrations: auto, the default, applies them, and check refuses to start while one is pending. With several replicas, use check and apply migrations as a step, so no replica of the earlier release runs against a migrated database. Keep check in your values after the upgrade, so the next one is a deliberate step too. The migration Job needs Postgres, replicaCount: 0 and autoscaling off; the chart refuses to render it otherwise.

With auto and one replica on a ReadWriteOnce claim, helm upgrade with next-values.yaml is the whole upgrade: the chart stops the old Pod, and the new one applies the migrations as it starts.

A release without Postgres keeps its state on the claim and runs no migration command. Run helm upgrade with next-values.yaml, then helm test.

What happens to the model catalogue#

Each release carries its own model catalogue. When the new release starts on a store that holds an earlier catalogue, it reviews the change, the same review catalogue preview prints, and adopts the new catalogue if nothing in the review stops it. It saves the replaced catalogue as a backup in the same write, and records a report that the dashboard and catalogue status show until you acknowledge it. The report lists the models added and removed, price changes, routers whose members or ranking changed, and behaviour the release retired.

The gateway stops, not ready, and asks for your review when the change would:

  • turn off one of your aliases, custom routers or other saved entries, including an alias whose source the new catalogue turns off;
  • turn off one of your custom models: the new catalogue now serves a model with that ID, or no longer offers the provider API and key the custom model uses;
  • take a member custom model from one of your custom routers;
  • take a model, or one of its client APIs, from an API key whose model list names that model, counting only the key's allowed providers when it has them;
  • leave a built-in router without members, or with a plan that does not resolve.

It also stops when it cannot compare the stored catalogue with its own, or cannot read the API keys' model lists. A model that the new catalogue removes and that no key's model list names does not stop the upgrade; the review lists it. A key limited only to providers, with no model list, does not stop it either; the review lists each model such a key would lose. The startup log and catalogue status give one reason for each stop. To recover, follow Recover a failed upgrade.

To review every catalogue upgrade yourself, set catalogue.upgrade: review in the Helm chart, or TOKEDEX_CATALOGUE_UPGRADE=review. The gateway then stops on each upgrade, even one its review allows, until you run catalogue apply. It also stops on a stored catalogue it cannot read, until you run catalogue rebuild, instead of rebuilding it as auto does. The default is auto.

In your rehearsal, and again after the upgrade, check the catalogue state. The command is read-only and calls no provider.

Shell
kubectl -n tokedex exec deployment/tokedex -- node /app/dist/cli.js catalogue status --json

data.status.reports lists the upgrade reports, each with its verdict, reasons and the backup that holds the replaced catalogue. data.status.disabled lists the entries that are off, and is [] when there are none. Repair or remove each entry in Models & Routing.

Go back after an adopted upgrade#

An adopted catalogue is written to the store, and the earlier release does not start on it. The replaced catalogue is kept next to it as <catalogue>.replaced-<hash>, and the upgrade report's backup field names it. To go back to the earlier release when the new one applied no database migration, stop the gateway, put the replaced catalogue back, and start the earlier image. If the new release applied a migration, restore the pre-upgrade backup instead.

An earlier release that starts on the adopted store refuses to start. A release that adopts catalogue upgrades says in that refusal that a newer release upgraded the store, and names the kept catalogue. Releases from before automatic adoption only say the stored authority is "unrecognized or incompatible", so when you go back to one of them, this section is the guidance.

catalogue restore puts the kept catalogue back. It takes two values from catalogue status --json: the report's backup, and catalogueHash, the SHA-256 of the catalogue stored now. A release that adopts catalogue upgrades prints the complete command in its refusal; with a release from before automatic adoption, run it with the new release.

Stop every gateway on the store first. On a shared Postgres store that means every replica of every deployment that uses it: the command cannot detect a running gateway. Run the command with the release that is installed now, then start the earlier image.

The backup value is a path such as /home/node/.tokedex/catalogue.json.replaced-0123456789abcdef on a file store, or a document name such as gateway.catalogue.replaced-0123456789abcdef on Postgres. The command refuses, and changes nothing, if the backup is not a file or document this store saved or its bytes do not match its name, if the stored catalogue changed since you read catalogueHash, or if the backup is already the stored catalogue. A backup's name holds a digest of its bytes: the command checks the bytes against the name, which shows they were not changed, not who wrote them. It restores an unreadable-<hash> backup only when the running release can read it. It keeps the catalogue it replaces as another replaced-<hash> backup, so you can undo the restore the same way.

With Helm, keep the current chart and the values the release runs now: the earlier chart's schema may not accept catalogue.upgrade, and the earlier image ignores it. Only the image changes. helm history tokedex -n tokedex lists the revisions, and helm get values tokedex -n tokedex --revision <previous revision> shows the earlier image.tag and image.digest. Set both: the chart uses the digest whenever one is set, and ignores the tag. If your values turn on autoscaling, the autoscaler starts the gateway again during these steps, so the commands keep it off; turn it back on once the earlier release runs.

Shell
values=release-values.yaml
helm upgrade tokedex ./helm/tokedex -n tokedex -f "$values" --set autoscaling.enabled=false --wait --timeout 10m
kubectl -n tokedex scale deployment/tokedex --replicas=0
kubectl -n tokedex wait pod -l app.kubernetes.io/name=tokedex,app.kubernetes.io/instance=tokedex,app.kubernetes.io/component=gateway --for=delete --timeout=5m
kubectl -n tokedex patch deployment tokedex --type json -p '[
  {"op": "add", "path": "/spec/template/spec/containers/0/command", "value": ["sleep", "infinity"]},
  {"op": "remove", "path": "/spec/template/spec/containers/0/startupProbe"},
  {"op": "remove", "path": "/spec/template/spec/containers/0/readinessProbe"},
  {"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}
]'
kubectl -n tokedex scale deployment/tokedex --replicas=1
kubectl -n tokedex rollout status deployment/tokedex --timeout=5m
kubectl -n tokedex exec -i deployment/tokedex -- node /app/dist/cli.js catalogue status --json
kubectl -n tokedex exec -i deployment/tokedex -- node /app/dist/cli.js \
  catalogue restore --backup '<backup>' --acknowledge <catalogueHash>
kubectl -n tokedex scale deployment/tokedex --replicas=0
kubectl -n tokedex wait pod -l app.kubernetes.io/name=tokedex,app.kubernetes.io/instance=tokedex,app.kubernetes.io/component=gateway --for=delete --timeout=5m
kubectl -n tokedex patch deployment tokedex --type json \
  -p '[{"op": "remove", "path": "/spec/template/spec/containers/0/command"}]'
helm upgrade tokedex ./helm/tokedex -n tokedex -f "$values" --set autoscaling.enabled=false \
  --set image.tag=<earlier tag> --set image.digest=<earlier digest, or ""> --wait --timeout 10m

The gateway stays at zero Pods from the restore until helm upgrade starts the earlier image: removing the sleep command while a Pod runs would start the new release, which adopts its catalogue again. For the same reason, do not run the helm upgrade from Run a gateway command while the gateway is stopped unchanged: it starts the new release. Put the earlier image.tag and image.digest in your values file afterwards, so the next helm upgrade does not start the new release either.

With Docker Compose, run the command with the current image, then start the earlier one:

Shell
docker compose stop gateway
docker compose run --rm --no-deps gateway catalogue status --json
docker compose run --rm --no-deps gateway catalogue restore --backup '<backup>' --acknowledge <catalogueHash>
TOKEDEX_GATEWAY_IMAGE=tokedex/gateway:<earlier tag> docker compose up -d gateway

Set TOKEDEX_GATEWAY_IMAGE to the earlier image wherever you keep it, so the gateway does not start the new release on its next restart.

Your settings, keys and users stay as they are, including changes made after the upgrade. Start the earlier image, not the new one: with catalogue.upgrade: auto the new release adopts its catalogue again as it starts.

Check the upgraded gateway#

Shell
curl -sS https://gateway.example.com/readyz
kubectl -n tokedex logs deployment/tokedex | grep 'State backends'
kubectl -n tokedex get deployment tokedex -o jsonpath='{.spec.template.spec.containers[0].image}'
JSON
{"ok":true,"release":{"environment":"local"},"service":"tokedex"}
  1. Check that the gateway is ready and that the State backends line names the storage you expect.
  2. Sign in as an Owner and check Workspaces, API keys, Providers and Models & Routing, and that Usage and Observability still show earlier activity.
  3. Send one request through each client API your applications use. It calls your provider and can incur charges.
  4. Let clients back in.

Then make the new values the running values, so every later helm upgrade keeps the new image:

Shell
mv next-values.yaml release-values.yaml

Recover a failed upgrade#

If the startup log says catalogue recovery is required, inference and administration are unavailable. Keep clients blocked and keep the logs of the failed run. Remove the master key and other secrets before sharing diagnostics.

  1. Check catalogue status (read-only). Set values=next-values.yaml and open the maintenance container. Run a gateway command while the gateway is stopped

    Shell
    kubectl -n tokedex exec deployment/tokedex -- node /app/dist/cli.js catalogue status --json
  2. If the startup log says the upgrade needs review, read its reasons, then preview the change (read-only).

    Shell
    kubectl -n tokedex exec deployment/tokedex -- node /app/dist/cli.js catalogue preview
  3. Choose how to recover.

    • Apply if preview succeeds and you accept the change (writes catalogue state). Review the proposed catalogue, router plan and entries the upgrade will disable. When a detailed comparison is unavailable, the model and key change lists are null. Run the write command in the maintenance container with the gateway stopped. Replace <reviewDigest> with the exact value from preview.

      Shell
      kubectl -n tokedex exec deployment/tokedex -- node /app/dist/cli.js catalogue apply --acknowledge '<reviewDigest>'
    • Decline the change by running the earlier release instead. The new release changes nothing until you apply.

    • Rebuild only when this gateway cannot read the stored catalogue (writes catalogue state). The startup log or catalogue status then says the catalogue is out of sync and prints a rebuild command; a readable catalogue is replaced only through preview and apply. Run the write command in the maintenance container with the gateway stopped. Copy the full rebuild command from the startup log or from the status JSON's data.status.message. It already contains the SHA-256 of the stored catalogue bytes, labelled File digest for rebuild in Details. The catalogue revision is listed separately.

      Shell
      kubectl -n tokedex exec deployment/tokedex -- node /app/dist/cli.js catalogue rebuild --acknowledge '<stored-catalogue-digest>'

      Rebuild keeps operator settings, keys and users. It backs up the old catalogue and its reports, then replaces the old catalogue routing, detached plan and catalogue overrides with the installed catalogue. Saved operator overrides stay in the operator store. If rebuild fails, stop and keep the status JSON and masked error for support. Remove credentials before sharing them.

  4. Start the repaired gateway and check it before unblocking clients. For other failures, deploy a corrected release or restore the pre-upgrade backup with the release and Secrets it was taken with.

An earlier release cannot always read state that a later one wrote, so a restore is the way back. Keep the pre-upgrade backup until you are sure you will not need to go back. Never delete the gateway's claim to clear an upgrade error: it holds keys nothing else can rebuild.

Note:

Warning: helm rollback changes Kubernetes resources only. It leaves the database migrated and the new catalogue in the store, and an earlier release must not run against them. Restore the whole backup, or, when no migration ran, put the replaced catalogue back first.

Next: Backup and restore, Troubleshooting or Recover dashboard access.