Skip to content

Commit 62526b1

Browse files
committed
- minor refactoring
- updated scorer interface Signed-off-by: Maroon Ayoub <maroon.ayoub@ibm.com>
1 parent 677455b commit 62526b1

File tree

5 files changed

+64
-68
lines changed

5 files changed

+64
-68
lines changed

pkg/epp/scheduling/max_score_picker.go

-65
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package pickers
2+
3+
import (
4+
"fmt"
5+
6+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
7+
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
8+
)
9+
10+
// MaxScorePicker picks the pod with the maximum score from the list of
11+
// candidates.
12+
type MaxScorePicker struct{}
13+
14+
var _ types.Picker = &MaxScorePicker{}
15+
16+
// Name returns the name of the picker.
17+
func (msp *MaxScorePicker) Name() string {
18+
return "max-score"
19+
}
20+
21+
// Pick selects the pod with the maximum score from the list of candidates.
22+
func (msp *MaxScorePicker) Pick(ctx *types.Context, pods []types.Pod) (*types.Result, error) {
23+
debugLogger := ctx.Logger.V(logutil.DEBUG).WithName("max-score-picker")
24+
debugLogger.Info(fmt.Sprintf("Selecting the pod with the max score from %d candidates: %+v",
25+
len(pods), pods))
26+
27+
winners := make([]types.Pod, 0)
28+
29+
maxScore := 0.0
30+
for _, pod := range pods {
31+
score := pod.Score()
32+
if score > maxScore {
33+
maxScore = score
34+
winners = []types.Pod{pod}
35+
} else if score == maxScore {
36+
winners = append(winners, pod)
37+
}
38+
}
39+
40+
if len(winners) == 0 {
41+
return nil, nil
42+
}
43+
44+
if len(winners) > 1 {
45+
debugLogger.Info(fmt.Sprintf("Multiple pods have the same max score (%f): %+v",
46+
maxScore, winners))
47+
48+
randomPicker := RandomPicker{}
49+
return randomPicker.Pick(ctx, winners)
50+
}
51+
52+
debugLogger.Info(fmt.Sprintf("Selected pod with max score (%f): %+v", maxScore, winners[0]))
53+
return &types.Result{TargetPod: winners[0]}, nil
54+
}

pkg/epp/scheduling/plugins/picker.go renamed to pkg/epp/scheduling/plugins/pickers/random.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package plugins
17+
package pickers
1818

1919
import (
2020
"fmt"
@@ -24,12 +24,17 @@ import (
2424
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
2525
)
2626

27+
// RandomPicker picks a pod randomly from the list of candidates.
2728
type RandomPicker struct{}
2829

30+
var _ types.Picker = &RandomPicker{}
31+
32+
// Name returns the name of the picker.
2933
func (rp *RandomPicker) Name() string {
3034
return "random"
3135
}
3236

37+
// Pick selects a random pod from the list of candidates.
3338
func (rp *RandomPicker) Pick(ctx *types.Context, pods []types.Pod) (*types.Result, error) {
3439
ctx.Logger.V(logutil.DEBUG).Info(fmt.Sprintf("Selecting a random pod from %d candidates: %+v", len(pods), pods))
3540
i := rand.Intn(len(pods))

pkg/epp/scheduling/scheduler.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
2727
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics"
2828
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins"
29+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins/pickers"
2930
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
3031
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
3132
)
@@ -74,7 +75,7 @@ func NewScheduler(datastore Datastore) *Scheduler {
7475
postSchedulePlugins: []types.PostSchedule{},
7576
scorers: []types.Scorer{},
7677
filters: []types.Filter{defaultPlugin},
77-
picker: &maxScorePicker{},
78+
picker: &pickers.MaxScorePicker{},
7879
}
7980
}
8081

@@ -198,7 +199,7 @@ func runScorersForPod(ctx *types.Context, scorers []types.Scorer, pod types.Pod)
198199
}
199200

200201
type defaultPlugin struct {
201-
plugins.RandomPicker
202+
pickers.RandomPicker
202203
}
203204

204205
func (p *defaultPlugin) Name() string {

pkg/epp/scheduling/types/interfaces.go

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Filter interface {
6363
}
6464

6565
// Scorer defines the interface for scoring pods based on context.
66+
// Scorers must return a score between 0 and 1, where 1 is the best score.
6667
type Scorer interface {
6768
Plugin
6869
Score(ctx *Context, pod Pod) (float64, error)

0 commit comments

Comments
 (0)