|
| 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 | + |
| 22 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins" |
| 23 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins/scorer" |
| 24 | +) |
| 25 | + |
| 26 | +// NewSchedulerConfig creates a new SchedulerConfig object and returns its pointer. |
| 27 | +func NewSchedulerConfig() *SchedulerConfig { |
| 28 | + return &SchedulerConfig{ |
| 29 | + preSchedulePlugins: []plugins.PreSchedule{}, |
| 30 | + filters: []plugins.Filter{}, |
| 31 | + scorers: map[plugins.Scorer]int{}, |
| 32 | + postSchedulePlugins: []plugins.PostSchedule{}, |
| 33 | + postResponsePlugins: []plugins.PostResponse{}, |
| 34 | + // picker remains nil since config doesn't support multiple pickers |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// SchedulerConfig provides a configuration for the scheduler which influence routing decisions. |
| 39 | +type SchedulerConfig struct { |
| 40 | + preSchedulePlugins []plugins.PreSchedule |
| 41 | + filters []plugins.Filter |
| 42 | + scorers map[plugins.Scorer]int // map from scorer to weight |
| 43 | + picker plugins.Picker |
| 44 | + postSchedulePlugins []plugins.PostSchedule |
| 45 | + postResponsePlugins []plugins.PostResponse |
| 46 | +} |
| 47 | + |
| 48 | +// WithPreSchedulePlugins sets the given plugins as the PreSchedule plugins. |
| 49 | +// If the SchedulerConfig has PreSchedule plugins, this call replaces the existing plugins with the given ones. |
| 50 | +func (c *SchedulerConfig) WithPreSchedulePlugins(plugins ...plugins.PreSchedule) *SchedulerConfig { |
| 51 | + c.preSchedulePlugins = plugins |
| 52 | + return c |
| 53 | +} |
| 54 | + |
| 55 | +// WithFilters sets the given filter plugins as the Filter plugins. |
| 56 | +// if the SchedulerConfig has Filter plugins, this call replaces the existing plugins with the given ones. |
| 57 | +func (c *SchedulerConfig) WithFilters(filters ...plugins.Filter) *SchedulerConfig { |
| 58 | + c.filters = filters |
| 59 | + return c |
| 60 | +} |
| 61 | + |
| 62 | +// WithScorers sets the given scorer plugins as the Scorer plugins. |
| 63 | +// if the SchedulerConfig has Scorer plugins, this call replaces the existing plugins with the given ones. |
| 64 | +func (c *SchedulerConfig) WithScorers(scorers ...*scorer.WeightedScorer) *SchedulerConfig { |
| 65 | + scorersMap := make(map[plugins.Scorer]int, len(scorers)) |
| 66 | + for _, scorer := range scorers { |
| 67 | + scorersMap[scorer.Scorer] = scorer.Weight() |
| 68 | + } |
| 69 | + c.scorers = scorersMap |
| 70 | + return c |
| 71 | +} |
| 72 | + |
| 73 | +// WithPicker sets the given picker plugins as the Picker plugin. |
| 74 | +// if the SchedulerConfig has Picker plugin, this call replaces the existing plugin with the given one. |
| 75 | +func (c *SchedulerConfig) WithPicker(picker plugins.Picker) *SchedulerConfig { |
| 76 | + c.picker = picker |
| 77 | + return c |
| 78 | +} |
| 79 | + |
| 80 | +// WithPostSchedulePlugins sets the given plugins as the PostSchedule plugins. |
| 81 | +// If the SchedulerConfig has PostSchedule plugins, this call replaces the existing plugins with the given ones. |
| 82 | +func (c *SchedulerConfig) WithPostSchedulePlugins(plugins ...plugins.PostSchedule) *SchedulerConfig { |
| 83 | + c.postSchedulePlugins = plugins |
| 84 | + return c |
| 85 | +} |
| 86 | + |
| 87 | +// WithPostResponsePlugins sets the given plugins as the PostResponse plugins. |
| 88 | +// If the SchedulerConfig has PostResponse plugins, this call replaces the existing plugins with the given ones. |
| 89 | +func (c *SchedulerConfig) WithPostResponsePlugins(plugins ...plugins.PostResponse) *SchedulerConfig { |
| 90 | + c.postResponsePlugins = plugins |
| 91 | + return c |
| 92 | +} |
| 93 | + |
| 94 | +// AddPlugins adds the given plugins to all scheduler plugins according to the interfaces each plugin implements. |
| 95 | +// A plugin may implement more than one scheduler plugin interface. |
| 96 | +func (c *SchedulerConfig) AddPlugins(pluginObjects ...plugins.Plugin) error { |
| 97 | + for _, plugin := range pluginObjects { |
| 98 | + if weightedScorer, ok := plugin.(*scorer.WeightedScorer); ok { |
| 99 | + c.scorers[weightedScorer.Scorer] = weightedScorer.Weight() |
| 100 | + plugin = weightedScorer.Scorer // if we got WeightedScorer, unwrap the plugin |
| 101 | + } |
| 102 | + if preSchedulePlugin, ok := plugin.(plugins.PreSchedule); ok { |
| 103 | + c.preSchedulePlugins = append(c.preSchedulePlugins, preSchedulePlugin) |
| 104 | + } |
| 105 | + if filter, ok := plugin.(plugins.Filter); ok { |
| 106 | + c.filters = append(c.filters, filter) |
| 107 | + } |
| 108 | + if picker, ok := plugin.(plugins.Picker); ok { |
| 109 | + if c.picker != nil { |
| 110 | + return fmt.Errorf("failed to set '%s' as picker, already have a registered picker plugin '%s'", picker.Name(), c.picker.Name()) |
| 111 | + } |
| 112 | + c.picker = picker |
| 113 | + } |
| 114 | + if postSchedulePlugin, ok := plugin.(plugins.PostSchedule); ok { |
| 115 | + c.postSchedulePlugins = append(c.postSchedulePlugins, postSchedulePlugin) |
| 116 | + } |
| 117 | + if postResponsePlugin, ok := plugin.(plugins.PostResponse); ok { |
| 118 | + c.postResponsePlugins = append(c.postResponsePlugins, postResponsePlugin) |
| 119 | + } |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
0 commit comments