Skip to content

manage: only annotate a pod as kmesh managed after xdp/tc attach succ… - #1847

Open
Anand-240 wants to merge 1 commit into
kmesh-net:mainfrom
Anand-240:fix/manage-controller-attach-gating
Open

manage: only annotate a pod as kmesh managed after xdp/tc attach succ…#1847
Anand-240 wants to merge 1 commit into
kmesh-net:mainfrom
Anand-240:fix/manage-controller-attach-gating

Conversation

@Anand-240

Copy link
Copy Markdown

Pods are only annotated as managed by Kmesh after the xdp and tc programs that actually redirect their traffic are attached, instead of the annotation being applied unconditionally while attach errors were silently discarded.

What type of PR is this?

/kind bug

What this PR does / why we need it:

In pkg/controller/manage/manage_controller.go, enableKmeshManage queued the pod for the Kmesh redirection annotation and then called linkXdp and linkTc, discarding their return values with _ =. If either attach failed, the pod was still patched with the "kmesh redirection enabled" annotation, so the control plane and anyone inspecting the pod would believe it was fully enrolled while no xdp or tc program was actually attached to its interface. Traffic for that pod would then bypass the Kmesh dataplane with no error surfaced anywhere except a daemon log line, and no automatic retry once the pod stopped receiving further updates.

This PR makes the annotation step depend on the attach step actually succeeding:

  • enableKmeshManage now enqueues an ActionAttach item instead of calling linkXdp/linkTc directly. disableKmeshManage enqueues ActionDetach the same way.
  • syncPod runs the real attach or detach work through two new helpers, attachPod and detachPod. Only on success does it enqueue ActionAddAnnotation or ActionDeleteAnnotation, the actions that actually patch the pod.
  • On failure syncPod returns an error, so the existing processItems retry path picks it up and retries with the controller's existing MaxRetries and rate limited backoff, the same mechanism already used for the annotation actions. No new retry logic was added.

Which issue(s) this PR fixes:
Fixes #1846

Special notes for your reviewer:

  • manage_controller_test.go mocks queue.AddRateLimited so tests can run synchronously without the real workqueue loop. That mock only understood ActionAddAnnotation/ActionDeleteAnnotation, so it has been replaced with a shared simulateQueueItem helper that mirrors syncPod's full switch, including the ActionAttach to ActionAddAnnotation and ActionDetach to ActionDeleteAnnotation chaining.
  • Both existing tests construct the controller with xdpProgFd=0, tcProgFd=-1, mode="". With that configuration linkXdp returns nil immediately since the mode is not Dual-Engine, and linkTc returns nil immediately since tcProgFd == -1, so attachPod/detachPod never touch a real network namespace in these tests.
  • I was not able to run go build/go test on this package locally since it only compiles on Linux (it pulls in netns, cilium/ebpf ringbuf, and ethtool, none of which build on macOS). Verified with gofmt -e for syntax and a full manual trace of the changed paths against both existing tests. Would appreciate this being confirmed by CI or a reviewer running it on Linux.

Does this PR introduce a user-facing change?:

fix: a pod is no longer annotated as managed by Kmesh unless its xdp and tc dataplane programs actually attach successfully. Attach and detach failures are now retried instead of being silently ignored.

…eeds

Previously the redirection annotation was queued before linkXdp and linkTc ran, and their errors were discarded. A pod could end up marked as managed while no dataplane program was actually attached to it, so traffic would silently bypass Kmesh. This routes the attach and detach steps through the existing workqueue retry path (MaxRetries, backoff) and only queues the annotation change once attach or detach actually succeeds.
Copilot AI review requested due to automatic review settings August 2, 2026 15:06
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@kmesh-bot kmesh-bot added the kind/bug Something isn't working label Aug 2, 2026
@kmesh-bot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign nlgwcy for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copilot AI left a comment

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.

Pull request overview

This PR fixes a correctness gap in the Kmesh manage controller by ensuring pods are only annotated as “Kmesh-managed” after the XDP/TC dataplane programs have successfully attached, and by routing attach/detach failures through the controller’s existing rate-limited retry mechanism.

Changes:

  • Introduces ActionAttach/ActionDetach to gate annotation add/delete behind successful dataplane attach/detach.
  • Refactors syncPod to perform attach/detach work and enqueue annotation actions only on success.
  • Updates unit tests to simulate the expanded action switch (including attach/detach → annotation chaining).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
pkg/controller/manage/manage_controller.go Adds attach/detach actions and gates annotation on successful XDP/TC operations with retry on failure.
pkg/controller/manage/manage_controller_test.go Replaces the queue mock with simulateQueueItem to mirror the new action flow in tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if !utils.ShouldEnroll(pod, namespace) {
return nil
}
nspath, _ := ns.GetPodNSpath(pod)
Comment on lines +421 to 427
if err := unlinkXdp(nspath, mode); err != nil {
return fmt.Errorf("xdp: %w", err)
}
if err := unlinkTc(nspath, tcProgFd); err != nil {
return fmt.Errorf("tc: %w", err)
}
return nil
Comment on lines +397 to +401
case ActionDetach:
nspath, _ := ns.GetPodNSpath(pod)
if err := detachPod(nspath, c.tcProgFd, c.mode); err != nil {
return fmt.Errorf("failed to detach kmesh dataplane programs for pod %s/%s: %v", pod.Namespace, pod.Name, err)
}
Comment on lines +410 to +416
if err := linkXdp(nspath, xdpProgFd, mode); err != nil {
return fmt.Errorf("xdp: %w", err)
}
if err := linkTc(nspath, tcProgFd); err != nil {
return fmt.Errorf("tc: %w", err)
}
return nil
Comment on lines +216 to +221
case ActionDetach:
nspath, _ := kmeshns.GetPodNSpath(pod)
if err := detachPod(nspath, controller.tcProgFd, controller.mode); err != nil {
t.Errorf("failed to detach pod %s/%s: %v", pod.Namespace, pod.Name, err)
return
}
if !utils.ShouldEnroll(pod, namespace) {
return
}
nspath, _ := kmeshns.GetPodNSpath(pod)
@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 13.88889% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 39.42%. Comparing base (63e0279) to head (2d1391d).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/controller/manage/manage_controller.go 13.88% 27 Missing and 4 partials ⚠️

❌ Your patch check has failed because the patch coverage (13.88%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Files with missing lines Coverage Δ
pkg/controller/manage/manage_controller.go 48.98% <13.88%> (-3.67%) ⬇️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c88ef30...2d1391d. Read the comment docs.

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

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

Labels

kind/bug Something isn't working size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kmesh-daemon marks a pod as Kmesh-managed even when eBPF TC/XDP attachment silently fails, leaving the pod unenforced but reported as enrolled

3 participants