diff --git a/cmd/epp/main.go b/cmd/epp/main.go index b5e06177b..bd15e87e7 100644 --- a/cmd/epp/main.go +++ b/cmd/epp/main.go @@ -25,8 +25,6 @@ import ( ) func main() { - // Register all known plugin factories - runner.RegisterAllPlugins() // For adding out-of-tree plugins to the plugins registry, use the following: // plugins.Register(my-out-of-tree-plugin-name, my-out-of-tree-plugin-factory-function) diff --git a/cmd/epp/runner/register.go b/cmd/epp/runner/register.go deleted file mode 100644 index 161a224f2..000000000 --- a/cmd/epp/runner/register.go +++ /dev/null @@ -1,95 +0,0 @@ -/* -Copyright 2025 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package runner - -import ( - "context" - - "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins" - "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/filter" - "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/multi/prefix" - "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/picker" - "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/profile" - "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/scorer" - testfilter "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/test/filter" -) - -// RegisterAllPlugins registers the factory functions of all known plugins -func RegisterAllPlugins() { - plugins.Register(filter.DecisionTreeFilterType, filter.DecisionTreeFilterFactory) - plugins.Register(filter.LeastKVCacheFilterType, filter.LeastKVCacheFilterFactory) - plugins.Register(filter.LeastQueueFilterType, filter.LeastQueueFilterFactory) - plugins.Register(filter.LoraAffinityFilterType, filter.LoraAffinityFilterFactory) - plugins.Register(filter.LowQueueFilterType, filter.LowQueueFilterFactory) - plugins.Register(prefix.PrefixCachePluginType, prefix.PrefixCachePluginFactory) - plugins.Register(picker.MaxScorePickerType, picker.MaxScorePickerFactory) - plugins.Register(picker.RandomPickerType, picker.RandomPickerFactory) - plugins.Register(profile.SingleProfileHandlerType, profile.SingleProfileHandlerFactory) - plugins.Register(scorer.KvCacheScorerType, scorer.KvCacheScorerFactory) - plugins.Register(scorer.QueueScorerType, scorer.QueueScorerFactory) - // register filter for test purpose only (used in conformance tests) - plugins.Register(testfilter.HeaderBasedTestingFilterType, testfilter.HeaderBasedTestingFilterFactory) -} - -// eppHandle is an implementation of the interface plugins.Handle -type eppHandle struct { - ctx context.Context - plugins.HandlePlugins -} - -// Context returns a context the plugins can use, if they need one -func (h *eppHandle) Context() context.Context { - return h.ctx -} - -// eppHandlePlugins implements the set of APIs to work with instantiated plugins -type eppHandlePlugins struct { - plugins map[string]plugins.Plugin -} - -// Plugin returns the named plugin instance -func (h *eppHandlePlugins) Plugin(name string) plugins.Plugin { - return h.plugins[name] -} - -// AddPlugin adds a plugin to the set of known plugin instances -func (h *eppHandlePlugins) AddPlugin(name string, plugin plugins.Plugin) { - h.plugins[name] = plugin -} - -// GetAllPlugins returns all of the known plugins -func (h *eppHandlePlugins) GetAllPlugins() []plugins.Plugin { - result := make([]plugins.Plugin, 0) - for _, plugin := range h.plugins { - result = append(result, plugin) - } - return result -} - -// GetAllPluginsWithNames returns al of the known plugins with their names -func (h *eppHandlePlugins) GetAllPluginsWithNames() map[string]plugins.Plugin { - return h.plugins -} - -func newEppHandle(ctx context.Context) *eppHandle { - return &eppHandle{ - ctx: ctx, - HandlePlugins: &eppHandlePlugins{ - plugins: map[string]plugins.Plugin{}, - }, - } -} diff --git a/cmd/epp/runner/runner.go b/cmd/epp/runner/runner.go index 1c45ce64d..494f2c381 100644 --- a/cmd/epp/runner/runner.go +++ b/cmd/epp/runner/runner.go @@ -44,9 +44,16 @@ import ( "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore" "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics" "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics/collectors" + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins" "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/requestcontrol" "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/saturationdetector" "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling" + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/filter" + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/multi/prefix" + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/picker" + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/profile" + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/scorer" + testfilter "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/test/filter" runserver "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/server" "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging" "sigs.k8s.io/gateway-api-inference-extension/version" @@ -335,6 +342,23 @@ func (r *Runner) Run(ctx context.Context) error { return nil } +// registerInTreePlugins registers the factory functions of all known plugins +func (r *Runner) registerInTreePlugins() { + plugins.Register(filter.DecisionTreeFilterType, filter.DecisionTreeFilterFactory) + plugins.Register(filter.LeastKVCacheFilterType, filter.LeastKVCacheFilterFactory) + plugins.Register(filter.LeastQueueFilterType, filter.LeastQueueFilterFactory) + plugins.Register(filter.LoraAffinityFilterType, filter.LoraAffinityFilterFactory) + plugins.Register(filter.LowQueueFilterType, filter.LowQueueFilterFactory) + plugins.Register(prefix.PrefixCachePluginType, prefix.PrefixCachePluginFactory) + plugins.Register(picker.MaxScorePickerType, picker.MaxScorePickerFactory) + plugins.Register(picker.RandomPickerType, picker.RandomPickerFactory) + plugins.Register(profile.SingleProfileHandlerType, profile.SingleProfileHandlerFactory) + plugins.Register(scorer.KvCacheScorerType, scorer.KvCacheScorerFactory) + plugins.Register(scorer.QueueScorerType, scorer.QueueScorerFactory) + // register filter for test purpose only (used in conformance tests) + plugins.Register(testfilter.HeaderBasedTestingFilterType, testfilter.HeaderBasedTestingFilterFactory) +} + func (r *Runner) parsePluginsConfiguration(ctx context.Context) error { if *configText == "" && *configFile == "" { return nil // configuring through code, not through file @@ -351,7 +375,8 @@ func (r *Runner) parsePluginsConfiguration(ctx context.Context) error { } } - handle := newEppHandle(ctx) + r.registerInTreePlugins() + handle := plugins.NewEppHandle(ctx) config, err := loader.LoadConfig(configBytes, handle) if err != nil { return fmt.Errorf("failed to load the configuration - %w", err) diff --git a/pkg/epp/plugins/handle.go b/pkg/epp/plugins/handle.go index f00aa5089..8c9153cf1 100644 --- a/pkg/epp/plugins/handle.go +++ b/pkg/epp/plugins/handle.go @@ -44,6 +44,55 @@ type HandlePlugins interface { GetAllPluginsWithNames() map[string]Plugin } +// eppHandle is an implementation of the interface plugins.Handle +type eppHandle struct { + ctx context.Context + HandlePlugins +} + +// Context returns a context the plugins can use, if they need one +func (h *eppHandle) Context() context.Context { + return h.ctx +} + +// eppHandlePlugins implements the set of APIs to work with instantiated plugins +type eppHandlePlugins struct { + plugins map[string]Plugin +} + +// Plugin returns the named plugin instance +func (h *eppHandlePlugins) Plugin(name string) Plugin { + return h.plugins[name] +} + +// AddPlugin adds a plugin to the set of known plugin instances +func (h *eppHandlePlugins) AddPlugin(name string, plugin Plugin) { + h.plugins[name] = plugin +} + +// GetAllPlugins returns all of the known plugins +func (h *eppHandlePlugins) GetAllPlugins() []Plugin { + result := make([]Plugin, 0) + for _, plugin := range h.plugins { + result = append(result, plugin) + } + return result +} + +// GetAllPluginsWithNames returns al of the known plugins with their names +func (h *eppHandlePlugins) GetAllPluginsWithNames() map[string]Plugin { + return h.plugins +} + +func NewEppHandle(ctx context.Context) Handle { + return &eppHandle{ + ctx: ctx, + HandlePlugins: &eppHandlePlugins{ + plugins: map[string]Plugin{}, + }, + } +} + // PluginByType retrieves the specified plugin by name and verifies its type func PluginByType[P Plugin](handlePlugins HandlePlugins, name string) (P, error) { var zero P