Health rule for a custom resource

Tell Wardn when a cert-manager Certificate is healthy and when it has failed, with a CEL health rule on the deployment.

The situation

shop serves shop.example.com over TLS, and cert-manager issues the certificate from a Certificate object that lives in git next to the Deployment. Kubernetes has no readiness convention for a Certificate, so Wardn has nothing to report for it: the object shows unknown, and a certificate that cannot be issued looks the same as one that is fine. The first sign is a pod stuck in ContainerCreating because the Secret it mounts does not exist.

A health rule fixes that. It is a small CEL expression, set on the deployment, that says when an object of one kind is healthy, when it has failed, and what to say while it is neither. With the rule, the deployment shows Degraded with cert-manager's own reason, instead of a silent wait (Health rules, Health).

Repository layout

certificate.yaml
deployment.yaml
service.yaml

The app is a plain-manifest app whose Path is deploy/web, on a deployment in the shop namespace (Deploy your first app). The rule is not in the repository: it is a setting of the deployment, like a job, and Wardn does not read it from git.

Settings

Before you start

Two things must exist in the cluster before Wardn applies a Certificate, and neither can come from the app:

  • cert-manager and its CustomResourceDefinitions. The operator refuses a custom resource whose kind it cannot find, because it cannot tell whether the kind is namespaced (Connect a cluster).
  • The issuer. A ClusterIssuer is cluster-scoped, and the operator refuses cluster-scoped objects. Create letsencrypt once, outside Wardn, with whatever solver you use.

The manifests

deploy/web/certificate.yaml
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: web-tls
  annotations:
    wardn.dev/sync-wave: "-5"
spec:
  secretName: web-tls
  dnsNames:
    - shop.example.com
  issuerRef:
    name: letsencrypt
    kind: ClusterIssuer
deploy/web/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: ghcr.io/example/shop-web:1.4.2
          ports:
            - containerPort: 8443
          volumeMounts:
            - name: tls
              mountPath: /etc/tls
              readOnly: true
      volumes:
        - name: tls
          secret:
            secretName: web-tls

The wave puts the Certificate ahead of the Deployment. It does not wait for the certificate to be issued: a custom resource with no standard readiness counts as ready the moment it exists, so the pods start, wait in ContainerCreating for the Secret, and start by themselves once cert-manager writes it. The health rule is what tells you why they are waiting (Health rules and the wait during an apply).

The rule

cert-manager writes a list of status.conditions on a Certificate. The one that matters is Ready, which is True once a valid certificate is in the Secret. When an issuance fails, it also sets status.lastFailureTime. The rule uses both:

certificate-rule.json
{
  "group": "cert-manager.io",
  "kind": "Certificate",
  "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.lastFailureTime)",
  "message": "has(object.status) && has(object.status.conditions) && object.status.conditions.exists(c, c.type == 'Ready') ? object.status.conditions.filter(c, c.type == 'Ready').map(c, 'message' in c ? c.message : c.reason).join(', ') : 'waiting for cert-manager'"
}

Wardn checks failed first, then healthy, so each object lands in exactly one state:

What cert-manager reportsHealthMessage
No status yet, or Ready is False and nothing has failedprogressingThe Ready condition's message, or waiting for cert-manager
Ready is True, no failure recordedhealthynone
lastFailureTime is setdegradedThe Ready condition's message

Every field is guarded with has(...). A rule that reads a missing field does not mean "not ready": it makes the object unknown with health rule failed: ..., which hides the very state you want to see.

lastFailureTime is set when an issuance or a renewal fails, and cleared when one succeeds. So a certificate that is still serving but cannot renew shows degraded: a warning you can act on before it expires. To reserve degraded for certificates that are not ready, use this failed instead: has(object.status) && has(object.status.lastFailureTime) && !(has(object.status.conditions) && object.status.conditions.exists(c, c.type == 'Ready' && c.status == 'True')).

Save it on the deployment

Health rules are part of the 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, change and write back. This replaces the rule for Certificate and keeps every other rule, so it is safe to run again after an edit:

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

api "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/settings" \
  | jq --slurpfile rule certificate-rule.json '
      del(.delivery.updatedAt)
      | .delivery.healthRules = (
          [.delivery.healthRules[]? | select((.group // "") != "cert-manager.io" or .kind != "Certificate")]
          + $rule)' \
  | api -X PUT "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/settings" -d @-

api -X POST -H "Idempotency-Key: $(uuidgen)" "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/sync"

Writing settings needs deployment:manage, and syncing needs deployment:sync. Rules travel to the operator with an apply, so Sync now (the second call) is what puts the new rule to work. A deployment holds up to 50 rules, one per kind, and each expression is limited to 4,096 bytes.

Check that it worked

Rules are checked for size when you save them, not for syntax, so first look for a mistake. List the objects with the health the operator reports:

api "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/resources" \
  | jq '.apps[].resources[] | select(.kind == "Certificate") | {name, health, healthMessage}'

In the web app, open the deployment, then the app's Resources tab: the Certificate row has its own health and message.

You seeMeaning
unknown, message starts invalid health ruleAn expression does not compile or returns the wrong type. healthy and failed must return a boolean, message a string.
unknown, message starts health rule failedAn expression read a field the object does not have. Guard it with has(...).
progressing with cert-manager's messageThe rule works; the certificate is not issued yet.
healthyThe certificate is issued, and so the deployment can be healthy too.

See the failed path without breaking cert-manager

You do not need a broken issuer to know the degraded branch works. Save the rule once with "failed": "true", sync, and the Certificate shows degraded with its message. Put the real expression back and sync again. To see a real failure, point issuerRef.name at an issuer that rejects requests and push. The deployment's own state shows the roll-up:

api "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/state" | jq '{health, healthMessage}'

A deployment's health is the worst of its objects, so a failed certificate makes the whole deployment degraded. The reason names the object, its kind and name, then the message. When the health changed is on the deployment's timeline.

Next

Related: Health, Deployment settings, Timelines and Activity.

On this page