Skip to content

lh-snap-revert: add a Longhorn snapshot revert tool for EVE-K - #7038

Open
andrewd-zededa wants to merge 2 commits into
lf-edge:masterfrom
andrewd-zededa:eve-k-lh-snap-revert-tool
Open

andrewd-zededa wants to merge 2 commits into
lf-edge:masterfrom
andrewd-zededa:eve-k-lh-snap-revert-tool

Conversation

@andrewd-zededa

@andrewd-zededa andrewd-zededa commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

Add lh-snap-revert, an out-of-band recovery tool for occasional use that
reverts the Longhorn volume behind an EVE-K app PVC to a snapshot in place.
The PVC, its UID and the PV do not change, so the app uses the reverted data
when it starts again.

The tool:

  • reverts a volume (revert)
  • shows the snapshot branch that a revert leaves behind (list)
  • helps delete it, so that a later revert stays possible (cleanup)
  • only changes the Longhorn volume, and does not manage the app state

Scheduling, retention and backup of snapshots stay with Longhorn and EVE.
Stopping the app stays with the controller.

The pillar image includes the tool as /opt/zededa/bin/lh-snap-revert for
HV=k. It also builds as a standalone binary with
make lh-snap-revert ZARCH=<arch>, so it can be copied to a node of an older
release that runs Longhorn v1.9.1.

Design notes:

  • The tool does not scale the app VMIRS, because pillar restores a VMIRS that
    has 0 replicas. It refuses to run while the PVC is in use.
  • The operator pauses EVE's snapshot schedule with
    storage.longhorn.snapshot.cron on every node, so that the recurring job
    does not delete the target snapshot during the procedure.
  • Longhorn runs recurring jobs on attached volumes, so for the run the tool
    sets the volume's recurring job labels to disabled and restores them
    after.
  • It takes a safety-net snapshot, attaches the volume with its own attachment
    ticket and the frontend disabled, reverts, and releases the volume on exit
    or failure. The snapshot comes first because the Longhorn v1.9.1 engine
    crashes on a snapshot while its frontend is disabled.
  • A revert leaves the old snapshots on a branch. Longhorn cannot purge a
    snapshot with two children and can then keep the volume attached, which
    blocks a later revert. cleanup deletes that branch.
  • On a Longhorn version other than v1.9.1, revert warns and exits unless
    --ignore-longhorn-version is passed.

The procedure, flags, exit codes and recovery steps are in
pkg/pillar/tools/lh-snap-revert/README.md.

How to test and validate this PR

Automated: go test -race ./tools/lh-snap-revert in pkg/pillar runs the
revert flow against a fake Longhorn API, including the v1.9.1 snapshot and
attachment behaviour, preflight refusals, the version override and a
safety-net snapshot that stays pending.

Manual, on a 3-node EVE-K cluster with Longhorn v1.9.1 and an app with a
Longhorn PVC:

  1. Write data in the app and wait for, or take, a snapshot. Write more data.
  2. Set storage.longhorn.snapshot.cron to an empty value on every node.
    Check that kubectl -n longhorn-system get recurringjobs.longhorn.io does
    not list default-eve-kube-app-snapshot.
  3. Deactivate the app from the controller.
  4. On a node, run lh-snap-revert pvcs. The USED BY column for the app's PVC
    must be -.
  5. Run lh-snap-revert list --pvc <pvc> and pick the snapshot from step 1.
  6. Run lh-snap-revert revert --pvc <pvc> --snapshot <snap> --dry-run, then
    without --dry-run. It must exit 0 and print the safety-net snapshot name.
  7. Activate the app. The data written after the snapshot must be gone.
  8. Run list again: the old snapshots and the safety-net snapshot show as
    old branch. Run lh-snap-revert cleanup --pvc <pvc>, and list must
    show one chain ending in volume-head.
  9. Restore storage.longhorn.snapshot.cron on every node.

Tested so far with the standalone binary on a 3-node cluster: the full cycle
above, including the safety-net snapshot and cleanup. Not yet tested: the
pillar-image path (eve exec pillar /opt/zededa/bin/lh-snap-revert), an app
with several disks, and interrupting a run with Ctrl-C.

make mini-yetus passes, with only the existing hadolint DL3018 warnings in
pkg/pillar/Dockerfile.

Changelog notes

EVE-K: add lh-snap-revert, a recovery tool that reverts an app volume to a
Longhorn snapshot in place, and lists and deletes the old snapshot branch
that a revert leaves.

PR Backports

- 17.0-stable: No, the standalone build covers nodes of this release.
- 16.0-stable: No, as the feature is not available there.
- 14.5-stable: No, as the feature is not available there.
- 13.4-stable: No, as the feature is not available there.

