Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions utils/kubernetes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func GetServiceEndpoint(ctx context.Context, client kubernetes.Interface, opts *
// GetEndpoint returns those endpoints in the given service which match the selector. Eg: service name = "client"
func GetEndpoint(ctx context.Context, opts *ServiceOptions, obj *corev1.Service) (*utils.Endpoint, error) {
var nodePort, clusterPort int32
var matched bool
endpoint := utils.Endpoint{}
if opts.WorkerNodeIP == "" {
opts.WorkerNodeIP = "localhost"
Expand All @@ -41,9 +42,16 @@ func GetEndpoint(ctx context.Context, opts *ServiceOptions, obj *corev1.Service)
nodePort = port.NodePort
clusterPort = port.Port
if opts.PortSelector != "" && port.Name == opts.PortSelector {
matched = true
break
}
}
// A non-empty PortSelector that matched no port would otherwise fall through
// with the last port's values and be returned as if it were the requested
// port; report that the endpoint was not found instead.
if opts.PortSelector != "" && !matched {
return nil, ErrEndpointNotFound
}
// get clusterip endpoint
endpoint.Internal = &utils.HostPort{
Address: obj.Spec.ClusterIP,
Expand Down
29 changes: 29 additions & 0 deletions utils/kubernetes/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,35 @@ func TestGetEndpoint(t *testing.T) {
},
wantErr: false,
},
{
name: "PortSelector matching no port returns ErrEndpointNotFound",
args: args{
ctx: context.TODO(),
opts: &ServiceOptions{
PortSelector: "does-not-exist",
Mock: &utils.MockOptions{
DesiredEndpoint: "1.1.1.1:1001",
},
},
obj: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test_service",
Namespace: "default",
Annotations: map[string]string{},
},
Spec: v1.ServiceSpec{
ClusterIP: "1.1.1.1",
Ports: []v1.ServicePort{
{Name: "test_port_1", Port: 1000},
{Name: "test_port_2", Port: 1001},
},
Type: v1.ServiceTypeClusterIP,
},
},
},
want: nil,
wantErr: true,
},
Comment on lines +551 to +553

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.

}

for _, tt := range tests {
Expand Down