@@ -8,11 +8,8 @@ import (
88)
99
1010const (
11- // DecodeFilterType is the type of the DecodeFilter
12- DecodeFilterType = "decode-filter"
13-
14- // PrefillFilterType is the type of the PrefillFilter
15- PrefillFilterType = "prefill-filter"
11+ // ByRoleLabelFilterType is the type of the ByLabelsFilter
12+ ByRoleLabelFilterType = "role-label"
1613
1714 // RoleLabel name
1815 RoleLabel = "llm-d.ai/role"
@@ -24,90 +21,64 @@ const (
2421 RoleBoth = "both"
2522)
2623
27- // compile-time type assertion
28- var _ framework.Filter = & PrefillFilter {}
29-
30- // NewPrefillFilter creates a new instance of the DecodeFilter
31- func NewPrefillFilter () * PrefillFilter {
32- return & PrefillFilter {
33- name : PrefillFilterType ,
34- }
35- }
36-
37- // PrefillFilter - filters out pods that are not marked with role Prefill
38- type PrefillFilter struct {
24+ // ByRoleLabel - filters out pods based on the role defined by RoleLabel label
25+ type ByRoleLabel struct {
26+ // name defines the filter name
3927 name string
28+ // validRoles defines list of valid role header values
29+ validRoles map [string ]bool
30+ // allowsNoRolesLabel - if true pods without role label will be considered as valid (not filtered out)
31+ allowsNoRolesLabel bool
4032}
4133
42- // Type returns the type of the filter
43- func (pf * PrefillFilter ) Type () string {
44- return PrefillFilterType
45- }
46-
47- // Name returns the name of the instance of the filter.
48- func (pf * PrefillFilter ) Name () string {
49- return pf .name
50- }
51-
52- // WithName sets the name of the filter.
53- func (pf * PrefillFilter ) WithName (name string ) * PrefillFilter {
54- pf .name = name
55- return pf
56- }
34+ var _ framework.Filter = & ByRoleLabel {} // validate interface conformance
5735
58- // Filter filters out all pods that are not marked as "prefill"
59- func (pf * PrefillFilter ) Filter (_ context.Context , _ * types.CycleState , _ * types.LLMRequest , pods []types.Pod ) []types.Pod {
60- filteredPods := []types.Pod {}
36+ // NewByRoleLabel creates and returns an instance of the RoleBasedFilter based on the input parameters
37+ // name - the filter name
38+ // rolesArr - list of valid roles
39+ func NewByRoleLabel (name string , allowsNoRolesLabel bool , rolesArr ... string ) * ByRoleLabel {
40+ roles := map [string ]bool {}
6141
62- for _ , pod := range pods {
63- role := pod .GetPod ().Labels [RoleLabel ]
64- if role == RolePrefill { // TODO: doesn't RoleBoth also imply Prefill?
65- filteredPods = append (filteredPods , pod )
66- }
42+ for _ , role := range rolesArr {
43+ roles [role ] = true
6744 }
68- return filteredPods
69- }
70-
71- // compile-time type assertion
72- var _ framework.Filter = & DecodeFilter {}
7345
74- // NewDecodeFilter creates a new instance of the DecodeFilter
75- func NewDecodeFilter () * DecodeFilter {
76- return & DecodeFilter {
77- name : DecodeFilterType ,
78- }
46+ return & ByRoleLabel {name : name , allowsNoRolesLabel : allowsNoRolesLabel , validRoles : roles }
7947}
8048
81- // DecodeFilter - filters out pods that are not marked with role Decode or Both
82- type DecodeFilter struct {
83- name string
49+ // NewPrefillFilter creates and returns an instance of the Filter configured for prefill role
50+ func NewPrefillFilter () framework. Filter {
51+ return NewByRoleLabel ( "prefill-filter" , false , RolePrefill )
8452}
8553
86- // Type returns the type of the filter
87- func ( df * DecodeFilter ) Type () string {
88- return DecodeFilterType
54+ // NewDecodeFilter creates and returns an instance of the Filter configured for decode role
55+ func NewDecodeFilter () framework. Filter {
56+ return NewByRoleLabel ( "decode-filter" , true , RoleDecode , RoleBoth )
8957}
9058
91- // Name returns the name of the instance of the filter.
92- func (df * DecodeFilter ) Name () string {
93- return df . name
59+ // Type returns the type of the filter
60+ func (f * ByRoleLabel ) Type () string {
61+ return ByRoleLabelFilterType
9462}
9563
96- // WithName sets the name of the filter.
97- func (df * DecodeFilter ) WithName (name string ) * DecodeFilter {
98- df .name = name
99- return df
64+ // Name returns the name of the filter
65+ func (f * ByRoleLabel ) Name () string {
66+ return f .name
10067}
10168
102- // Filter removes all pods that are not marked as "decode" or "both"
103- func (df * DecodeFilter ) Filter (_ context.Context , _ * types.CycleState , _ * types.LLMRequest , pods []types.Pod ) []types.Pod {
69+ // Filter filters out all pods that are not marked with one of roles from the validRoles collection
70+ // or has no role label in case allowsNoRolesLabel is true
71+ func (f * ByRoleLabel ) Filter (_ context.Context , _ * types.CycleState , _ * types.LLMRequest , pods []types.Pod ) []types.Pod {
10472 filteredPods := []types.Pod {}
10573
10674 for _ , pod := range pods {
107- role , defined := pod .GetPod ().Labels [RoleLabel ]
108- if ! defined || role == RoleDecode || role == RoleBoth {
75+ role , labelDefined := pod .GetPod ().Labels [RoleLabel ]
76+ _ , roleExists := f .validRoles [role ]
77+
78+ if (! labelDefined && f .allowsNoRolesLabel ) || roleExists {
10979 filteredPods = append (filteredPods , pod )
11080 }
11181 }
82+
11283 return filteredPods
11384}
0 commit comments