Checklist

  • I've provided a proper description

  • I've added the proper documentation

  • I've tested my PR on amd64 device

  • I've tested my PR on arm64 device

  • I've written the test verification instructions

  • I've set the proper labels to this PR

  • I've checked the boxes above, or I've provided a good reason why I didn't
    check them.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 31.29%. Comparing base (fdc29c5) to head (1bd631a).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7038      +/-   ##
==========================================
+ Coverage   30.79%   31.29%   +0.49%     
==========================================
  Files         562      573      +11     
  Lines      101223   102818    +1595     
==========================================
+ Hits        31169    32172    +1003     
- Misses      67634    68271     +637     
+ Partials     2420     2375      -45     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread pkg/pillar/Makefile Resolved
@andrewd-zededa
andrewd-zededa force-pushed the eve-k-lh-snap-revert-tool branch from 35095ab to 893a9dc Compare November 10, 2026 19:46
@rene

rene commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

@claude

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @rene's task in 3m 56s —— View job


Review of PR #7038 — lh-snap-revert

  • Read the PR diff and changed files
  • Review Makefile / Dockerfile / packaging
  • Review Go source (api.go, revert.go, list.go, cleanup.go, hold.go, main.go)
  • Review tests
  • Post inline comments & summary

I reviewed the full tool. Overall it is carefully written — the attach/revert/detach state machine, the touched/attached/held flags driving cleanup and exit codes, and the afterAttachError "reverted: yes/no/unknown" reporting are all sound, and the test coverage of the tricky paths (failed revert, Ctrl-C not cancelling the revert, failed detach keeping the hold, pending safety snapshot) is good.

