Skip to content

Commit 33eb946

Browse files
authored
feat: Add a context.Context to the plugins.HAndle interface (#1076)
* Added a context.Context to the plugins.Handle interface Signed-off-by: Shmuel Kallner <[email protected]> * Changes due to changes in internal APIs Signed-off-by: Shmuel Kallner <[email protected]> * Changes to tests due to changes in internal APIs Signed-off-by: Shmuel Kallner <[email protected]> --------- Signed-off-by: Shmuel Kallner <[email protected]>
1 parent 54822dd commit 33eb946

File tree

6 files changed

+39
-11
lines changed

6 files changed

+39
-11
lines changed

cmd/epp/runner/register.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package runner
1818

1919
import (
20+
"context"
21+
2022
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
2123
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/filter"
2224
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/multi/prefix"
@@ -42,9 +44,15 @@ func RegisterAllPlugins() {
4244

4345
// eppHandle is an implementation of the interface plugins.Handle
4446
type eppHandle struct {
47+
ctx context.Context
4548
plugins plugins.HandlePlugins
4649
}
4750

51+
// Context returns a context the plugins can use, if they need one
52+
func (h *eppHandle) Context() context.Context {
53+
return h.ctx
54+
}
55+
4856
// Plugins returns the sub-handle for working with instantiated plugins
4957
func (h *eppHandle) Plugins() plugins.HandlePlugins {
5058
return h.plugins
@@ -79,8 +87,9 @@ func (h *eppHandlePlugins) GetAllPluginsWithNames() map[string]plugins.Plugin {
7987
return h.thePlugins
8088
}
8189

82-
func newEppHandle() *eppHandle {
90+
func newEppHandle(ctx context.Context) *eppHandle {
8391
return &eppHandle{
92+
ctx: ctx,
8493
plugins: &eppHandlePlugins{
8594
thePlugins: map[string]plugins.Plugin{},
8695
},

cmd/epp/runner/runner.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (r *Runner) Run(ctx context.Context) error {
222222
return err
223223
}
224224

225-
err = r.parseConfiguration()
225+
err = r.parseConfiguration(ctx)
226226
if err != nil {
227227
setupLog.Error(err, "Failed to parse the configuration")
228228
return err
@@ -315,14 +315,14 @@ func (r *Runner) initializeScheduler() (*scheduling.Scheduler, error) {
315315
return scheduler, nil
316316
}
317317

318-
func (r *Runner) parseConfiguration() error {
318+
func (r *Runner) parseConfiguration(ctx context.Context) error {
319319
if len(*configText) != 0 || len(*configFile) != 0 {
320320
theConfig, err := loader.LoadConfig([]byte(*configText), *configFile)
321321
if err != nil {
322322
return fmt.Errorf("failed to load the configuration - %w", err)
323323
}
324324

325-
epp := newEppHandle()
325+
epp := newEppHandle(ctx)
326326

327327
err = loader.LoadPluginReferences(theConfig.Plugins, epp)
328328
if err != nil {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,12 @@ func TestLoadConfiguration(t *testing.T) {
205205
}
206206

207207
func TestLoadPluginReferences(t *testing.T) {
208+
ctx := context.Background()
208209
theConfig, err := LoadConfig([]byte(successConfigText), "")
209210
if err != nil {
210211
t.Fatalf("LoadConfig returned unexpected error: %v", err)
211212
}
212-
handle := utils.NewTestHandle()
213+
handle := utils.NewTestHandle(ctx)
213214
err = LoadPluginReferences(theConfig.Plugins, handle)
214215
if err != nil {
215216
t.Fatalf("LoadPluginReferences returned unexpected error: %v", err)
@@ -227,15 +228,15 @@ func TestLoadPluginReferences(t *testing.T) {
227228
if err != nil {
228229
t.Fatalf("LoadConfig returned unexpected error: %v", err)
229230
}
230-
err = LoadPluginReferences(theConfig.Plugins, utils.NewTestHandle())
231+
err = LoadPluginReferences(theConfig.Plugins, utils.NewTestHandle(ctx))
231232
if err == nil {
232233
t.Fatalf("LoadPluginReferences did not return the expected error")
233234
}
234235
}
235236

236237
func TestInstantiatePlugin(t *testing.T) {
237238
plugSpec := configapi.PluginSpec{Type: "plover"}
238-
_, err := instantiatePlugin(plugSpec, utils.NewTestHandle())
239+
_, err := instantiatePlugin(plugSpec, utils.NewTestHandle(context.Background()))
239240
if err == nil {
240241
t.Fatalf("InstantiatePlugin did not return the expected error")
241242
}
@@ -286,6 +287,8 @@ func TestLoadSchedulerConfig(t *testing.T) {
286287

287288
registerNeededPlgugins()
288289

290+
ctx := context.Background()
291+
289292
for _, test := range tests {
290293
theConfig, err := LoadConfig([]byte(test.configText), "")
291294
if err != nil {
@@ -294,7 +297,7 @@ func TestLoadSchedulerConfig(t *testing.T) {
294297
}
295298
t.Fatalf("LoadConfig returned unexpected error: %v", err)
296299
}
297-
handle := utils.NewTestHandle()
300+
handle := utils.NewTestHandle(ctx)
298301
err = LoadPluginReferences(theConfig.Plugins, handle)
299302
if err != nil {
300303
if test.wantErr {

pkg/epp/plugins/plugins.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
package plugins
1818

19+
import "context"
20+
1921
// Plugin defines the interface for a plugin.
2022
// This interface should be embedded in all plugins across the code.
2123
type Plugin interface {
@@ -27,6 +29,9 @@ type Plugin interface {
2729

2830
// Handle provides plugins a set of standard data and tools to work with
2931
type Handle interface {
32+
// Context returns a context the plugins can use, if they need one
33+
Context() context.Context
34+
3035
// Plugins returns the sub-handle for working with instantiated plugins
3136
Plugins() HandlePlugins
3237
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func TestDecisionTreeFilterFactory(t *testing.T) {
406406

407407
kvCacheScorer := scorer.NewKVCacheScorer()
408408

409-
testHandle := utils.NewTestHandle()
409+
testHandle := utils.NewTestHandle(context.Background())
410410

411411
testHandle.Plugins().AddPlugin("leastKvCache", leastKvCacheFilter)
412412
testHandle.Plugins().AddPlugin("leastQueue", leastQueueFilter)

test/utils/handle.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ limitations under the License.
1616

1717
package utils
1818

19-
import "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
19+
import (
20+
"context"
21+
22+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
23+
)
2024

2125
// testHandle is an implmentation of plugins.Handle for test purposes
2226
type testHandle struct {
27+
ctx context.Context
2328
plugins plugins.HandlePlugins
2429
}
2530

31+
// Context returns a context the plugins can use, if they need one
32+
func (h *testHandle) Context() context.Context {
33+
return h.ctx
34+
}
35+
2636
func (h *testHandle) Plugins() plugins.HandlePlugins {
2737
return h.plugins
2838
}
@@ -51,8 +61,9 @@ func (h *testHandlePlugins) GetAllPluginsWithNames() map[string]plugins.Plugin {
5161
return h.thePlugins
5262
}
5363

54-
func NewTestHandle() plugins.Handle {
64+
func NewTestHandle(ctx context.Context) plugins.Handle {
5565
return &testHandle{
66+
ctx: ctx,
5667
plugins: &testHandlePlugins{
5768
thePlugins: map[string]plugins.Plugin{},
5869
},

0 commit comments

Comments
 (0)