fix: return ErrEndpointNotFound when PortSelector matches no port - #1084
fix: return ErrEndpointNotFound when PortSelector matches no port#1084singhharsh1708 wants to merge 2 commits into
Conversation
GetEndpoint ranged over the service ports and only broke out of the loop on a name match, so a non-empty PortSelector that matched nothing fell through with the last port's values and returned them as if they were the requested port, with a nil error. Callers probing a named port (e.g. the broker/meshsync "monitor" port) then targeted the wrong port and reported a false connectivity status instead of surfacing that the endpoint was not found. Track whether the selector matched and return the existing ErrEndpointNotFound when a non-empty PortSelector matches no port. The empty-selector path is unchanged. Signed-off-by: Harsh Singh <hs1663531@gmail.com>
📝 WalkthroughWalkthrough
ChangesEndpoint selection
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@utils/kubernetes/service_test.go`:
- Around line 551-553: Update the affected test case in the shared test table to
set its expected error to ErrEndpointNotFound, then strengthen the assertion to
compare the returned error against that value or its stable error code instead
of checking only that an error exists.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 18695638-d53f-4ca1-b4a6-469f29d56a47
📒 Files selected for processing (2)
utils/kubernetes/service.goutils/kubernetes/service_test.go
| want: nil, | ||
| wantErr: true, | ||
| }, |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert the specific endpoint error.
This case sets only wantErr: true. The shared assertion checks only whether err is non-nil. The test can therefore pass for an incorrect error. Store ErrEndpointNotFound as the expected error and compare the returned error with that value or its stable error code.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@utils/kubernetes/service_test.go` around lines 551 - 553, Update the affected
test case in the shared test table to set its expected error to
ErrEndpointNotFound, then strengthen the assertion to compare the returned error
against that value or its stable error code instead of checking only that an
error exists.
What
GetEndpointranges over a service's ports and onlybreaks on a name match:If a non-empty
PortSelectormatches no port, the loop runs to completion andnodePort/clusterPortkeep the last port's values, which are then returned as if they were the requested port, with anilerror — contradicting the doc comment ("endpoints which match the selector").Real impact: the broker and meshsync controllers probe a port named
monitor(GetEndpointForPort("monitor")→ServiceOptions{PortSelector: "monitor"}). Against a service that doesn't expose amonitorport, the probe silently targets the last port and reports a false connectivity status instead of "endpoint not found".Fix
Track whether the selector matched and return the existing
ErrEndpointNotFoundwhen a non-emptyPortSelectormatches no port. The empty-selector behavior is unchanged, so existing callers don't regress.Test
Added a case to
TestGetEndpoint: aPortSelectorthat matches no port now returnsErrEndpointNotFound(want: nil, wantErr: true). It fails on the current code (the call returns the last port with a nil error) and passes with the fix; the existing empty-selector and matching-selector cases are unchanged.Summary by CodeRabbit
Bug Fixes
Tests