|
| 1 | +package picker |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins" |
| 7 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" |
| 8 | + logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging" |
| 9 | +) |
| 10 | + |
| 11 | +// MaxScorePicker picks the pod with the maximum score from the list of |
| 12 | +// candidates. |
| 13 | +type MaxScorePicker struct{} |
| 14 | + |
| 15 | +var _ plugins.Picker = &MaxScorePicker{} |
| 16 | + |
| 17 | +// Name returns the name of the picker. |
| 18 | +func (msp *MaxScorePicker) Name() string { |
| 19 | + return "max_score" |
| 20 | +} |
| 21 | + |
| 22 | +// 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) |
| 29 | + |
| 30 | + maxScore := 0.0 |
| 31 | + 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) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 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) |
| 51 | + } |
| 52 | + |
| 53 | + debugLogger.Info(fmt.Sprintf("Selected pod with max score (%f): %+v", maxScore, winners[0])) |
| 54 | + return &types.Result{TargetPod: winners[0]} |
| 55 | +} |
0 commit comments