Webhook icon
Schedule icon
Script icon
Loop icon
If icon
SlackIncomingWebhook icon
Log icon
Fail icon

Reclaim Stale KVM Snapshots Before They Eat the Datastore

Score KVM snapshot inventories against retention policy, skip referenced and exempt hosts, alert Slack per host and gate the run on reclaimable GB.

Categories
Infrastructure

How it works

  1. on_inventory_publish (io.kestra.plugin.core.trigger.Webhook, keyed with the KVM_SNAPSHOT_WEBHOOK_KEY secret) receives the snapshot inventory from your provisioning or maintenance pipeline. nightly_retention_scan (io.kestra.plugin.core.trigger.Schedule, cron 0 4 * * *) is the safety net and ships disabled: true until an operator turns it on.
  2. apply_retention (io.kestra.plugin.scripts.python.Script on python:3.12-slim) prefers the posted inventory and falls back to a bundled six snapshot example so trial runs still produce a report. Each snapshot is aged against scan_date; it is reclaimable only when its age exceeds retention_days and it is neither VM referenced nor on a host listed in exempt_hosts. The reclaimable names, the gigabyte total, the protected count, the oldest age and the per host rollup are printed as a single ::{"outputs": ...}:: line.
  3. review_hosts (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) walks the scored host rows one at a time. flag_host (io.kestra.plugin.core.flow.If) branches on the per host reclaimable gigabytes, so a host carrying stale snapshots gets its own alert_host (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) with the exact counts, while a clean host lands on log_host_clean.
  4. gate_reclaim (io.kestra.plugin.core.flow.If) branches on the fleet verdict. Reclaimable gigabytes at or above min_reclaimable_gb reach alert_reclaim with the snapshot names and the oldest age, then fail_retention (io.kestra.plugin.core.execution.Fail) turns the run red. A compliant estate lands on log_retention_clean.
  5. The flow level errors handler (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) reports any failure with the execution id, because a dead scorer looks exactly like a clean datastore.

What you get

  • Per snapshot age, reference and exemption verdicts against a single retention policy that lives in version control.
  • Reclaimable snapshot names in inventory order, ready to feed a virsh snapshot-delete job or a storage ticket.
  • One focused Slack alert per host holding reclaimable space plus a fleet escalation when the gigabyte threshold is met.
  • reclaimable_count, reclaimable_gb, oldest_reclaimable_age_days and the full host_summary table as typed outputs.
  • A disabled nightly schedule as a second net once the webhook path is trusted.

Who it's for

  • Virtualization admins responsible for KVM estates where provision and self-heal snapshots accumulate without garbage collection.
  • Platform teams that need retention evidence before approving storage expansion.
  • SREs who want stale snapshot pressure surfaced as a gate in the same pipeline as the rest of the platform checks.

Why orchestrate this with Kestra

Snapshot sprawl is invisible until a datastore fills and a live VM stalls. Kestra gives the provisioning pipeline a webhook to land on, a scorer whose age, reference and exemption rules are reviewable, a Loop that names every leaking host, a branch that separates escalation from the all clear, and outputs that a reclaim job or weekly storage report can consume. A cron script on the hypervisor can list snapshots, but it has no alert routing, no execution history and no gate.

Prerequisites

  • A worker with Docker access, the scorer runs in python:3.12-slim.
  • A provisioning or maintenance pipeline that POSTs snapshot inventory JSON with host, created_at, size_gb and has_vm_ref fields, or rely on the bundled example for trial runs.
  • A Slack incoming webhook for the per host alerts and for failure alerts.

Secrets

  • KVM_SNAPSHOT_WEBHOOK_KEY: key segment guarding the inventory webhook URL, anyone holding it can trigger a retention scan.
  • SLACK_WEBHOOK_URL: Slack incoming webhook for the per host alerts and failure alerts.

Quick start

  1. Add the two secrets to your Kestra namespace.
  2. Run the flow once with the default inputs and confirm reclaimable_count reads 3, reclaimable_gb reads 91.0, oldest_reclaimable_age_days reads 122 and protected_count reads 3, and that the host and escalation Slack alerts arrived.
  3. Raise min_reclaimable_gb to 100 and re-run to watch the same inventory pass the gate while the reclaimable counters stay identical.
  4. Point your pipeline at /api/v1/{tenant}/executions/webhook/{namespace}/kvm-snapshot-retention/{KVM_SNAPSHOT_WEBHOOK_KEY} and enable nightly_retention_scan once the path is trusted.

How to extend

  • Drive reclamation directly with io.kestra.plugin.scripts.shell.Commands on a libvirt host after the gate passes, consuming the reclaimable_snapshots output as the removal list.
  • Persist the host_summary rows to a warehouse with io.kestra.plugin.jdbc.postgresql.Queries to chart reclaimable gigabytes trending down week over week.
  • Fan out per cluster with io.kestra.plugin.core.flow.ForEachItem over a subflow that scores one cluster inventory each.
  • Add a second threshold for single snapshot size so one giant snapshot escalates even when the fleet total stays under the limit.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.