@@ -19,11 +19,13 @@ package v1alpha1
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "reflect"
22
23
"time"
23
24
24
25
v1 "k8s.io/api/core/v1"
25
26
"k8s.io/apimachinery/pkg/runtime"
26
27
"k8s.io/apimachinery/pkg/types"
28
+ "k8s.io/apimachinery/pkg/util/sets"
27
29
"k8s.io/client-go/util/workqueue"
28
30
"k8s.io/klog"
29
31
"k8s.io/kubernetes/pkg/scheduler/apis/config"
@@ -101,169 +103,93 @@ func NewFramework(r Registry, plugins *config.Plugins, args []config.PluginConfi
101
103
}
102
104
}
103
105
104
- if plugins .PreFilter != nil {
105
- for _ , pf := range plugins .PreFilter .Enabled {
106
- if pg , ok := pluginsMap [pf .Name ]; ok {
107
- p , ok := pg .(PreFilterPlugin )
108
- if ! ok {
109
- return nil , fmt .Errorf ("plugin %q does not extend prefilter plugin" , pf .Name )
110
- }
111
- f .preFilterPlugins = append (f .preFilterPlugins , p )
112
- } else {
113
- return nil , fmt .Errorf ("prefilter plugin %q does not exist" , pf .Name )
114
- }
115
- }
106
+ if err := updatePluginList (reflect .ValueOf (& f .preFilterPlugins ), plugins .PreFilter , reflect .TypeOf ((* PreFilterPlugin )(nil )), pluginsMap ); err != nil {
107
+ return nil , err
116
108
}
117
109
118
- if plugins .Filter != nil {
119
- for _ , r := range plugins .Filter .Enabled {
120
- if pg , ok := pluginsMap [r .Name ]; ok {
121
- p , ok := pg .(FilterPlugin )
122
- if ! ok {
123
- return nil , fmt .Errorf ("plugin %q does not extend filter plugin" , r .Name )
124
- }
125
- f .filterPlugins = append (f .filterPlugins , p )
126
- } else {
127
- return nil , fmt .Errorf ("filter plugin %q does not exist" , r .Name )
128
- }
129
- }
110
+ if err := updatePluginList (reflect .ValueOf (& f .filterPlugins ), plugins .Filter , reflect .TypeOf ((* FilterPlugin )(nil )), pluginsMap ); err != nil {
111
+ return nil , err
130
112
}
131
113
132
- if plugins .Score != nil {
133
- for _ , sc := range plugins .Score .Enabled {
134
- if pg , ok := pluginsMap [sc .Name ]; ok {
135
- p , ok := pg .(ScorePlugin )
136
- if ! ok {
137
- return nil , fmt .Errorf ("plugin %q does not extend score plugin" , sc .Name )
138
- }
139
- if f .pluginNameToWeightMap [p .Name ()] == 0 {
140
- return nil , fmt .Errorf ("score plugin %q is not configured with weight" , p .Name ())
141
- }
142
- f .scorePlugins = append (f .scorePlugins , p )
143
- } else {
144
- return nil , fmt .Errorf ("score plugin %q does not exist" , sc .Name )
145
- }
146
- }
114
+ if err := updatePluginList (reflect .ValueOf (& f .reservePlugins ), plugins .Reserve , reflect .TypeOf ((* ReservePlugin )(nil )), pluginsMap ); err != nil {
115
+ return nil , err
147
116
}
148
117
149
- if plugins .Reserve != nil {
150
- for _ , r := range plugins .Reserve .Enabled {
151
- if pg , ok := pluginsMap [r .Name ]; ok {
152
- p , ok := pg .(ReservePlugin )
153
- if ! ok {
154
- return nil , fmt .Errorf ("plugin %q does not extend reserve plugin" , r .Name )
155
- }
156
- f .reservePlugins = append (f .reservePlugins , p )
157
- } else {
158
- return nil , fmt .Errorf ("reserve plugin %q does not exist" , r .Name )
159
- }
160
- }
118
+ if err := updatePluginList (reflect .ValueOf (& f .postFilterPlugins ), plugins .PostFilter , reflect .TypeOf ((* PostFilterPlugin )(nil )), pluginsMap ); err != nil {
119
+ return nil , err
161
120
}
162
121
163
- if plugins .PostFilter != nil {
164
- for _ , r := range plugins .PostFilter .Enabled {
165
- if pg , ok := pluginsMap [r .Name ]; ok {
166
- p , ok := pg .(PostFilterPlugin )
167
- if ! ok {
168
- return nil , fmt .Errorf ("plugin %q does not extend post-filter plugin" , r .Name )
169
- }
170
- f .postFilterPlugins = append (f .postFilterPlugins , p )
171
- } else {
172
- return nil , fmt .Errorf ("post-filter plugin %q does not exist" , r .Name )
173
- }
174
- }
122
+ if err := updatePluginList (reflect .ValueOf (& f .scorePlugins ), plugins .Score , reflect .TypeOf ((* ScorePlugin )(nil )), pluginsMap ); err != nil {
123
+ return nil , err
175
124
}
176
125
177
- if plugins .PreBind != nil {
178
- for _ , pb := range plugins .PreBind .Enabled {
179
- if pg , ok := pluginsMap [pb .Name ]; ok {
180
- p , ok := pg .(PreBindPlugin )
181
- if ! ok {
182
- return nil , fmt .Errorf ("plugin %q does not extend prebind plugin" , pb .Name )
183
- }
184
- f .preBindPlugins = append (f .preBindPlugins , p )
185
- } else {
186
- return nil , fmt .Errorf ("prebind plugin %q does not exist" , pb .Name )
187
- }
188
- }
126
+ if err := updatePluginList (reflect .ValueOf (& f .preBindPlugins ), plugins .PreBind , reflect .TypeOf ((* PreBindPlugin )(nil )), pluginsMap ); err != nil {
127
+ return nil , err
189
128
}
190
129
191
- if plugins .Bind != nil {
192
- for _ , pb := range plugins .Bind .Enabled {
193
- if pg , ok := pluginsMap [pb .Name ]; ok {
194
- p , ok := pg .(BindPlugin )
195
- if ! ok {
196
- return nil , fmt .Errorf ("plugin %q does not extend bind plugin" , pb .Name )
197
- }
198
- f .bindPlugins = append (f .bindPlugins , p )
199
- } else {
200
- return nil , fmt .Errorf ("bind plugin %q does not exist" , pb .Name )
201
- }
202
- }
130
+ if err := updatePluginList (reflect .ValueOf (& f .bindPlugins ), plugins .Bind , reflect .TypeOf ((* BindPlugin )(nil )), pluginsMap ); err != nil {
131
+ return nil , err
203
132
}
204
133
205
- if plugins .PostBind != nil {
206
- for _ , pb := range plugins .PostBind .Enabled {
207
- if pg , ok := pluginsMap [pb .Name ]; ok {
208
- p , ok := pg .(PostBindPlugin )
209
- if ! ok {
210
- return nil , fmt .Errorf ("plugin %q does not extend postbind plugin" , pb .Name )
211
- }
212
- f .postBindPlugins = append (f .postBindPlugins , p )
213
- } else {
214
- return nil , fmt .Errorf ("postbind plugin %q does not exist" , pb .Name )
215
- }
216
- }
134
+ if err := updatePluginList (reflect .ValueOf (& f .postBindPlugins ), plugins .PostBind , reflect .TypeOf ((* PostBindPlugin )(nil )), pluginsMap ); err != nil {
135
+ return nil , err
217
136
}
218
137
219
- if plugins .Unreserve != nil {
220
- for _ , ur := range plugins .Unreserve .Enabled {
221
- if pg , ok := pluginsMap [ur .Name ]; ok {
222
- p , ok := pg .(UnreservePlugin )
223
- if ! ok {
224
- return nil , fmt .Errorf ("plugin %q does not extend unreserve plugin" , ur .Name )
225
- }
226
- f .unreservePlugins = append (f .unreservePlugins , p )
227
- } else {
228
- return nil , fmt .Errorf ("unreserve plugin %q does not exist" , ur .Name )
229
- }
230
- }
138
+ if err := updatePluginList (reflect .ValueOf (& f .unreservePlugins ), plugins .Unreserve , reflect .TypeOf ((* UnreservePlugin )(nil )), pluginsMap ); err != nil {
139
+ return nil , err
231
140
}
232
141
233
- if plugins .Permit != nil {
234
- for _ , pr := range plugins .Permit .Enabled {
235
- if pg , ok := pluginsMap [pr .Name ]; ok {
236
- p , ok := pg .(PermitPlugin )
237
- if ! ok {
238
- return nil , fmt .Errorf ("plugin %q does not extend permit plugin" , pr .Name )
239
- }
240
- f .permitPlugins = append (f .permitPlugins , p )
241
- } else {
242
- return nil , fmt .Errorf ("permit plugin %q does not exist" , pr .Name )
243
- }
244
- }
142
+ if err := updatePluginList (reflect .ValueOf (& f .permitPlugins ), plugins .Permit , reflect .TypeOf ((* PermitPlugin )(nil )), pluginsMap ); err != nil {
143
+ return nil , err
245
144
}
246
145
247
- if plugins .QueueSort != nil {
248
- for _ , qs := range plugins .QueueSort .Enabled {
249
- if pg , ok := pluginsMap [qs .Name ]; ok {
250
- p , ok := pg .(QueueSortPlugin )
251
- if ! ok {
252
- return nil , fmt .Errorf ("plugin %q does not extend queue sort plugin" , qs .Name )
253
- }
254
- f .queueSortPlugins = append (f .queueSortPlugins , p )
255
- if len (f .queueSortPlugins ) > 1 {
256
- return nil , fmt .Errorf ("only one queue sort plugin can be enabled" )
257
- }
258
- } else {
259
- return nil , fmt .Errorf ("queue sort plugin %q does not exist" , qs .Name )
260
- }
146
+ if err := updatePluginList (reflect .ValueOf (& f .queueSortPlugins ), plugins .QueueSort , reflect .TypeOf ((* QueueSortPlugin )(nil )), pluginsMap ); err != nil {
147
+ return nil , err
148
+ }
149
+
150
+ for _ , scorePlugin := range f .scorePlugins {
151
+ if f .pluginNameToWeightMap [scorePlugin .Name ()] == 0 {
152
+ return nil , fmt .Errorf ("score plugin %q is not configured with weight" , scorePlugin .Name ())
261
153
}
262
154
}
263
155
156
+ if len (f .queueSortPlugins ) > 1 {
157
+ return nil , fmt .Errorf ("only one queue sort plugin can be enabled" )
158
+ }
159
+
264
160
return f , nil
265
161
}
266
162
163
+ func updatePluginList (pluginList reflect.Value , pluginSet * config.PluginSet , pluginType reflect.Type , pluginsMap map [string ]Plugin ) error {
164
+ if pluginSet == nil {
165
+ return nil
166
+ }
167
+
168
+ plugins := pluginList .Elem ()
169
+ pluginType = pluginType .Elem ()
170
+ set := sets .NewString ()
171
+ for _ , ep := range pluginSet .Enabled {
172
+ pg , ok := pluginsMap [ep .Name ]
173
+ if ! ok {
174
+ return fmt .Errorf ("%s %q does not exist" , pluginType .String (), ep .Name )
175
+ }
176
+
177
+ if ! reflect .TypeOf (pg ).Implements (pluginType ) {
178
+ return fmt .Errorf ("plugin %q does not extend %s plugin" , ep .Name , pluginType .String ())
179
+ }
180
+
181
+ if set .Has (ep .Name ) {
182
+ return fmt .Errorf ("plugin %q already registered as %q" , ep .Name , pluginType .String ())
183
+ }
184
+
185
+ set .Insert (ep .Name )
186
+
187
+ newPlugins := reflect .Append (plugins , reflect .ValueOf (pg ))
188
+ plugins .Set (newPlugins )
189
+ }
190
+ return nil
191
+ }
192
+
267
193
// QueueSortFunc returns the function to sort pods in scheduling queue
268
194
func (f * framework ) QueueSortFunc () LessFunc {
269
195
if len (f .queueSortPlugins ) == 0 {
0 commit comments