@@ -8,48 +8,42 @@ import (
8
8
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
9
9
)
10
10
11
- // MaxScorePicker picks the pod with the maximum score from the list of
12
- // candidates.
13
- type MaxScorePicker struct {}
14
-
15
11
var _ plugins.Picker = & MaxScorePicker {}
16
12
13
+ func NewMaxScorePicker () plugins.Picker {
14
+ return & MaxScorePicker {
15
+ random : & RandomPicker {},
16
+ }
17
+ }
18
+
19
+ // MaxScorePicker picks the pod with the maximum score from the list of candidates.
20
+ type MaxScorePicker struct {
21
+ random * RandomPicker
22
+ }
23
+
17
24
// Name returns the name of the picker.
18
- func (msp * MaxScorePicker ) Name () string {
25
+ func (p * MaxScorePicker ) Name () string {
19
26
return "max_score"
20
27
}
21
28
22
29
// Pick selects the pod with the maximum score from the list of candidates.
23
- func (msp * MaxScorePicker ) Pick (ctx * types.SchedulingContext , scoredPods []* types.ScoredPod ) * types.Result {
24
- debugLogger := ctx .Logger .V (logutil .DEBUG ).WithName ("max-score-picker" )
25
- debugLogger .Info (fmt .Sprintf ("Selecting the pod with the max score from %d candidates: %+v" ,
26
- len (scoredPods ), scoredPods ))
27
-
28
- winners := make ([]* types.ScoredPod , 0 )
30
+ func (p * MaxScorePicker ) Pick (ctx * types.SchedulingContext , scoredPods []* types.ScoredPod ) * types.Result {
31
+ ctx .Logger .V (logutil .DEBUG ).Info (fmt .Sprintf ("Selecting a pod with the max score from %d candidates: %+v" , len (scoredPods ), scoredPods ))
29
32
30
- maxScore := 0.0
33
+ highestScorePods := []* types.ScoredPod {}
34
+ maxScore := - 1.0 // pods min score is 0, putting value lower than 0 in order to find at least one pod as highest
31
35
for _ , pod := range scoredPods {
32
- score := pod .Score
33
- if score > maxScore {
34
- maxScore = score
35
- winners = []* types.ScoredPod {pod }
36
- } else if score == maxScore {
37
- winners = append (winners , pod )
36
+ if pod .Score > maxScore {
37
+ maxScore = pod .Score
38
+ highestScorePods = []* types.ScoredPod {pod }
39
+ } else if pod .Score == maxScore {
40
+ highestScorePods = append (highestScorePods , pod )
38
41
}
39
42
}
40
43
41
- if len (winners ) == 0 {
42
- return nil
43
- }
44
-
45
- if len (winners ) > 1 {
46
- debugLogger .Info (fmt .Sprintf ("Multiple pods have the same max score (%f): %+v" ,
47
- maxScore , winners ))
48
-
49
- randomPicker := RandomPicker {}
50
- return randomPicker .Pick (ctx , winners )
44
+ if len (highestScorePods ) > 1 {
45
+ return p .random .Pick (ctx , highestScorePods ) // pick randomly from the highest score pods
51
46
}
52
47
53
- debugLogger .Info (fmt .Sprintf ("Selected pod with max score (%f): %+v" , maxScore , winners [0 ]))
54
- return & types.Result {TargetPod : winners [0 ]}
48
+ return & types.Result {TargetPod : highestScorePods [0 ]}
55
49
}
0 commit comments