Jobs and waves
The order Wardn applies objects in, how to run a migration or smoke test before or after an apply, and what a failure, a timeout or a rollback does.
Applying a snapshot is not one step. A database must exist before the app that connects to it, a ConfigMap before the Deployment that reads it, and a schema migration has to finish before the new code starts. Wardn's operator applies a snapshot in waves, one after another, and can run Kubernetes Jobs before the first wave and after the last. This page explains the order, how to add jobs, and what happens when something fails. Read Deployment settings first for the delivery settings these live in.
The order of an apply
Every apply of every snapshot runs the same sequence on the cluster:
- The operator checks the whole snapshot (namespace, scope, ownership). If anything is refused, nothing is applied.
- Before jobs run, in the order listed.
- Waves are applied from the lowest number to the highest. Within a wave the objects are applied together with server-side apply, and the operator then waits for every one of them to become ready before the next wave starts.
- Prune removes objects the snapshot no longer has, if pruning is on and the prune limit allows it.
- After jobs run.
If any step fails, the later ones do not run. In particular, a failed wave means nothing is pruned and no after job runs. The deployment's sync shows Apply failed with the reason, such as wave 0: ... naming the object that did not become ready.
Waves
A wave is a number. Objects with a lower number are applied, and must become ready, before objects with a higher one. You set it on an object with the annotation wardn.dev/sync-wave, an integer that may be negative. An object without it takes the default for its kind:
| Kind | Wave |
|---|---|
| Namespace, CustomResourceDefinition | -20 |
| ServiceAccount, Role, ClusterRole | -10 |
| RoleBinding, ClusterRoleBinding | -9 |
| ConfigMap, Secret, PersistentVolumeClaim | -5 |
| Everything else | 0 |
So by default identities and configuration are in place before the workloads that use them. An annotation that is not an integer is ignored and the kind's default applies.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
annotations:
wardn.dev/sync-wave: "-1"
spec:
# ...
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
# no annotation: wave 0, after the database is ready
spec:
# ...The annotation goes in the manifests, so it is part of the snapshot: put it in the YAML, in a Kustomize patch, or in a Helm template. Namespaces and CRDs sit in the earliest wave by default, but an app deployment cannot apply either, because the operator refuses cluster-scoped objects; see Connect a cluster.
What "ready" means
After each wave the operator waits until its objects are current by the standard Kubernetes readiness rules: a Deployment has rolled out, a StatefulSet's replicas are ready, a Job has completed, and objects with no status count as ready once they exist. That is the same reading health starts from. A wave fails the apply as soon as one of its objects reports a failure, or when its objects are not all ready within five minutes.
A wave that never becomes ready is the usual way a bad change fails after it has applied, for example an image that starts and crashes. The apply stops at that wave, nothing later runs, and the deployment shows Apply failed. The cluster keeps what was applied; to go back, restore an earlier snapshot, or set a job to roll back on failure.
Jobs before and after an apply
Jobs are set on the app and run in every deployment of it, once per release, each as a Kubernetes Job of its own (not once per pod). A deployment can turn one off or change it for itself, under Per deployment when you edit the app's jobs; a job that two apps both name runs as <app>-<job>. Jobs saved on a deployment before this change were moved to their app when the identical copies were merged; where a deployment's copy differed, it stays as that deployment's change.
A job is a Kubernetes batch/v1 Job that Wardn creates, waits for, and reports on. Use a before job for work that must finish before new code starts, such as a schema migration, and an after job for a check that the new version works, such as a smoke test.
Open the deployment, then Settings, and find Jobs ("Kubernetes Jobs that run around each sync"). Choose Add job and fill in:
| Field | Meaning |
|---|---|
| Job name | 1 to 63 characters, for example db-migrate. |
| Runs | Before deploy or After deploy. |
| App | Every app, or one app of the deployment. |
| If it fails | Stop, don't deploy, Carry on or Roll back this deployment. |
| Kubernetes spec | The Job as JSON. A new job starts from a minimal template. |
Choose Save delivery settings. The change is recorded in Activity and takes effect with the next apply. The web app does not set the timeout or an undo job; use the API for those.
Each app's own Jobs tab lists its jobs with Runs and Last run (Succeeded or Failed, and the snapshot).
Prop
Type
A deployment has up to 20 jobs before and 20 after.
How a job runs
- The operator creates the Job in the deployment's namespace, as the deployment's service account (
wardn-deployer). That account needs the right to create and read Jobs, pods and pod logs in the namespace; the built-inadminrole has it. A spec that names another namespace is refused. - The pods of the Job run as the
serviceAccountNamein the spec, or the namespace's default service account if it names none. - The Job gets a generated name (
migrate-and a random suffix), so it can run again and again. Wardn adds the annotationwardn.dev/deployment. If the spec leaves them out,backoffLimitis set to 0 (no retries) andttlSecondsAfterFinishedto an hour, so the cluster cleans up after a while and a failed Job's pods stay long enough to read. - The operator checks the Job every second until it completes, fails or reaches its timeout (five minutes unless
timeoutSecondssays otherwise). - When a job fails or times out, the last 30 lines of each container's log are kept with the run and reported, so you can see why without cluster access.
- Jobs run one after another, in the order listed; an after job runs only when every wave succeeded.
Jobs are delivery settings, not part of a snapshot. They are not rendered from git and do not change with a push. If a migration job needs the new version's image, change the job in the settings as part of the release, or use a tag that follows the release. Pin it the same way you pin the app's own images.
What a failure does
failurePolicy | When the job fails or times out |
|---|---|
stop (default) | The deploy ends and the apply is marked failed. Waves, prune and after jobs that had not run do not run. For a before job that means nothing was applied. |
continue | The failure is recorded on the run and the deploy goes on. |
roll_back | Like stop, and in addition Wardn restores the last snapshot that applied. |
The automatic rollback is skipped, with a reason in Activity (deployment.auto_rollback_skipped), when no earlier snapshot has applied or when the restore preflight finds something blocking, because nobody is there to bypass it. A rollback that itself fails a roll_back job stops, and does not ask for another rollback. Otherwise the rollback is recorded as deployment.auto_rollback. The preflight is described in Restore and rollback.
Undo jobs
A rollback goes backwards, so running the forward job again would be wrong. In a rollback, whether you asked for it or Wardn did, every job runs in a special mode:
- A job with an
undoJobruns the undo job instead, for examplemigrate down. Its run is reported with the phaseundo. - A job without an undo job is skipped.
This applies to both before and after jobs. A forward migration is therefore never repeated by a rollback, and a down migration only runs if you wrote one.
See the runs
GET /deployments/{id}/jobs lists the configured jobs with their image, policy, timeout and last run. GET /deployments/{id}/job-runs lists every run, newest first, with phase (before, after or undo), status (succeeded or failed), the snapshotId, the error and the logTail. Both take appId; the runs also take snapshotId. Both need deployment:read.
api "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/job-runs?snapshotId=$SNAPSHOT_ID" \
| jq '.items[] | {name, phase, status, error}'Next
Health rules
Tell Wardn what healthy means for your own custom resources.
Restore and rollback
Go back to an earlier snapshot, with the preflight.
Run a migration before an apply
A complete setup with an undo job.
Related: Sync and arrival, Deployment settings, Held snapshots.
Held snapshots
Why a render is held instead of applied, what happens to the deployment meanwhile, and how to release or discard the snapshot.
Health checks
The checks that decide an app is healthy, set once on the app (built-in checks, custom checks in CEL or Lua, HTTP and metric checks), and the watch that follows a release.