Skip to content

Commit 677455b

Browse files
committed
Add max score picker
Signed-off-by: Maya Barnea <[email protected]>
1 parent c2ac5b9 commit 677455b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package scheduling
18+
19+
import (
20+
"fmt"
21+
"math/rand/v2"
22+
23+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
24+
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
25+
)
26+
27+
type maxScorePicker struct {
28+
}
29+
30+
func (p *maxScorePicker) Name() string {
31+
return "max score picker"
32+
}
33+
34+
func (p *maxScorePicker) Pick(ctx *types.Context, pods []types.Pod) (*types.Result, error) {
35+
ctx.Logger.V(logutil.DEBUG).Info(fmt.Sprintf("Selecting a pod with maximum score from %d candidates: %+v", len(pods), pods))
36+
37+
// select pod with maximum score, if more than one with the max score - use random pods from the list
38+
var highestScoreTargets []types.Pod
39+
// score weights cound be negative
40+
maxScore := 0.0
41+
isFirst := true
42+
43+
for _, pod := range pods {
44+
if isFirst {
45+
maxScore = pod.Score()
46+
highestScoreTargets = []types.Pod{pod}
47+
isFirst = false
48+
} else {
49+
if pod.Score() > maxScore {
50+
maxScore = pod.Score()
51+
highestScoreTargets = []types.Pod{pod}
52+
} else if pod.Score() == maxScore {
53+
highestScoreTargets = append(highestScoreTargets, pod)
54+
}
55+
}
56+
}
57+
58+
// single pod with max score
59+
if len(highestScoreTargets) == 1 {
60+
return &types.Result{TargetPod: highestScoreTargets[0]}, nil
61+
}
62+
63+
// select random pod from list of pods with max score
64+
return &types.Result{TargetPod: highestScoreTargets[rand.IntN(len(highestScoreTargets))]}, nil
65+
}

pkg/epp/scheduling/scheduler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func NewScheduler(datastore Datastore) *Scheduler {
7474
postSchedulePlugins: []types.PostSchedule{},
7575
scorers: []types.Scorer{},
7676
filters: []types.Filter{defaultPlugin},
77-
picker: defaultPlugin,
77+
picker: &maxScorePicker{},
7878
}
7979
}
8080

0 commit comments

Comments
 (0)