CodeQL "potentially unsafe quoting" (hold.go) — resolved. manualReleaseCmd now escapes single quotes in the embedded JSON (strings.ReplaceAll(string(data), "'", '''), hold.go:112), and heldLabels now validates keys with validation.IsQualifiedName (hold.go:67-75). The two companion helpers that build kubectl ... '...' strings — manualDetachCmd (revert.go:566) and deleteSnapshotCmd (revert.go:572) — only embed tool-controlled constants/generated names (ticketID, safetyPrefix+timestamp), so they aren't exposed to the same injection.

Findings

  • main.go:92-95 (minor): explicit -h/--help/help returns exit code 1; exit 0 is the usual convention for an explicit help request. Posted inline. Low priority.

No correctness or safety bugs found in the revert flow. A few non-blocking observations:

  • The build/vet step for the tool package was blocked by sandbox permissions in this environment, so I reviewed by reading only; the vendored deps (longhorn-manager clientset, client-go/dynamic) are present under pkg/pillar/vendor, and DISTDIR/ZARCH wiring between the top-level Makefile, pkg/pillar/Makefile and Dockerfile is consistent.
  • @milan-zededa's open question about whether the binary belongs in pkg/kube vs pkg/pillar (vendoring footprint) is a packaging decision the author said they'd evaluate — out of scope for correctness.
    · branch eve-k-lh-snap-revert-tool

Comment thread pkg/kube/lh-snap-revert/main.go Resolved
@andrewd-zededa
andrewd-zededa force-pushed the eve-k-lh-snap-revert-tool branch from 893a9dc to 11de832 Compare November 16, 2026 21:44
@andrewd-zededa
andrewd-zededa force-pushed the eve-k-lh-snap-revert-tool branch 2 times, most recently from bb7d3a7 to c468b25 Compare November 16, 2026 22:09
@andrewd-zededa

Copy link
Copy Markdown
Contributor Author

pushed again to resolve spdx issue

@andrewd-zededa
andrewd-zededa force-pushed the eve-k-lh-snap-revert-tool branch from c468b25 to 91ec977 Compare November 17, 2026 00:08
@andrewd-zededa

Copy link
Copy Markdown
Contributor Author

CI notes on the remaining failures:

  • yetus (golangcilint, 1 new): the only new line in patch-golangcilint-result.txt is golangci-lint's summary pkg/kube/lh-snap-revert/0 issues.. Yetus counts every module's summary line (e.g. pkg/kube/cnirpc/0 issues. on master), so adding a Go module always adds one. The module itself has no findings.
  • osv-scan: the only finding is GO-2026-6859 in stdlib 1.26.10, fixed in 1.26.11. The module's toolchain go1.26.10 matches the Go in eve-alpine, which builds with GOTOOLCHAIN=local, so this goes away when eve-alpine's Go is bumped. The golang.org/x dependencies are on current versions.

@andrewd-zededa
andrewd-zededa force-pushed the eve-k-lh-snap-revert-tool branch from 91ec977 to 82a8fb5 Compare November 18, 2026 17:18
@andrewd-zededa

Copy link
Copy Markdown
Contributor Author

Pushed an update to 82a8fb5:

  • pvcs: a failed read of a PVC, its PV or its Longhorn volume now shows ? in the affected columns and prints a warning to stderr, instead
    of showing - as if the PVC had no Longhorn volume. A PVC that is not Bound or not backed by Longhorn still shows -.
  • list: if the volume cannot be read, the output now says so and notes that the snapshot tree may be stale, instead of skipping the
    attached check without a word.
  • Manual release command: if the label patch cannot be built, the printed recovery text names the labels to re-enable and the annotation to
    remove, instead of printing a kubectl patch with an empty -p ''.
  • Added TestListPVCsReadErrors to cover these cases.

Checked with make lh-snap-revert, go test -race on the package and make mini-yetus (no issues).

@andrewd-zededa andrewd-zededa mentioned this pull request Nov 20, 2026
3 of 7 tasks
@andrewd-zededa
andrewd-zededa force-pushed the eve-k-lh-snap-revert-tool branch from 82a8fb5 to c1923ec Compare November 20, 2026 05:54
@andrewd-zededa

Copy link
Copy Markdown
Contributor Author

Bumped toolchain in pkg/kube/lh-snap-revert/go.mod from go1.26.10 to go1.26.11. osv-scan flagged the new module for GO-2026-6859 (Go stdlib, fixed in 1.26.11), and this clears that check.

This only affects the scan. The real binary is still compiled with Go 1.26.10, because both eve-build-eve (make lh-snap-revert) and lfedge/eve-alpine (the pkg/kube image) ship Go 1.26.10 with GOTOOLCHAIN=local, so the toolchain line is ignored. The full fix is #7170, which moves both builder images to Go 1.26.11. This PR doesn't depend on it: it builds and passes the scan either way, and once #7170 lands the toolchain line matches the real compiler.

The remaining yetus -1 is a false positive. golangci-lint prints pkg/kube/lh-snap-revert/0 issues. for a clean module, and yetus counts that line as a new issue. Master's baseline already includes 7 of these lines from other modules.

andrewd-zededa and others added 2 commits November 19, 2026 22:56
Add the go.mod and vendored dependencies for the Longhorn snapshot
revert tool, a separate Go module in pkg/kube/lh-snap-revert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Andrew Durbin <andrewd@zededa.com>
Add an out-of-band recovery tool, for occasional use, that reverts the
Longhorn volume behind an EVE-K app PVC to a snapshot in place. The
PVC, its UID and the PV do not change. The tool changes only the
Longhorn volume and does not manage the app state. Scheduling,
retention and backup of snapshots stay with Longhorn and EVE, and
stopping the app stays with the controller. The kube image includes
it as /usr/bin/lh-snap-revert. It also builds as a standalone binary
with "make lh-snap-revert ZARCH=<arch>" for nodes of older releases
that run Longhorn v1.9.1.

The tool does not scale the app VMIRS, because pillar restores a VMIRS
that has 0 replicas. The operator pauses EVE's snapshot schedule with
storage.longhorn.snapshot.cron, so that the recurring job does not
delete the target snapshot, and stops the app from the controller. The
tool refuses to run while the PVC is in use. Longhorn runs recurring
jobs on attached volumes, so the tool sets the volume's recurring job
labels to disabled for the run and restores them after. It takes a
safety-net snapshot, attaches the volume with its own attachment ticket
and the frontend disabled, reverts, and releases the volume on exit or
failure. The snapshot comes first because the Longhorn v1.9.1 engine
crashes on a snapshot while its frontend is disabled.

A revert leaves the old snapshots on a branch. Longhorn cannot purge a
snapshot with two branches below it and can then keep the volume
attached, which blocks a later revert. "list" shows the snapshot tree
and "cleanup" deletes the old branch, so that a later revert stays
possible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Andrew Durbin <andrewd@zededa.com>
@andrewd-zededa
andrewd-zededa force-pushed the eve-k-lh-snap-revert-tool branch from c1923ec to 1bd631a Compare November 20, 2026 05:57
@andrewd-zededa

Copy link
Copy Markdown
Contributor Author

rebased for spdx issue

Comment thread .spdxignore
pkg/alpine/dnstest/vendor/
pkg/kube/stale-mount-cleanup/vendor/
pkg/storage-resizer/vendor/
pkg/kube/lh-snap-revert/vendor/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewd-zededa , this should be added to Yetus excludes as well (.yetus/excludes.txt)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.yetus/excludes.txt already has ^.+/vendor/.+, which covers pkg/kube/lh-snap-revert/vendor/, looks like the remaining -1 is the pkg/kube/lh-snap-revert/0 issues. line

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants