Skip to content

Commit 4485f63

Browse files
authored
Add APIs for the instantiated plugins to the EPP Handle (#1039)
* Added plugin instance APIs to plugins.Handle Signed-off-by: Shmuel Kallner <[email protected]> * An implementation of the new plugins.Handle APIs Signed-off-by: Shmuel Kallner <[email protected]> * Moved all configuration loading code to new package Signed-off-by: Shmuel Kallner <[email protected]> * Updates due to new and moved APIs Signed-off-by: Shmuel Kallner <[email protected]> * Cleanup of old configuration loading code Signed-off-by: Shmuel Kallner <[email protected]> --------- Signed-off-by: Shmuel Kallner <[email protected]>
1 parent 5afe58b commit 4485f63

File tree

8 files changed

+417
-330
lines changed

8 files changed

+417
-330
lines changed

cmd/epp/runner/register.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,49 @@ func RegisterAllPlugins() {
3939
plugins.Register(scorer.QueueScorerType, scorer.QueueScorerFactory)
4040
}
4141

42-
// eppHandle is a temporary implementation of the interface plugins.Handle
42+
// eppHandle is an implementation of the interface plugins.Handle
4343
type eppHandle struct {
44+
plugins plugins.HandlePlugins
45+
}
46+
47+
// Plugins returns the sub-handle for working with instantiated plugins
48+
func (h *eppHandle) Plugins() plugins.HandlePlugins {
49+
return h.plugins
50+
}
51+
52+
// eppHandlePlugins implements the set of APIs to work with instantiated plugins
53+
type eppHandlePlugins struct {
54+
thePlugins map[string]plugins.Plugin
55+
}
56+
57+
// Plugin returns the named plugin instance
58+
func (h *eppHandlePlugins) Plugin(name string) plugins.Plugin {
59+
return h.thePlugins[name]
60+
}
61+
62+
// AddPlugin adds a plugin to the set of known plugin instances
63+
func (h *eppHandlePlugins) AddPlugin(name string, plugin plugins.Plugin) {
64+
h.thePlugins[name] = plugin
65+
}
66+
67+
// GetAllPlugins returns all of the known plugins
68+
func (h *eppHandlePlugins) GetAllPlugins() []plugins.Plugin {
69+
result := make([]plugins.Plugin, 0)
70+
for _, plugin := range h.thePlugins {
71+
result = append(result, plugin)
72+
}
73+
return result
74+
}
75+
76+
// GetAllPluginsWithNames returns al of the known plugins with their names
77+
func (h *eppHandlePlugins) GetAllPluginsWithNames() map[string]plugins.Plugin {
78+
return h.thePlugins
79+
}
80+
81+
func newEppHandle() *eppHandle {
82+
return &eppHandle{
83+
plugins: &eppHandlePlugins{
84+
thePlugins: map[string]plugins.Plugin{},
85+
},
86+
}
4487
}

cmd/epp/runner/runner.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
conformance_epp "sigs.k8s.io/gateway-api-inference-extension/conformance/testing-epp"
3939
"sigs.k8s.io/gateway-api-inference-extension/internal/runnable"
4040
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
41-
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/common/config"
41+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/common/config/loader"
4242
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
4343
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics"
4444
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics/collectors"
@@ -216,29 +216,28 @@ func (r *Runner) Run(ctx context.Context) error {
216216
}
217217

218218
if len(*configText) != 0 || len(*configFile) != 0 {
219-
theConfig, err := config.LoadConfig([]byte(*configText), *configFile)
219+
theConfig, err := loader.LoadConfig([]byte(*configText), *configFile)
220220
if err != nil {
221221
setupLog.Error(err, "Failed to load the configuration")
222222
return err
223223
}
224224

225-
epp := eppHandle{}
226-
instantiatedPlugins, err := config.LoadPluginReferences(theConfig.Plugins, epp)
225+
epp := newEppHandle()
226+
227+
err = loader.LoadPluginReferences(theConfig.Plugins, epp)
227228
if err != nil {
228229
setupLog.Error(err, "Failed to instantiate the plugins")
229230
return err
230231
}
231232

232-
r.schedulerConfig, err = scheduling.LoadSchedulerConfig(theConfig.SchedulingProfiles, instantiatedPlugins)
233+
r.schedulerConfig, err = loader.LoadSchedulerConfig(theConfig.SchedulingProfiles, epp)
233234
if err != nil {
234235
setupLog.Error(err, "Failed to create Scheduler configuration")
235236
return err
236237
}
237238

238-
// Add requestcontrol plugins
239-
if instantiatedPlugins != nil {
240-
r.requestControlConfig = requestcontrol.LoadRequestControlConfig(instantiatedPlugins)
241-
}
239+
// Add requestControl plugins
240+
r.requestControlConfig.AddPlugins(epp.Plugins().GetAllPlugins()...)
242241
}
243242

244243
// --- Initialize Core EPP Components ---

pkg/epp/common/config/configloader.go renamed to pkg/epp/common/config/loader/configloader.go

Lines changed: 53 additions & 8 deletions
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 config
17+
package loader
1818

1919
import (
2020
"errors"
@@ -25,8 +25,11 @@ import (
2525
"k8s.io/apimachinery/pkg/runtime/serializer"
2626
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2727

28+
"sigs.k8s.io/gateway-api-inference-extension/api/config/v1alpha1"
2829
configapi "sigs.k8s.io/gateway-api-inference-extension/api/config/v1alpha1"
2930
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
31+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling"
32+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework"
3033
)
3134

3235
var scheme = runtime.NewScheme()
@@ -62,19 +65,61 @@ func LoadConfig(configText []byte, fileName string) (*configapi.EndpointPickerCo
6265
return theConfig, nil
6366
}
6467

65-
func LoadPluginReferences(thePlugins []configapi.PluginSpec, handle plugins.Handle) (map[string]plugins.Plugin, error) {
66-
references := map[string]plugins.Plugin{}
68+
func LoadPluginReferences(thePlugins []configapi.PluginSpec, handle plugins.Handle) error {
6769
for _, pluginConfig := range thePlugins {
68-
thePlugin, err := InstantiatePlugin(pluginConfig, handle)
70+
thePlugin, err := instantiatePlugin(pluginConfig, handle)
6971
if err != nil {
70-
return nil, err
72+
return err
7173
}
72-
references[pluginConfig.Name] = thePlugin
74+
handle.Plugins().AddPlugin(pluginConfig.Name, thePlugin)
7375
}
74-
return references, nil
76+
return nil
77+
}
78+
79+
func LoadSchedulerConfig(configProfiles []v1alpha1.SchedulingProfile, handle plugins.Handle) (*scheduling.SchedulerConfig, error) {
80+
81+
var profiles = map[string]*framework.SchedulerProfile{}
82+
83+
for _, configProfile := range configProfiles {
84+
profile := framework.SchedulerProfile{}
85+
86+
for _, plugin := range configProfile.Plugins {
87+
var err error
88+
thePlugin := handle.Plugins().Plugin(plugin.PluginRef)
89+
if theScorer, ok := thePlugin.(framework.Scorer); ok {
90+
if plugin.Weight == nil {
91+
return nil, fmt.Errorf("scorer '%s' is missing a weight", plugin.PluginRef)
92+
}
93+
thePlugin = framework.NewWeightedScorer(theScorer, *plugin.Weight)
94+
}
95+
err = profile.AddPlugins(thePlugin)
96+
if err != nil {
97+
return nil, err
98+
}
99+
}
100+
profiles[configProfile.Name] = &profile
101+
}
102+
103+
var profileHandler framework.ProfileHandler
104+
var profileHandlerName string
105+
106+
for pluginName, thePlugin := range handle.Plugins().GetAllPluginsWithNames() {
107+
if theProfileHandler, ok := thePlugin.(framework.ProfileHandler); ok {
108+
if profileHandler != nil {
109+
return nil, fmt.Errorf("only one profile handler is allowed. Both %s and %s are profile handlers", profileHandlerName, pluginName)
110+
}
111+
profileHandler = theProfileHandler
112+
profileHandlerName = pluginName
113+
}
114+
}
115+
if profileHandler == nil {
116+
return nil, errors.New("no profile handler was specified")
117+
}
118+
119+
return scheduling.NewSchedulerConfig(profileHandler, profiles), nil
75120
}
76121

77-
func InstantiatePlugin(pluginSpec configapi.PluginSpec, handle plugins.Handle) (plugins.Plugin, error) {
122+
func instantiatePlugin(pluginSpec configapi.PluginSpec, handle plugins.Handle) (plugins.Plugin, error) {
78123
factory, ok := plugins.Registry[pluginSpec.PluginName]
79124
if !ok {
80125
return nil, fmt.Errorf("failed to instantiate the plugin. plugin %s not found", pluginSpec.PluginName)

0 commit comments

Comments
 (0)