Skip to content

kubeapi: patch Longhorn objects instead of replacing them - #7182

Open
andrewd-zededa wants to merge 1 commit into
lf-edge:masterfrom
andrewd-zededa:eve-k-lh-patch-writes
Open

andrewd-zededa wants to merge 1 commit into
lf-edge:masterfrom
andrewd-zededa:eve-k-lh-patch-writes

Conversation

@andrewd-zededa

Copy link
Copy Markdown
Contributor

Description

pillar changes three kinds of Longhorn object: a volume and its engines
when a workload fails over to another node (longhornVolumeSetNode,
called from DetachOldWorkload), and the EVE recurring snapshot job when
storage.longhorn.snapshot.cron changes (SetLonghornRecurringSnapshot).
Each one read the object, changed a field and wrote the whole object back
with Update.

The object passes through pillar's vendored Longhorn types on the way, so
any field those types do not know is sent back as absent and the API
server clears it. With the Longhorn EVE ships today the types match and
nothing is lost, but newer Longhorn releases add spec fields to volumes,
engines and recurring jobs. Once the cluster runs a newer Longhorn than
the vendored client, these writes would reset those fields. This PR
removes that hazard in preparation for upgrading the Longhorn version in
use, and keeps it removed independent of when the vendored client is
bumped.

The writes are now JSON merge patches that name only the fields pillar
changes:

  • volume: spec.nodeID, plus spec.backupTargetName: default when it is
    empty (Longhorn's webhook rejects any update to a volume without one);
  • engine: spec.nodeID, for each engine of the volume;
  • recurring job: spec.cron.

Two related changes in the same function:

  • The volume update also set status.ownerID and status.currentNodeID.
    The volume CRD has a status subresource, so the API server already
    ignored those in a plain Update; they are dropped. Status belongs to
    longhorn-manager.
  • A failed engine List returned nil, so failover reported success
    without moving the engines. It now returns the error.

The Longhorn node and setting writes are unchanged.

How to test and validate this PR

Automated: make -C pkg/pillar test runs the new
pkg/pillar/kubeapi/longhornpatch_test.go, which checks the exact patch
bodies (node only, and node plus default backup target).

On a 3-node eve-k cluster, exercise the two actions that write Longhorn
objects. In each, first set by hand a spec field pillar does not write,
and check it survives:

  1. App failover (volume and engine patches): power off the node running
    a VM app with a replicated volume. The app should restart on another
    node, with the Longhorn volume and its engine moved to that node.
    zedkube's DetachOldWorkload: set lhVol:<vol> to node:<node> err: log
    line should end in err:<nil>; a failed write shows there as
    patch volume ..., patch engine ... or list engines ....
  2. Recurring snapshot schedule (recurring job patch): change
    storage.longhorn.snapshot.cron from the controller. The
    default-eve-kube-app-snapshot RecurringJob should take the new cron
    and keep its other settings.

Both passed on an amd64 cluster.

Changelog notes

None. Internal change to how eve-k updates Longhorn objects; no
behaviour change with the Longhorn version EVE ships today.

PR Backports

- 17.0-stable: No, ships the same Longhorn as its vendored client, so no fields are lost.
- 16.0-stable: No, same reason (it has the failover path but not the recurring snapshot job).
- 14.5-stable: No, the code is not there.
- 13.4-stable: No, the code is not 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.

@rene

rene commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@claude

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @rene's task in 2m 31s —— View job


Review: kubeapi: patch Longhorn objects instead of replacing them

  • Read the diff (longhorninfo.go, longhornsnap.go, longhornpatch_test.go)
  • Verified patched JSON field names against vendored Longhorn types
  • Verified the status-subresource claim
  • Checked caller behavior for the changed error return

Verdict: looks correct, no blocking issues. The switch from full-object Update to JSON merge patches (MergePatchType) is the right tool here — strategic merge patch isn't supported on CRDs, and a merge patch touches only the named keys, which is exactly the goal of not clearing fields the vendored types don't know about.

Checks I ran against the vendored types (v1beta2):

  • spec.cron on RecurringJob ✓ (Cron string \json:"cron"``)
  • spec.nodeID / spec.backupTargetName on Volume ✓
  • spec.nodeID on Engine ✓ (inlined via embedded InstanceSpec \json:""`, so spec.nodeID` is the correct merge-patch path)

So a wrong field name silently pruned by the CRD schema — the main risk with merge patches — is not a concern.

Other points confirmed:

  • Dropping status.OwnerID / status.CurrentNodeID: the Volume type carries // +kubebuilder:subresource:status, so a plain Update never persisted those anyway. Removing them is a genuine no-op, as the PR claims. ✓
  • Engine List now returns the error instead of nil: the caller in kubeapi.go:1200 only logs it and proceeds, and a failed List meant engines weren't moved regardless — so this is strictly an improvement in observability with no harmful behavior change. ✓
  • engPatch built once and reused across engines: fine, the body is identical and immutable.
  • Determinism of the patch bytes the test pins (backupTargetName before nodeID) relies on json.Marshal sorting map keys, which is guaranteed — the tests are stable. ✓

Minor (non-blocking) observation: longhornsnap.go wraps patch errors with %v while the new longhorninfo.go code uses %w. Not worth changing — the callers don't unwrap these errors, and %v matches the pre-existing style in that file.

Nothing to fix.
· branch eve-k-lh-patch-writes

pillar changed Longhorn volumes, engines and the EVE recurring snapshot
job by reading the object, changing a field and writing the whole
object back with Update. The object goes through pillar's vendored
Longhorn types on the way, so any field those types do not know is
sent back as absent and cleared. With the Longhorn EVE ships today the
types match and nothing is lost, but newer Longhorn releases add spec
fields to these objects. This prepares for upgrading the Longhorn
version in use.

Send JSON merge patches that name only the fields pillar changes:

- volume: spec.nodeID, plus spec.backupTargetName when it is empty,
  since Longhorn's webhook rejects any update to a volume without one;
- engine: spec.nodeID, for each engine of the volume;
- recurring job: spec.cron.

The volume update also set status.ownerID and status.currentNodeID.
The volume CRD has a status subresource, so the API server already
ignored those in a plain Update; they are dropped. Status belongs to
longhorn-manager.

Listing the engines failed silently before; it now returns the error.

The Longhorn node and setting writes are left as they are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Andrew Durbin <andrewd@zededa.com>
@andrewd-zededa

Copy link
Copy Markdown
Contributor Author

Latest push is a rebase onto current master only, no code changes, to clear the previous CI failures.

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.

2 participants