Skip to content

EPP: Update GetRandomPod() to return nil if no pods exist #731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
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
3 changes: 3 additions & 0 deletions pkg/epp/handlers/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func (s *StreamingServer) HandleRequestHeaders(ctx context.Context, reqCtx *Requ
// The above PR will address endpoint admission, but currently any request without a body will be
// routed to a random upstream pod.
pod := GetRandomPod(s.datastore)
if pod == nil {
return errutil.Error{Code: errutil.Internal, Msg: "no pods available in datastore"}
}
pool, err := s.datastore.PoolGet()
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions pkg/epp/handlers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ func RandomWeightedDraw(logger logr.Logger, model *v1alpha2.InferenceModel, seed

func GetRandomPod(ds datastore.Datastore) *backendmetrics.Pod {
pods := ds.PodGetAll()
if len(pods) == 0 {
return nil
}
number := rand.Intn(len(pods))
pod := pods[number]
return pod.GetPod()
Expand Down
55 changes: 55 additions & 0 deletions pkg/epp/handlers/streamingserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ package handlers

import (
"testing"
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/gateway-api-inference-extension/api/v1alpha2"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
)

Expand Down Expand Up @@ -126,6 +132,55 @@ func TestRandomWeightedDraw(t *testing.T) {
}
}

func TestGetRandomPod(t *testing.T) {
tests := []struct {
name string
storePods []*corev1.Pod
expectNil bool
}{
{
name: "No pods available",
storePods: []*corev1.Pod{},
expectNil: true,
},
{
name: "Single pod available",
storePods: []*corev1.Pod{
{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}},
},
expectNil: false,
},
{
name: "Multiple pods available",
storePods: []*corev1.Pod{
{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}},
{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}},
{ObjectMeta: metav1.ObjectMeta{Name: "pod3"}},
},
expectNil: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pmf := metrics.NewPodMetricsFactory(&metrics.FakePodMetricsClient{}, time.Millisecond)
ds := datastore.NewDatastore(t.Context(), pmf)
for _, pod := range test.storePods {
ds.PodUpdateOrAddIfNotExist(pod)
}

gotPod := GetRandomPod(ds)

if test.expectNil && gotPod != nil {
t.Errorf("expected nil pod, got: %v", gotPod)
}
if !test.expectNil && gotPod == nil {
t.Errorf("expected non-nil pod, got nil")
}
})
}
}

func pointer(v int32) *int32 {
return &v
}