Skip to content

Commit 371e0a6

Browse files
authored
embed handlePlugins inside the general plugins handle (#1128)
Signed-off-by: Nir Rozenbaum <[email protected]>
1 parent a4a6ab4 commit 371e0a6

File tree

9 files changed

+90
-83
lines changed

9 files changed

+90
-83
lines changed

cmd/epp/runner/register.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,54 +44,49 @@ func RegisterAllPlugins() {
4444

4545
// eppHandle is an implementation of the interface plugins.Handle
4646
type eppHandle struct {
47-
ctx context.Context
48-
plugins plugins.HandlePlugins
47+
ctx context.Context
48+
plugins.HandlePlugins
4949
}
5050

5151
// Context returns a context the plugins can use, if they need one
5252
func (h *eppHandle) Context() context.Context {
5353
return h.ctx
5454
}
5555

56-
// Plugins returns the sub-handle for working with instantiated plugins
57-
func (h *eppHandle) Plugins() plugins.HandlePlugins {
58-
return h.plugins
59-
}
60-
6156
// eppHandlePlugins implements the set of APIs to work with instantiated plugins
6257
type eppHandlePlugins struct {
63-
thePlugins map[string]plugins.Plugin
58+
plugins map[string]plugins.Plugin
6459
}
6560

6661
// Plugin returns the named plugin instance
6762
func (h *eppHandlePlugins) Plugin(name string) plugins.Plugin {
68-
return h.thePlugins[name]
63+
return h.plugins[name]
6964
}
7065

7166
// AddPlugin adds a plugin to the set of known plugin instances
7267
func (h *eppHandlePlugins) AddPlugin(name string, plugin plugins.Plugin) {
73-
h.thePlugins[name] = plugin
68+
h.plugins[name] = plugin
7469
}
7570

7671
// GetAllPlugins returns all of the known plugins
7772
func (h *eppHandlePlugins) GetAllPlugins() []plugins.Plugin {
7873
result := make([]plugins.Plugin, 0)
79-
for _, plugin := range h.thePlugins {
74+
for _, plugin := range h.plugins {
8075
result = append(result, plugin)
8176
}
8277
return result
8378
}
8479

8580
// GetAllPluginsWithNames returns al of the known plugins with their names
8681
func (h *eppHandlePlugins) GetAllPluginsWithNames() map[string]plugins.Plugin {
87-
return h.thePlugins
82+
return h.plugins
8883
}
8984

9085
func newEppHandle(ctx context.Context) *eppHandle {
9186
return &eppHandle{
9287
ctx: ctx,
93-
plugins: &eppHandlePlugins{
94-
thePlugins: map[string]plugins.Plugin{},
88+
HandlePlugins: &eppHandlePlugins{
89+
plugins: map[string]plugins.Plugin{},
9590
},
9691
}
9792
}

cmd/epp/runner/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ func (r *Runner) parseConfiguration(ctx context.Context) error {
363363
}
364364

365365
// Add requestControl plugins
366-
r.requestControlConfig.AddPlugins(handle.Plugins().GetAllPlugins()...)
366+
r.requestControlConfig.AddPlugins(handle.GetAllPlugins()...)
367367

368368
return nil
369369
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func LoadSchedulerConfig(configProfiles []v1alpha1.SchedulingProfile, handle plu
6666
for _, namedProfile := range configProfiles {
6767
profile := framework.NewSchedulerProfile()
6868
for _, plugin := range namedProfile.Plugins {
69-
referencedPlugin := handle.Plugins().Plugin(plugin.PluginRef)
69+
referencedPlugin := handle.Plugin(plugin.PluginRef)
7070
if scorer, ok := referencedPlugin.(framework.Scorer); ok {
7171
if plugin.Weight == nil {
7272
return nil, fmt.Errorf("scorer '%s' is missing a weight", plugin.PluginRef)
@@ -81,7 +81,7 @@ func LoadSchedulerConfig(configProfiles []v1alpha1.SchedulingProfile, handle plu
8181
}
8282

8383
var profileHandler framework.ProfileHandler
84-
for pluginName, plugin := range handle.Plugins().GetAllPluginsWithNames() {
84+
for pluginName, plugin := range handle.GetAllPluginsWithNames() {
8585
if theProfileHandler, ok := plugin.(framework.ProfileHandler); ok {
8686
if profileHandler != nil {
8787
return nil, fmt.Errorf("only one profile handler is allowed. Both %s and %s are profile handlers", profileHandler.TypedName().Name, pluginName)
@@ -119,7 +119,7 @@ func instantiatePlugins(configuredPlugins []configapi.PluginSpec, handle plugins
119119
return fmt.Errorf("failed to instantiate the plugin type '%s' - %w", pluginConfig.Type, err)
120120
}
121121

122-
handle.Plugins().AddPlugin(pluginConfig.Name, plugin)
122+
handle.AddPlugin(pluginConfig.Name, plugin)
123123
}
124124

125125
return nil

pkg/epp/common/config/loader/configloader_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ func TestInstantiatePlugins(t *testing.T) {
209209
if err != nil {
210210
t.Fatalf("LoadConfig returned unexpected error - %v", err)
211211
}
212-
if len(handle.Plugins().GetAllPlugins()) == 0 {
212+
if len(handle.GetAllPlugins()) == 0 {
213213
t.Fatalf("unexpected empty set of loaded plugins")
214214
}
215-
if t1 := handle.Plugins().Plugin("test1"); t1 == nil {
215+
if t1 := handle.Plugin("test1"); t1 == nil {
216216
t.Fatalf("loaded plugins did not contain test1")
217217
} else if _, ok := t1.(*test1); !ok {
218218
t.Fatalf("loaded plugins returned test1 has the wrong type %#v", t1)

pkg/epp/plugins/handle.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 plugins
18+
19+
import (
20+
"context"
21+
"fmt"
22+
)
23+
24+
// Handle provides plugins a set of standard data and tools to work with
25+
type Handle interface {
26+
// Context returns a context the plugins can use, if they need one
27+
Context() context.Context
28+
29+
HandlePlugins
30+
}
31+
32+
// HandlePlugins defines a set of APIs to work with instantiated plugins
33+
type HandlePlugins interface {
34+
// Plugin returns the named plugin instance
35+
Plugin(name string) Plugin
36+
37+
// AddPlugin adds a plugin to the set of known plugin instances
38+
AddPlugin(name string, plugin Plugin)
39+
40+
// GetAllPlugins returns all of the known plugins
41+
GetAllPlugins() []Plugin
42+
43+
// GetAllPluginsWithNames returns all of the known plugins with their names
44+
GetAllPluginsWithNames() map[string]Plugin
45+
}
46+
47+
// PluginByType retrieves the specified plugin by name and verifies its type
48+
func PluginByType[P Plugin](handlePlugins HandlePlugins, name string) (P, error) {
49+
var zero P
50+
51+
rawPlugin := handlePlugins.Plugin(name)
52+
if rawPlugin == nil {
53+
return zero, fmt.Errorf("there is no plugin with the name '%s' defined", name)
54+
}
55+
plugin, ok := rawPlugin.(P)
56+
if !ok {
57+
return zero, fmt.Errorf("the plugin with the name '%s' is not an instance of %T", name, zero)
58+
}
59+
return plugin, nil
60+
}

pkg/epp/plugins/plugins.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,9 @@ limitations under the License.
1616

1717
package plugins
1818

19-
import (
20-
"context"
21-
"fmt"
22-
)
23-
2419
// Plugin defines the interface for a plugin.
2520
// This interface should be embedded in all plugins across the code.
2621
type Plugin interface {
2722
// TypedName returns the type and name tuple of this plugin instance.
2823
TypedName() TypedName
2924
}
30-
31-
// Handle provides plugins a set of standard data and tools to work with
32-
type Handle interface {
33-
// Context returns a context the plugins can use, if they need one
34-
Context() context.Context
35-
36-
// Plugins returns the sub-handle for working with instantiated plugins
37-
Plugins() HandlePlugins
38-
}
39-
40-
// HandlePlugins defines a set of APIs to work with instantiated plugins
41-
type HandlePlugins interface {
42-
// Plugin returns the named plugin instance
43-
Plugin(name string) Plugin
44-
45-
// AddPlugin adds a plugin to the set of known plugin instances
46-
AddPlugin(name string, plugin Plugin)
47-
48-
// GetAllPlugins returns all of the known plugins
49-
GetAllPlugins() []Plugin
50-
51-
// GetAllPluginsWithNames returns all of the known plugins with their names
52-
GetAllPluginsWithNames() map[string]Plugin
53-
}
54-
55-
// PluginByType retrieves the specified plugin by name and verifies its type
56-
func PluginByType[P Plugin](handlePlugins HandlePlugins, name string) (P, error) {
57-
var zero P
58-
59-
rawPlugin := handlePlugins.Plugin(name)
60-
if rawPlugin == nil {
61-
return zero, fmt.Errorf("there is no plugin with the name '%s' defined", name)
62-
}
63-
thePlugin, ok := rawPlugin.(P)
64-
if !ok {
65-
return zero, fmt.Errorf("the plugin with the name '%s' is not an instance of %T", name, zero)
66-
}
67-
return thePlugin, nil
68-
}

pkg/epp/scheduling/framework/plugins/filter/decision_tree_filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func loadDecisionTreeEntry(entry *decisionTreeFilterEntry, handle plugins.Handle
119119
}
120120

121121
if entry.PluginRef != nil {
122-
instance := handle.Plugins().Plugin(*entry.PluginRef)
122+
instance := handle.Plugin(*entry.PluginRef)
123123
if instance == nil {
124124
return nil, errors.New(*entry.PluginRef + " is a reference to an undefined Plugin")
125125
}

pkg/epp/scheduling/framework/plugins/filter/filter_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,12 @@ func TestDecisionTreeFilterFactory(t *testing.T) {
275275

276276
testHandle := utils.NewTestHandle(context.Background())
277277

278-
testHandle.Plugins().AddPlugin("leastKvCache", leastKvCacheFilter)
279-
testHandle.Plugins().AddPlugin("leastQueue", leastQueueFilter)
280-
testHandle.Plugins().AddPlugin("loraAffinity", loraAffinityFilter)
281-
testHandle.Plugins().AddPlugin("lowQueue", lowQueueFilter)
278+
testHandle.AddPlugin("leastKvCache", leastKvCacheFilter)
279+
testHandle.AddPlugin("leastQueue", leastQueueFilter)
280+
testHandle.AddPlugin("loraAffinity", loraAffinityFilter)
281+
testHandle.AddPlugin("lowQueue", lowQueueFilter)
282282

283-
testHandle.Plugins().AddPlugin("kvCacheScorer", kvCacheScorer)
283+
testHandle.AddPlugin("kvCacheScorer", kvCacheScorer)
284284

285285
tests := []struct {
286286
name string

test/utils/handle.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,48 +24,44 @@ import (
2424

2525
// testHandle is an implmentation of plugins.Handle for test purposes
2626
type testHandle struct {
27-
ctx context.Context
28-
plugins plugins.HandlePlugins
27+
ctx context.Context
28+
plugins.HandlePlugins
2929
}
3030

3131
// Context returns a context the plugins can use, if they need one
3232
func (h *testHandle) Context() context.Context {
3333
return h.ctx
3434
}
3535

36-
func (h *testHandle) Plugins() plugins.HandlePlugins {
37-
return h.plugins
38-
}
39-
4036
type testHandlePlugins struct {
41-
thePlugins map[string]plugins.Plugin
37+
plugins map[string]plugins.Plugin
4238
}
4339

4440
func (h *testHandlePlugins) Plugin(name string) plugins.Plugin {
45-
return h.thePlugins[name]
41+
return h.plugins[name]
4642
}
4743

4844
func (h *testHandlePlugins) AddPlugin(name string, plugin plugins.Plugin) {
49-
h.thePlugins[name] = plugin
45+
h.plugins[name] = plugin
5046
}
5147

5248
func (h *testHandlePlugins) GetAllPlugins() []plugins.Plugin {
5349
result := make([]plugins.Plugin, 0)
54-
for _, plugin := range h.thePlugins {
50+
for _, plugin := range h.plugins {
5551
result = append(result, plugin)
5652
}
5753
return result
5854
}
5955

6056
func (h *testHandlePlugins) GetAllPluginsWithNames() map[string]plugins.Plugin {
61-
return h.thePlugins
57+
return h.plugins
6258
}
6359

6460
func NewTestHandle(ctx context.Context) plugins.Handle {
6561
return &testHandle{
6662
ctx: ctx,
67-
plugins: &testHandlePlugins{
68-
thePlugins: map[string]plugins.Plugin{},
63+
HandlePlugins: &testHandlePlugins{
64+
plugins: map[string]plugins.Plugin{},
6965
},
7066
}
7167
}

0 commit comments

Comments
 (0)