Skip to content

kubeapi: bound zedkube's Kubernetes API calls during failover - #7202

Draft
andrewd-zededa wants to merge 3 commits into
lf-edge:masterfrom
andrewd-zededa:eve-k-lh-api-timeouts
Draft

andrewd-zededa wants to merge 3 commits into
lf-edge:masterfrom
andrewd-zededa:eve-k-lh-api-timeouts

Conversation

@andrewd-zededa

Copy link
Copy Markdown
Contributor

Description

When a node in an eve-k cluster fails, a surviving node runs
DetachOldWorkload on zedkube's main loop to take over its apps. Several
Kubernetes API calls on that path used context.Background() or
context.TODO(), and GetKubeConfig sets no client timeout, so a
stalled API server held the loop until zedkube's watchdog rebooted the
node. A stall is likely just then, with one cluster member gone, and a
watchdog reboot at that point takes down a second node.

This PR bounds those calls and keeps the watchdog kicked while they run:

  • Each call reachable from DetachOldWorkload gets kubeAPITimeout, as
    the neighbouring code already does: the Longhorn volume and engine
    hand-over and replica delete, DeleteControlPlanePodsOnNode (node Get
    and Update, kubevirt and longhorn-system pod List and Delete) and the
    VolumeAttachment List and Delete.
  • Bounded calls can still add up, so the watchdog is kicked after each
    pod, replica and VolumeAttachment delete, volume hand-over and attach
    check. DeleteControlPlanePodsOnNode takes the watchdog function for
    that.
  • DetachUtilVmirsReplicaReset retries for up to two minutes while
    virt-api or the API server is unreachable, with nothing kicking the
    watchdog. It now takes the caller's watchdog function and kicks it on
    each try.
  • collectKubeStats already bounds its calls with one kubeAPITimeout
    context. PopulateKSI now takes that context and uses it for every
    call, the Longhorn snapshot List included, instead of a timeout per
    call that could add up past the watchdog across DaemonSets and PVCs.

PR dependencies

Depends on #7182, which rewrites the Longhorn writes in
longhorninfo.go that this PR bounds. Until #7182 merges, its commit
also shows in this PR's diff; review only the last two commits.

How to test and validate this PR

Automated: make -C pkg/pillar test. No new tests; the existing
TestPopulateKVIInner runs with the context-taking snapshot function.

On a 3-node eve-k cluster with a VM app on a 2-replica Longhorn volume,
power off the node running the app. On the node that takes it over:

  • zedkube logs checkAppsFailover: failover start, then
    DetachOldWorkload: set lhVol:<vol> to node:<node> err:<nil>,
    volumeattachment list empty finally,
    DetachOldWorkload Completed failover ... duration: and
    checkAppsFailover: failover complete.
  • No StillRunning(zedkube) took a long time line appears, even when the
    vmirs scale reset runs its full two minutes. Without this PR that reset
    left zedkube 121 s past its last watchdog kick.
  • No can't get KSI errors before or after the failure.
  • The VMI runs on the surviving node with its volume attached.

Tested on an amd64 3-node cluster with two failovers of the same app.
Each time DetachOldWorkload completed in under 10 s and the app came
up on the surviving node. The first run, without the vmirs reset kick,
logged the 121 s watchdog warning; the second, with it, did not.

Changelog notes

eve-k: a node taking over apps from a failed cluster node no longer
risks a watchdog reboot when the Kubernetes API server is slow to
respond.

PR Backports

- 17.0-stable: No, depends on #7182, which is not backported.
- 16.0-stable: No, same reason.
- 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.

🤖 Generated with Claude Code

andrewd-zededa and others added 3 commits November 23, 2026 10:30
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>
Kubernetes API calls on two paths that run on zedkube's main loop used
context.Background() or context.TODO(), and GetKubeConfig sets no client
timeout, so a stalled API server held the loop until zedkube's watchdog
rebooted the node. One path is DetachOldWorkload, which a node runs
while it takes over the apps of a node that just failed: the stall it
waits out is likely then, and a watchdog reboot there takes down a
second node. The other is the Longhorn storage info collectKubeStats
gathers.

DetachOldWorkload: give each call kubeAPITimeout, as the neighbouring
code already does. That covers the volume and engine hand-over and the
replica delete in longhorninfo.go, DeleteControlPlanePodsOnNode (node
Get and Update, the kubevirt and longhorn-system pod List and Delete)
and the VolumeAttachment List and Delete. Bounded calls can still add
up, so kick the watchdog after each pod delete, replica delete,
VolumeAttachment delete, volume hand-over and attach check.
DeleteControlPlanePodsOnNode takes the watchdog function for that.

collectKubeStats already bounds its calls with one kubeAPITimeout
context. PopulateKSI now takes that context and uses it for every call,
the snapshot List included, instead of a timeout per call: one per
DaemonSet and PVC could add up past the watchdog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Andrew Durbin <andrewd@zededa.com>
DetachUtilVmirsReplicaReset retries on zedkube's main loop for up to
two minutes while virt-api is unreachable, which is common when the
failed node was hosting it. Nothing kicked the watchdog meanwhile, so a
failover left zedkube 121s past its last kick: over the warning
threshold and about a minute short of the error threshold, on the node
that is taking over the workload.

Pass the caller's wdFunc in and kick it on each try.

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

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 6.66667% with 98 lines in your changes missing coverage. Please review.
✅ Project coverage is 31.46%. Comparing base (9267176) to head (5515e0b).

Files with missing lines Patch % Lines
pkg/pillar/kubeapi/longhorninfo.go 13.20% 45 Missing and 1 partial ⚠️
pkg/pillar/kubeapi/kubeapi.go 0.00% 35 Missing ⚠️
pkg/pillar/kubeapi/vitoapiserver.go 0.00% 9 Missing ⚠️
pkg/pillar/kubeapi/longhornsnap.go 0.00% 6 Missing ⚠️
pkg/pillar/cmd/zedkube/failover.go 0.00% 1 Missing ⚠️
pkg/pillar/cmd/zedkube/kubestatscollect.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7202      +/-   ##
==========================================
+ Coverage   30.99%   31.46%   +0.46%     
==========================================
  Files         562      573      +11     
  Lines      101231   102882    +1651     
==========================================
+ Hits        31380    32371     +991     
- Misses      67412    68115     +703     
+ Partials     2439     2396      -43     

☔ 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.

@andrewd-zededa

Copy link
Copy Markdown
Contributor Author

Will pull out of draft once #7182 merges

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.

1 participant