Skip to content

fix: return ErrEndpointNotFound when PortSelector matches no port - #1084

Open
singhharsh1708 wants to merge 2 commits into
meshery:masterfrom
singhharsh1708:fix/getendpoint-unmatched-portselector
Open

fix: return ErrEndpointNotFound when PortSelector matches no port#1084
singhharsh1708 wants to merge 2 commits into
meshery:masterfrom
singhharsh1708:fix/getendpoint-unmatched-portselector

Conversation

@singhharsh1708

@singhharsh1708 singhharsh1708 commented Aug 4, 2026

Copy link
Copy Markdown

What

GetEndpoint ranges over a service's ports and only breaks on a name match:

for _, port := range obj.Spec.Ports {
    nodePort = port.NodePort
    clusterPort = port.Port
    if opts.PortSelector != "" && port.Name == opts.PortSelector {
        break
    }
}

If a non-empty PortSelector matches no port, the loop runs to completion and nodePort/clusterPort keep the last port's values, which are then returned as if they were the requested port, with a nil error — 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 a monitor port, 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 ErrEndpointNotFound when a non-empty PortSelector matches no port. The empty-selector behavior is unchanged, so existing callers don't regress.

Test

Added a case to TestGetEndpoint: a PortSelector that matches no port now returns ErrEndpointNotFound (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

    • Corrected service endpoint selection when a requested port does not exist.
    • The system now reports that no endpoint was found instead of returning incorrect port information.
  • Tests

    • Added coverage for missing port selections on ClusterIP services.

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>
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

GetEndpoint now returns ErrEndpointNotFound when a non-empty PortSelector matches no service port. A table-driven test verifies the nil endpoint and error for a ClusterIP service.

Changes

Endpoint selection

Layer / File(s) Summary
Validate selected service ports
utils/kubernetes/service.go, utils/kubernetes/service_test.go
GetEndpoint tracks whether the selector matched a service port and returns ErrEndpointNotFound when no match exists. The test verifies the nil endpoint and error response.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the fix for unmatched PortSelector values in GetEndpoint.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 762c011 and 584cbfe.

📒 Files selected for processing (2)
  • utils/kubernetes/service.go
  • utils/kubernetes/service_test.go

Comment on lines +551 to +553
want: nil,
wantErr: true,
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

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