Skip to content

Commit 3aff5f8

Browse files
committed
adjust plugins to use TypedName
Signed-off-by: Etai Lev Ran <[email protected]>
1 parent 612ec67 commit 3aff5f8

File tree

17 files changed

+137
-235
lines changed

17 files changed

+137
-235
lines changed

conformance/testing-epp/plugins/filter/request_header_based_filter.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"strings"
2222

23+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
2324
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework"
2425
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
2526
)
@@ -38,20 +39,14 @@ var _ framework.Filter = &HeaderBasedTestingFilter{}
3839
// NewHeaderBasedTestingFilter initializes a new HeaderBasedTestingFilter.
3940
// This should only be used for testing purposes.
4041
func NewHeaderBasedTestingFilter() *HeaderBasedTestingFilter {
41-
return &HeaderBasedTestingFilter{}
42+
return &HeaderBasedTestingFilter{
43+
TypedName: plugins.NewTypedName("header-based-testing", "header-based-testing-filter"),
44+
}
4245
}
4346

4447
// HeaderBasedTestingFilter filters Pods based on an address specified in the "test-epp-endpoint-selection" request header.
45-
type HeaderBasedTestingFilter struct{}
46-
47-
// Type returns the type of the filter.
48-
func (f *HeaderBasedTestingFilter) Type() string {
49-
return "header-based-testing"
50-
}
51-
52-
// Name returns the type of the filter.
53-
func (f *HeaderBasedTestingFilter) Name() string {
54-
return "header-based-testing-filter"
48+
type HeaderBasedTestingFilter struct {
49+
plugins.TypedName
5550
}
5651

5752
// Filter selects pods that match the IP addresses specified in the request header.

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

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -555,15 +555,14 @@ schedulingProfiles:
555555
var _ framework.Filter = &test1{}
556556

557557
type test1 struct {
558+
plugins.TypedName
558559
Threshold int `json:"threshold"`
559560
}
560561

561-
func (f *test1) Type() string {
562-
return test1Type
563-
}
564-
565-
func (f *test1) Name() string {
566-
return "test-1"
562+
func newTest1() *test1 {
563+
return &test1{
564+
TypedName: plugins.NewTypedName(test1Type, "test-1"),
565+
}
567566
}
568567

569568
// Filter filters out pods that doesn't meet the filter criteria.
@@ -575,14 +574,14 @@ func (f *test1) Filter(_ context.Context, _ *types.CycleState, _ *types.LLMReque
575574
var _ framework.Scorer = &test2{}
576575
var _ framework.PostCycle = &test2{}
577576

578-
type test2 struct{}
579-
580-
func (f *test2) Type() string {
581-
return test2Type
577+
type test2 struct {
578+
plugins.TypedName
582579
}
583580

584-
func (f *test2) Name() string {
585-
return "test-2"
581+
func newTest2() *test2 {
582+
return &test2{
583+
TypedName: plugins.NewTypedName(test2Type, "test-2"),
584+
}
586585
}
587586

588587
func (m *test2) Score(_ context.Context, _ *types.CycleState, _ *types.LLMRequest, _ []types.Pod) map[types.Pod]float64 {
@@ -594,14 +593,14 @@ func (m *test2) PostCycle(_ context.Context, _ *types.CycleState, _ *types.Profi
594593
// compile-time type validation
595594
var _ framework.Picker = &testPicker{}
596595

597-
type testPicker struct{}
598-
599-
func (p *testPicker) Type() string {
600-
return testPickerType
596+
type testPicker struct {
597+
plugins.TypedName
601598
}
602599

603-
func (p *testPicker) Name() string {
604-
return "test-picker"
600+
func newTestPicker() *testPicker {
601+
return &testPicker{
602+
TypedName: plugins.NewTypedName(testPickerType, "test-picker"),
603+
}
605604
}
606605

607606
func (p *testPicker) Pick(_ context.Context, _ *types.CycleState, _ []*types.ScoredPod) *types.ProfileRunResult {
@@ -611,14 +610,14 @@ func (p *testPicker) Pick(_ context.Context, _ *types.CycleState, _ []*types.Sco
611610
// compile-time type validation
612611
var _ framework.ProfileHandler = &testProfileHandler{}
613612

614-
type testProfileHandler struct{}
615-
616-
func (p *testProfileHandler) Type() string {
617-
return testProfileHandlerType
613+
type testProfileHandler struct {
614+
plugins.TypedName
618615
}
619616

620-
func (p *testProfileHandler) Name() string {
621-
return "test-profile-handler"
617+
func newTestProfileHandler() *testProfileHandler {
618+
return &testProfileHandler{
619+
TypedName: plugins.NewTypedName(testProfileHandlerType, "test-profile-handler"),
620+
}
622621
}
623622

624623
func (p *testProfileHandler) Pick(_ context.Context, _ *types.CycleState, _ *types.LLMRequest, _ map[string]*framework.SchedulerProfile, _ map[string]*types.ProfileRunResult) map[string]*framework.SchedulerProfile {
@@ -631,28 +630,28 @@ func (p *testProfileHandler) ProcessResults(_ context.Context, _ *types.CycleSta
631630

632631
func registerTestPlugins() {
633632
plugins.Register(test1Type,
634-
func(name string, parameters json.RawMessage, handle plugins.Handle) (plugins.Plugin, error) {
635-
result := test1{}
636-
err := json.Unmarshal(parameters, &result)
637-
return &result, err
633+
func(_ string, parameters json.RawMessage, _ plugins.Handle) (plugins.Plugin, error) {
634+
result := newTest1()
635+
err := json.Unmarshal(parameters, result)
636+
return result, err
638637
},
639638
)
640639

641640
plugins.Register(test2Type,
642-
func(name string, parameters json.RawMessage, handle plugins.Handle) (plugins.Plugin, error) {
643-
return &test2{}, nil
641+
func(_ string, _ json.RawMessage, _ plugins.Handle) (plugins.Plugin, error) {
642+
return newTest2(), nil
644643
},
645644
)
646645

647646
plugins.Register(testPickerType,
648-
func(name string, parameters json.RawMessage, handle plugins.Handle) (plugins.Plugin, error) {
649-
return &testPicker{}, nil
647+
func(_ string, _ json.RawMessage, _ plugins.Handle) (plugins.Plugin, error) {
648+
return newTestPicker(), nil
650649
},
651650
)
652651

653652
plugins.Register(testProfileHandlerType,
654-
func(name string, parameters json.RawMessage, handle plugins.Handle) (plugins.Plugin, error) {
655-
return &testProfileHandler{}, nil
653+
func(_ string, _ json.RawMessage, _ plugins.Handle) (plugins.Plugin, error) {
654+
return newTestProfileHandler(), nil
656655
},
657656
)
658657
}

pkg/epp/requestcontrol/director_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
3737
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
3838
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/handlers"
39+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
3940
schedulingtypes "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
4041
errutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/error"
4142
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
@@ -520,9 +521,7 @@ func pointer(v int32) *int32 {
520521
}
521522

522523
func TestDirector_HandleResponse(t *testing.T) {
523-
pr1 := &testPostResponse{
524-
TypeRes: "pr1",
525-
}
524+
pr1 := newTestPostResponse("pr1")
526525

527526
ctx := logutil.NewTestLoggerIntoContext(context.Background())
528527
ds := datastore.NewDatastore(t.Context(), nil)
@@ -558,14 +557,21 @@ func TestDirector_HandleResponse(t *testing.T) {
558557
}
559558
}
560559

560+
const (
561+
testPostResponseType = "test-post-response"
562+
)
563+
561564
type testPostResponse struct {
562-
TypeRes string
565+
plugins.TypedName
563566
lastRespOnResponse *Response
564567
lastTargetPodOnResponse string
565568
}
566569

567-
func (p *testPostResponse) Type() string { return p.TypeRes }
568-
func (p *testPostResponse) Name() string { return "test-post-response" }
570+
func newTestPostResponse(name string) *testPostResponse {
571+
return &testPostResponse{
572+
TypedName: plugins.NewTypedName(testPostResponseType, name),
573+
}
574+
}
569575

570576
func (p *testPostResponse) PostResponse(_ context.Context, _ *schedulingtypes.LLMRequest, response *Response, targetPod *backend.Pod) {
571577
p.lastRespOnResponse = response

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ const (
3636
// compile-time type assertion
3737
var _ framework.Filter = &DecisionTreeFilter{}
3838

39-
// DecisionTreeFilter applies current fitler, and then recursively applies next filters
39+
// DecisionTreeFilter applies current filter, and then recursively applies next filters
4040
// depending success or failure of the current filter.
4141
// It can be used to construct a flow chart algorithm.
42+
// Since a DecisionTreeFilter takes on the type and name of the current filter,
43+
// it is not embedding a fixed plugins.TypeName.
4244
type DecisionTreeFilter struct {
4345
Current framework.Filter
4446
// NextOnSuccess filter will be applied after successfully applying the current filter.

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import (
2525
"github.com/google/go-cmp/cmp/cmpopts"
2626
"github.com/google/uuid"
2727
k8stypes "k8s.io/apimachinery/pkg/types"
28+
2829
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend"
2930
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
31+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
3032
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/config"
3133
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework"
3234
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/scorer"
@@ -37,14 +39,14 @@ import (
3739
// compile-time type assertion
3840
var _ framework.Filter = &filterAll{}
3941

40-
type filterAll struct{}
41-
42-
func (f *filterAll) Type() string {
43-
return "filter-all"
42+
type filterAll struct {
43+
plugins.TypedName
4444
}
4545

46-
func (f *filterAll) Name() string {
47-
return "test-all"
46+
func newFilterAll() *filterAll {
47+
return &filterAll{
48+
TypedName: plugins.NewTypedName("filter-all", "test-all"),
49+
}
4850
}
4951

5052
func (f *filterAll) Filter(_ context.Context, _ *types.CycleState, _ *types.LLMRequest, pods []types.Pod) []types.Pod {
@@ -61,7 +63,7 @@ func TestFilter(t *testing.T) {
6163
}{
6264
{
6365
name: "simple filter filters all pods",
64-
filter: &filterAll{},
66+
filter: newFilterAll(),
6567
output: []types.Pod{},
6668
},
6769
{
@@ -274,7 +276,7 @@ func TestSubsettingFilter(t *testing.T) {
274276
}{
275277
{
276278
name: "SubsetFilter, filter not present — return all pods",
277-
filter: &SubsetFilter{},
279+
filter: NewSubsetFilter(),
278280
metadata: map[string]any{},
279281
input: []types.Pod{
280282
&types.PodMetrics{
@@ -295,7 +297,7 @@ func TestSubsettingFilter(t *testing.T) {
295297
},
296298
{
297299
name: "SubsetFilter, namespace present filter not present — return all pods",
298-
filter: &SubsetFilter{},
300+
filter: NewSubsetFilter(),
299301
metadata: map[string]any{"envoy.lb.subset_hint": map[string]any{}},
300302
input: []types.Pod{
301303
&types.PodMetrics{
@@ -316,7 +318,7 @@ func TestSubsettingFilter(t *testing.T) {
316318
},
317319
{
318320
name: "SubsetFilter, filter present with empty list — return no pods",
319-
filter: &SubsetFilter{},
321+
filter: NewSubsetFilter(),
320322
metadata: makeFilterMetadata([]interface{}{}),
321323
input: []types.Pod{
322324
&types.PodMetrics{
@@ -331,7 +333,7 @@ func TestSubsettingFilter(t *testing.T) {
331333
{
332334
name: "SubsetFilter, subset with one matching pod",
333335
metadata: makeFilterMetadata([]interface{}{"10.0.0.1"}),
334-
filter: &SubsetFilter{},
336+
filter: NewSubsetFilter(),
335337
input: []types.Pod{
336338
&types.PodMetrics{
337339
Pod: &backend.Pod{Address: "10.0.0.1"},
@@ -349,7 +351,7 @@ func TestSubsettingFilter(t *testing.T) {
349351
{
350352
name: "SubsetFilter, subset with multiple matching pods",
351353
metadata: makeFilterMetadata([]interface{}{"10.0.0.1", "10.0.0.2", "10.0.0.3"}),
352-
filter: &SubsetFilter{},
354+
filter: NewSubsetFilter(),
353355
input: []types.Pod{
354356
&types.PodMetrics{
355357
Pod: &backend.Pod{Address: "10.0.0.1"},
@@ -370,7 +372,7 @@ func TestSubsettingFilter(t *testing.T) {
370372
{
371373
name: "SubsetFilter, subset with no matching pods",
372374
metadata: makeFilterMetadata([]interface{}{"10.0.0.3"}),
373-
filter: &SubsetFilter{},
375+
filter: NewSubsetFilter(),
374376
input: []types.Pod{
375377
&types.PodMetrics{
376378
Pod: &backend.Pod{Address: "10.0.0.1"},
@@ -498,7 +500,7 @@ func TestDecisionTreeFilterFactory(t *testing.T) {
498500
}
499501

500502
cmpOptions := cmpopts.IgnoreUnexported(LeastKVCacheFilter{}, LeastQueueFilter{},
501-
LoraAffinityFilter{}, LowQueueFilter{}, scorer.KVCacheScorer{})
503+
LoraAffinityFilter{}, LowQueueFilter{}, scorer.KVCacheScorer{}, plugins.TypedName{})
502504

503505
for _, test := range tests {
504506
rawParameters := struct {

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func LeastKVCacheFilterFactory(name string, _ json.RawMessage, _ plugins.Handle)
4141
// NewLeastKVCacheFilter initializes a new LeastKVCacheFilter and returns its pointer.
4242
func NewLeastKVCacheFilter() *LeastKVCacheFilter {
4343
return &LeastKVCacheFilter{
44-
name: LeastKVCacheFilterType,
44+
TypedName: plugins.NewTypedName(LeastKVCacheFilterType, LeastKVCacheFilterType),
4545
}
4646
}
4747

@@ -51,22 +51,12 @@ func NewLeastKVCacheFilter() *LeastKVCacheFilter {
5151
// should consider them all instead of the absolute minimum one. This worked better than picking the
5252
// least one as it gives more choices for the next filter, which on aggregate gave better results.
5353
type LeastKVCacheFilter struct {
54-
name string
55-
}
56-
57-
// Type returns the type of the filter.
58-
func (f *LeastKVCacheFilter) Type() string {
59-
return LeastKVCacheFilterType
60-
}
61-
62-
// Name returns the name of the filter.
63-
func (f *LeastKVCacheFilter) Name() string {
64-
return f.name
54+
plugins.TypedName
6555
}
6656

6757
// WithName sets the name of the filter.
6858
func (f *LeastKVCacheFilter) WithName(name string) *LeastKVCacheFilter {
69-
f.name = name
59+
f.TypedName.SetName(name)
7060
return f
7161
}
7262

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func LeastQueueFilterFactory(name string, _ json.RawMessage, _ plugins.Handle) (
4141
// NewLeastQueueFilter initializes a new LeastQueueFilter and returns its pointer.
4242
func NewLeastQueueFilter() *LeastQueueFilter {
4343
return &LeastQueueFilter{
44-
name: LeastQueueFilterType,
44+
TypedName: plugins.NewTypedName(LeastQueueFilterType, LeastQueueFilterType),
4545
}
4646
}
4747

@@ -51,22 +51,12 @@ func NewLeastQueueFilter() *LeastQueueFilter {
5151
// we should consider them all instead of the absolute minimum one. This worked better than picking
5252
// the least one as it gives more choices for the next filter, which on aggregate gave better results.
5353
type LeastQueueFilter struct {
54-
name string
55-
}
56-
57-
// Type returns the type of the filter.
58-
func (f *LeastQueueFilter) Type() string {
59-
return LeastQueueFilterType
60-
}
61-
62-
// Name returns the name of the filter.
63-
func (f *LeastQueueFilter) Name() string {
64-
return f.name
54+
plugins.TypedName
6555
}
6656

6757
// WithName sets the name of the filter.
6858
func (f *LeastQueueFilter) WithName(name string) *LeastQueueFilter {
69-
f.name = name
59+
f.TypedName.SetName(name)
7060
return f
7161
}
7262

0 commit comments

Comments
 (0)