Skip to content

Commit 51b5570

Browse files
committed
introduce TypedName to reduce boilerplate, modify plugins
Signed-off-by: Etai Lev Ran <[email protected]>
1 parent 3322cf8 commit 51b5570

File tree

17 files changed

+314
-218
lines changed

17 files changed

+314
-218
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/plugins/typedname.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
// TypedName is a utility struct providing a type and a name
20+
// to plugins.
21+
// It implements the Plugin interface and can be embedded in
22+
// plugins across the code to reduce boilerplate.
23+
type TypedName struct {
24+
objType string
25+
objName string
26+
}
27+
28+
// NewTypedName returns a new TypedName struct configured with
29+
// the given type and name.
30+
func NewTypedName(objtype, objname string) TypedName {
31+
return TypedName{
32+
objType: objtype,
33+
objName: objname,
34+
}
35+
}
36+
37+
// Type returns the type of the plugin.
38+
func (tn *TypedName) Type() string {
39+
return tn.objType
40+
}
41+
42+
// Name returns the name of this plugin instance.
43+
func (tn *TypedName) Name() string {
44+
return tn.objName
45+
}
46+
47+
// SetName sets the instance name.
48+
func (tn *TypedName) SetName(name string) {
49+
tn.objName = name
50+
}

pkg/epp/requestcontrol/director_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
3838
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
3939
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/handlers"
40+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
4041
schedulingtypes "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
4142
errutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/error"
4243
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
@@ -653,9 +654,7 @@ func pointer(v int32) *int32 {
653654
}
654655

655656
func TestDirector_HandleResponse(t *testing.T) {
656-
pr1 := &testPostResponse{
657-
TypeRes: "pr1",
658-
}
657+
pr1 := newTestPostResponse("pr1")
659658

660659
ctx := logutil.NewTestLoggerIntoContext(context.Background())
661660
ds := datastore.NewDatastore(t.Context(), nil)
@@ -691,14 +690,21 @@ func TestDirector_HandleResponse(t *testing.T) {
691690
}
692691
}
693692

693+
const (
694+
testPostResponseType = "test-post-response"
695+
)
696+
694697
type testPostResponse struct {
695-
TypeRes string
698+
plugins.TypedName
696699
lastRespOnResponse *Response
697700
lastTargetPodOnResponse string
698701
}
699702

700-
func (p *testPostResponse) Type() string { return p.TypeRes }
701-
func (p *testPostResponse) Name() string { return "test-post-response" }
703+
func newTestPostResponse(name string) *testPostResponse {
704+
return &testPostResponse{
705+
TypedName: plugins.NewTypedName(testPostResponseType, name),
706+
}
707+
}
702708

703709
func (p *testPostResponse) PostResponse(_ context.Context, _ *schedulingtypes.LLMRequest, response *Response, targetPod *backend.Pod) {
704710
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.

0 commit comments

Comments
 (0)