Health rules

How Wardn decides a deployment is healthy, and how to write CEL rules for custom resources that Kubernetes has no standard readiness for.

Applying an object is not the same as it working. Wardn's operator keeps reading every object it applied and reports a health for it, then rolls them up into one health for the deployment. For the Kubernetes kinds with standard readiness, that needs no setup. For a kind Kubernetes knows nothing about, such as a custom resource, you write a health rule: a few small expressions that say when an object of that kind is healthy, when it has failed, and what to say about it. Health explains the idea; this page is how to configure it.

Checks now belong to the app, and custom checks live in the organization's library: see Health checks. A rule set on one deployment, as described here, still works and is read before the app's checks.

The four states

HealthMeaning
healthyEvery object is ready.
progressingSomething is not ready yet but may become so: a rollout in progress, a rule whose healthy expression does not hold yet.
degradedSomething has failed or is stuck.
unknownWardn cannot tell: a kind with no readiness, or a rule that could not be evaluated.

A deployment's health is the worst among its objects, in the order degraded, progressing, unknown, healthy. The reason shown is the worst object's: its kind and name, then its message. A deployment on an environment also has a health per cluster. The deployment page shows the one status that matters most (Degraded, Progressing, Healthy), and an app's Resources tab shows each object with its own health and message. GET /deployments/{id}/state returns health and healthMessage.

What is built in

Without any rule, the operator reads each object with the standard Kubernetes readiness rules (the ones the kstatus library implements) and looks at the pods of workloads.

  • Ready or not. A Deployment is healthy when its rollout is complete, a StatefulSet when its replicas are ready, a Job when it has completed, and so on. An object still rolling out is progressing. A failed or missing one is degraded.
  • Stuck pods. A Deployment, StatefulSet, DaemonSet, ReplicaSet or Pod can look ready between two crashes, so the operator also checks the pods. If any container is waiting with CrashLoopBackOff, ImagePullBackOff, ErrImagePull, CreateContainerConfigError, InvalidImageName or CreateContainerError, or a pod's phase is Failed, the workload is degraded and the message names the pod and container, for example pod web-6d9f container web: CrashLoopBackOff.
  • No standard. An object whose kind has no readiness convention and no rule has no signal to report. Add a rule.

Write a rule

A rule belongs to a kind, and it replaces the built-in reading for that kind in the deployment. It has up to three expressions in CEL, the small expression language Kubernetes uses for admission policies. Each sees the live object exactly as kubectl get -o json shows it, as object (also data, with its status, spec and metadata as variables).

Prop

Type

Wardn evaluates them in this order:

  1. If failed is set and true, the object is degraded, with the message (or the failed rule matched).
  2. Otherwise, if healthy is true, the object is healthy.
  3. Otherwise the object is progressing, with the message (or the healthy rule does not hold yet).
  4. If an expression fails to evaluate, for example because a field it reads is missing, the object is unknown with health rule failed: .... If an expression does not compile or returns the wrong type, the object is unknown with invalid health rule: ....

Rules are checked for size when you save them, not for syntax, so a typo shows up afterwards as unknown and that message. Expressions are limited to 4,096 bytes each, and the work one can do is bounded, so a rule cannot stall the operator. A deployment can have up to 50 rules; give each kind one.

An example

A custom Widget that reports a phase:

{
  "group": "example.com",
  "kind": "Widget",
  "healthy": "has(object.status) && has(object.status.phase) && object.status.phase == 'Ready'",
  "failed": "has(object.status) && has(object.status.phase) && object.status.phase == 'Error'",
  "message": "has(object.status) && has(object.status.message) ? object.status.message : 'waiting for the controller'"
}

Guard fields with has(...): before a controller has written status, object.status.phase is an error, which would show as unknown instead of progressing. Many controllers use conditions rather than a phase. The same pattern works with exists:

{
  "group": "example.com",
  "kind": "Gadget",
  "healthy": "has(object.status) && has(object.status.conditions) && object.status.conditions.exists(c, c.type == 'Ready' && c.status == 'True')",
  "failed": "has(object.status) && has(object.status.conditions) && object.status.conditions.exists(c, c.type == 'Failed' && c.status == 'True')",
  "message": "has(object.status) && has(object.status.conditions) ? object.status.conditions.map(c, c.type + '=' + c.status).join(', ') : 'no conditions yet'"
}

Set the rules

Rules are part of the deployment's delivery settings and are set through the API; the web app keeps the ones that are saved but does not edit them yet. Settings are replaced as a whole, so read, add and write back (Deployment settings):

api() { curl -fsS -H "Authorization: Bearer $WARDN_TOKEN" -H 'Content-Type: application/json' "$@"; }

api "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/settings" | jq '
  del(.delivery.updatedAt)
  | .delivery.healthRules += [{
      "group": "example.com",
      "kind": "Widget",
      "healthy": "has(object.status) && has(object.status.phase) && object.status.phase == \"Ready\"",
      "failed": "has(object.status) && has(object.status.phase) && object.status.phase == \"Error\"",
      "message": "has(object.status) && has(object.status.message) ? object.status.message : \"waiting for the controller\""
    }]' \
  | api -X PUT "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/settings" -d @-

Needs deployment:manage. Rules travel to the operator with an apply, so a change takes effect with the deployment's next apply. After saving, use Sync now (or POST /deployments/{id}/sync) to apply the current snapshot again and pick the rules up.

A rule applies to objects Wardn applied for this deployment. The custom resource must exist in the snapshot, and its CustomResourceDefinition must already be in the cluster: the operator refuses a custom resource whose kind it cannot find, because it cannot tell its scope. Install CRDs once, outside Wardn. See Connect a cluster.

Health rules and the wait during an apply

The wait after each wave uses the standard readiness rules only, not your health rules. A custom resource with no standard readiness counts as ready as soon as it exists, so a rule does not hold an apply back. The rule is what the operator reports afterwards: the object, and the deployment, show progressing or degraded until the rule says otherwise. If a workload needs a custom resource to be truly ready first, order them with waves and use a job to check it.

Troubleshooting

What you seeWhy
The deployment stays unknown and the message starts invalid health rule.A CEL expression does not compile or returns the wrong type: healthy and failed must return a boolean, message a string.
The message starts health rule failed.An expression read a field the object does not have. Guard it with has(...).
A rule has no effect.The rule is for a different group or kind than the object (check kubectl get <kind> -o yaml for apiVersion and kind), or the settings were saved but no apply has happened since.
progressing forever.healthy never becomes true. Add a message that prints the field you wait on.

Next

Related: Health, Deployment settings, Drift.

On this page