Check a release and roll back automatically¶
Declare [[checks]] in release.toml and set
rollback_on_failed_checks = true: after piceli release apply becomes ready it
runs the checks, and when one fails it re-applies the previous ready release
without operator action and records the failed check and the rollback.
Maturity: preview
Post-deploy checks ([[checks]], piceli.checks, piceli release check,
--skip-checks) are new. Keys, JSON fields and the Python API may change in a
minor release, with a changelog entry. See the Roadmap for every
feature’s status.
Readiness only says that pods started and their probes pass. A check says that
the release works: the login page answers, a self-test command exits 0,
the error rate stays low. A release is ready only when its checks pass.
Prerequisites¶
A working
piceli releasespec (see Releases from a spec (piceli release)).For
httpandmetricchecks, the spec’s credentials needcreateonpods/portforwardin the namespace. They reach a ready pod of the target (a Service through its selector andtargetPort) through a port forward of the API, with the spec’s kubeconfig and context, never the current context, and need nokubectl. The connection starts inside the pod’s network namespace, so an isolated branch environment’s NetworkPolicy does not stand in the way and is not widened (the API server’s proxy is refused there when the API server runs on another node, as on k3s). When the port forward is refused, they use the API server proxy (getonservices/proxyorpods/proxy), then akubectl port-forwardifkubectlis onPATH.For
execchecks, the spec’s credentials needcreateonpods/execin the namespace.
Steps¶
Add checks and the rollback policy to
release.toml:[release] name = "shop" # … owner, field_manager, composition, state_dir … rollback_on_failed_checks = true [[checks]] type = "http" target = "service/web" path = "/login" expect = 200 body_contains = "Sign in" [[checks]] type = "exec" target = "deployment/api" command = ["api", "self-test"]
Plan. The plan validates the checks (a
pythoncheck must import) and shows whatapplywill run; nothing is executed yet:piceli release plan --spec release.toml
"checks": {"names": ["http-service-web-login", "exec-deployment-api-api"], "rollback_on_failed_checks": true}
The checks and the policy are stored with the plan:
applyruns exactly what was reviewed, even if the spec changes afterwards.Apply the approved plan:
piceli release apply --spec release.toml --approve <plan hash>
When the checks pass, the output has
"release_state": "ready"and the exit code is0:check http-service-web-login: passed GET /login returned 200 check exec-deployment-api-api: passed api exited 0 apply shop-3f9a1c2e4b5d: ready
When a check fails, the command exits
1and the result records the failed check and the automatic rollback:check http-service-web-login: FAILED [check-failed] GET /login returned 500, expected 200 apply shop-8b20e71d9c44: checks-failed rolled back automatically to shop-3f9a1c2e4b5d: ready check http-service-web-login: passed GET /login returned 200{ "release": "shop-8b20e71d9c44", "release_state": "checks-failed", "execution": {"state": "ready", "execution_id": "…"}, "checks": {"passed": false, "failed": ["http-service-web-login"], "results": [{"name": "http-service-web-login", "type": "http", "passed": false, "code": "check-failed", "detail": "GET /login returned 500, expected 200", "attempts": 4, "duration": 6.2}]}, "rollback": {"state": "rolled-back", "target": "shop-3f9a1c2e4b5d", "release_state": "ready", "execution": {"…": "…"}, "checks": {"passed": true, "…": "…"}} }
See the outcome later without contacting the cluster:
piceli release status --spec release.toml
Each release has its latest
checksoutcome, and the history shows thechecks-failedexecution followed by the rollback ("trigger": "checks-failed").
To run the checks again at any time, without applying or rolling back anything:
piceli release check --spec release.toml [--release NAME]
It prints {"state": "succeeded", "release", "intent": "check", "checks"} and
exits 0 when every check passed, or {"state": "failed", "reason": "check-failed", …} and exits 1 when one failed (checks.failed names
them). A refusal (invalid spec, unknown release) is the usual
{"state": "rejected", …} with exit 2.
Check types¶
Every check has name (unique; derived from the check when omitted),
timeout (seconds per attempt, default 10), retries (extra attempts,
default 3) and interval (seconds between attempts, default 2). Checks run
in order; every check runs even after one failed.
Type |
Passes when |
Keys |
|---|---|---|
|
|
|
|
|
|
|
a Prometheus-compatible query returns at least one sample and every sample satisfies |
|
|
the function returns |
|
The same checks in Python, with App handles or plain kind/name targets:
from piceli import App
from piceli.checks import Checks
app = App("shop", owner="shop-prod")
web = app.deployment("web", image="registry.test/web@sha256:…", ports=[3000])
web_service = app.service(web, port=80, target_port=3000)
checks = [
Checks.http(web_service, "/login", expect=200, body_contains="Sign in"),
Checks.exec("deployment/api", ["api", "self-test"], retries=0),
Checks.metric(
"service/prometheus",
"sum(rate(http_errors_total[5m]))",
op="<",
threshold=0.05,
port=9090,
),
Checks.python("checks.py:smoke"),
]
A handle’s first port becomes the check’s port, and a workload handle keeps
its kind (app.stateful_set("store", …) becomes statefulset/store; a Job
or CronJob handle is refused). In TOML, and for a plain target without
port, the port is read from the live Service or pod template.
Python checks¶
A python check receives a piceli.checks.CheckContext:
# checks.py, next to release.toml
from piceli.checks import CheckContext, CheckFailed
def smoke(ctx: CheckContext) -> str | None:
response = ctx.http_get("service/web", "/api/health") # API port forward
if response.status != 200:
raise CheckFailed(f"health returned {response.status}")
result = ctx.exec("deployment/api", ["api", "check-db"]) # pods/exec
if result.exit_code != 0:
raise CheckFailed("api cannot reach its database")
if "web" not in ctx.images:
return "the release has no web image" # a string fails
return None # passes
It fails by returning False or a string (the detail), or by raising
CheckFailed(detail). Any other exception fails with check-raised and only
the exception type is recorded, so an exception message can never leak a
secret. Never put a secret value in a detail: details are printed and stored
in the release history.
Run checks from Python¶
run_checks(checks, context) -> CheckReport is the whole interface; piceli release uses it after readiness.
from pathlib import Path
from piceli.checks import CheckContext, Checks, run_checks
with CheckContext(
Path("cluster.kubeconfig"), # explicit file; KUBECONFIG is never read
"kind-shop", # explicit context; never current-context
"shop", # namespace
"shop-3f9a1c2e4b5d", # release name
{"web": "registry.test/web@sha256:…"}, # images: references, ImageRef or dicts
) as ctx:
report = run_checks(Checks.http("service/web", "/login"), ctx)
report.passed # True only when every check passed
for result in report.results: # CheckResult(name, passed, detail, duration, …)
print(result.name, result.passed, result.code, result.detail)
report.to_dict() # {"passed", "failed": [names], "results": […]}
CheckContext(kubeconfig, context, namespace, release, images=None, *, values=None, base=None, transport="https", request_seconds=10.0, kubectl="kubectl", forward_seconds=20.0, forwarder=None, executor=None, api_client=None): building one contacts nothing; the API client is created on
first use and closed by close() or the with block. base is where relative
python check files resolve from. forwarder, executor and api_client
replace the transport of http_get (a port forward instead of the API server
proxy), the exec transport and the client (tests). ctx.forward(target)
still opens a supervised kubectl port-forward for a Python check that needs
a local URL.
CheckResult also has type, attempts and code (None when passed).
Release states and rollback¶
After an execution, release_state in the result and state in the history
is one of:
State |
Meaning |
Selected |
Exit |
|---|---|---|---|
|
The execution is ready and every check passed (or there are none, or |
yes |
|
|
The execution is ready but a check failed |
no (the previous selection is kept) |
|
|
The execution did not become ready; no check ran |
no |
|
With rollback_on_failed_checks = true, a checks-failed release triggers
exactly one automatic rollback:
The target is the last other release whose execution was
ready. If there is none,rollback.stateisunavailable(checks-rollback-unavailable) and the failed release is left running.The rollback is an ordinary re-apply: it is planned against fresh discovery, executed through the journal as a new execution, and runs the checks too. Its history entry has
"trigger": "checks-failed",rolled_back_fromandfailed_execution_id; the failed entry getsrollbackwith the state, target and execution id.It restores the target release whole (0.16.0), like a manual
piceli release rollback: the objects both releases declare go back to the target’s version, an object the failed release removed (by its prune, or by hand) that the target declares is created again, and an object only the failed release declares is pruned like in any apply (claims, Secrets and retained objects are kept). From 0.14.7 to 0.15.1 it restored only the objects both releases declare, which could leave a workload without an object it needs (a Role the failed release had removed). A GitOps controller then stops: the environment isfailedwithchecks-failed-rolled-backand is not retried (see GitOps: Piceli’s controller and manifest export).It needs no second approval:
rollback_on_failed_checks = truewas part of the approved plan. It never triggers another rollback. If the rollback is refused, does not become ready or fails its own checks,rollback.stateisfailedwithchecks-rollback-failed.
The full report of each checked execution is kept privately in
state_dir/checks/<execution id>.json.
resume runs the checks when the resumed execution becomes ready, with the
same outcome and automatic rollback. A manual rollback runs the current
spec’s checks against the restored release.
Skipping checks in an emergency¶
--skip-checks on apply, rollback or resume marks the release ready
without running its checks. It is recorded: the result has
"checks": {"skipped": true, "flag": "--skip-checks", "declared": […]} and the
history entry has "skip_checks": true. Use it only to restore service when a
check itself is wrong, then fix the check.
If it fails¶
Each failing check has a code; piceli explain <code> prints the cause and
the fix.
Code |
Means |
Next step |
|---|---|---|
|
The condition was not met (status, body, exit code, threshold, a Python check returned False) |
Read |
|
The API refused the port forward and its proxy, could not reach a ready endpoint, or the port forward did not become healthy |
Grant |
|
No such Service/workload/pod, or no ready pod |
Check the target name against the composition |
|
No |
Set |
|
An attempt exceeded |
Raise |
|
The API refused the exec |
Grant |
|
The kubeconfig context cannot read the namespace |
Fix |
|
Not a Prometheus query response |
Fix |
|
A Python check raised, returned something odd, or cannot be imported |
Run the function locally |
|
The automatic rollback had no target or did not succeed |
|
A spec with an invalid [[checks]] table is refused before planning (exit 2,
check-invalid in the message).
For agents¶
piceli release checkchanges no Piceli state and never rolls back, but its checks may exec the declared commands in pods and run the declared Python functions: ask the owner before running it on a spec you did not write.An automatic rollback happens only when the approved plan had
rollback_on_failed_checks = true; afterapplyexits1, readrelease_stateandrollbackbefore doing anything else.Never add
--skip-checksunless the owner asked for it for this run.