From 7f57c6e52dc862c22feca1c2d7fb4f8fc77d0765 Mon Sep 17 00:00:00 2001 From: Nick Parker Date: Thu, 27 Feb 2025 23:55:17 +1300 Subject: [PATCH] Update factory to use generics, keep single New function --- .../plugins/defaultpreemption/default_preemption.go | 9 ++------- .../defaultpreemption/default_preemption_test.go | 12 ++++++------ pkg/scheduler/framework/runtime/registry.go | 4 ++-- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption.go b/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption.go index fbe76bc89e2..0a145130416 100644 --- a/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption.go +++ b/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption.go @@ -82,13 +82,8 @@ func (pl *DefaultPreemption) Name() string { return Name } -// New initializes a new plugin and returns it. -func New(_ context.Context, dpArgs runtime.Object, fh framework.Handle, fts feature.Features) (framework.Plugin, error) { - return NewDefaultPreemption(dpArgs, fh, fts) -} - -// NewDefaultPreemption initializes a new plugin and returns it. The plugin type is retained to allow modification. -func NewDefaultPreemption(dpArgs runtime.Object, fh framework.Handle, fts feature.Features) (*DefaultPreemption, error) { +// New initializes a new plugin and returns it. The plugin type is retained to allow modification. +func New(_ context.Context, dpArgs runtime.Object, fh framework.Handle, fts feature.Features) (*DefaultPreemption, error) { args, ok := dpArgs.(*config.DefaultPreemptionArgs) if !ok { return nil, fmt.Errorf("got args of type %T, want *DefaultPreemptionArgs", dpArgs) diff --git a/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption_test.go b/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption_test.go index e8f3b903eb5..9be143b479c 100644 --- a/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption_test.go +++ b/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption_test.go @@ -436,7 +436,7 @@ func TestPostFilter(t *testing.T) { if err != nil { t.Fatal(err) } - p, err := NewDefaultPreemption(getDefaultDefaultPreemptionArgs(), f, feature.Features{}) + p, err := New(ctx, getDefaultDefaultPreemptionArgs(), f, feature.Features{}) if err != nil { t.Fatal(err) } @@ -1186,7 +1186,7 @@ func TestDryRunPreemption(t *testing.T) { if tt.args == nil { tt.args = getDefaultDefaultPreemptionArgs() } - pl, err := NewDefaultPreemption(tt.args, fwk, feature.Features{}) + pl, err := New(ctx, tt.args, fwk, feature.Features{}) if err != nil { t.Fatal(err) } @@ -1430,7 +1430,7 @@ func TestSelectBestCandidate(t *testing.T) { t.Errorf("Unexpected PreFilter Status: %v", status) } - pl, err := NewDefaultPreemption(getDefaultDefaultPreemptionArgs(), fwk, feature.Features{}) + pl, err := New(ctx, getDefaultDefaultPreemptionArgs(), fwk, feature.Features{}) if err != nil { t.Fatal(err) } @@ -1693,7 +1693,7 @@ func TestCustomSelection(t *testing.T) { t.Fatal(err) } - pl, err := NewDefaultPreemption(getDefaultDefaultPreemptionArgs(), fwk, feature.Features{}) + pl, err := New(ctx, getDefaultDefaultPreemptionArgs(), fwk, feature.Features{}) if err != nil { t.Fatal(err) } @@ -1809,7 +1809,7 @@ func TestPodEligibleToPreemptOthers(t *testing.T) { if err != nil { t.Fatal(err) } - pl, err := NewDefaultPreemption(getDefaultDefaultPreemptionArgs(), f, feature.Features{}) + pl, err := New(ctx, getDefaultDefaultPreemptionArgs(), f, feature.Features{}) if err != nil { t.Fatal(err) } @@ -2115,7 +2115,7 @@ func TestPreempt(t *testing.T) { features := feature.Features{ EnableAsyncPreemption: asyncPreemptionEnabled, } - pl, err := NewDefaultPreemption(getDefaultDefaultPreemptionArgs(), fwk, features) + pl, err := New(ctx, getDefaultDefaultPreemptionArgs(), fwk, features) if err != nil { t.Fatal(err) } diff --git a/pkg/scheduler/framework/runtime/registry.go b/pkg/scheduler/framework/runtime/registry.go index b6fca3c29cd..8c5901ae52a 100644 --- a/pkg/scheduler/framework/runtime/registry.go +++ b/pkg/scheduler/framework/runtime/registry.go @@ -31,11 +31,11 @@ import ( type PluginFactory = func(ctx context.Context, configuration runtime.Object, f framework.Handle) (framework.Plugin, error) // PluginFactoryWithFts is a function that builds a plugin with certain feature gates. -type PluginFactoryWithFts func(context.Context, runtime.Object, framework.Handle, plfeature.Features) (framework.Plugin, error) +type PluginFactoryWithFts[T framework.Plugin] func(context.Context, runtime.Object, framework.Handle, plfeature.Features) (T, error) // FactoryAdapter can be used to inject feature gates for a plugin that needs // them when the caller expects the older PluginFactory method. -func FactoryAdapter(fts plfeature.Features, withFts PluginFactoryWithFts) PluginFactory { +func FactoryAdapter[T framework.Plugin](fts plfeature.Features, withFts PluginFactoryWithFts[T]) PluginFactory { return func(ctx context.Context, plArgs runtime.Object, fh framework.Handle) (framework.Plugin, error) { return withFts(ctx, plArgs, fh, fts) }