@@ -119,33 +119,29 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options, regist
119
119
verflag .PrintAndExitIfRequested ()
120
120
utilflag .PrintFlags (cmd .Flags ())
121
121
122
- if len (args ) != 0 {
123
- fmt .Fprint (os .Stderr , "arguments are not supported\n " )
124
- }
122
+ ctx , cancel := context .WithCancel (context .Background ())
123
+ defer cancel ()
125
124
126
- if errs := opts .Validate (); len (errs ) > 0 {
127
- return utilerrors .NewAggregate (errs )
125
+ cc , sched , err := Setup (ctx , args , opts , registryOptions ... )
126
+ if err != nil {
127
+ return err
128
128
}
129
129
130
130
if len (opts .WriteConfigTo ) > 0 {
131
- c := & schedulerserverconfig.Config {}
132
- if err := opts .ApplyTo (c ); err != nil {
133
- return err
134
- }
135
- if err := options .WriteConfigFile (opts .WriteConfigTo , & c .ComponentConfig ); err != nil {
131
+ if err := options .WriteConfigFile (opts .WriteConfigTo , & cc .ComponentConfig ); err != nil {
136
132
return err
137
133
}
138
134
klog .Infof ("Wrote configuration to: %s\n " , opts .WriteConfigTo )
139
135
return nil
140
136
}
141
137
142
- c , err := opts .Config ()
143
- if err != nil {
144
- return err
145
- }
138
+ return Run (ctx , cc , sched )
139
+ }
146
140
147
- // Get the completed config
148
- cc := c .Complete ()
141
+ // Run executes the scheduler based on the given configuration. It only returns on error or when context is done.
142
+ func Run (ctx context.Context , cc * schedulerserverconfig.CompletedConfig , sched * scheduler.Scheduler ) error {
143
+ // To help debugging, immediately log version
144
+ klog .V (1 ).Infof ("Starting Kubernetes Scheduler version %+v" , version .Get ())
149
145
150
146
// Configz registration.
151
147
if cz , err := configz .New ("componentconfig" ); err == nil {
@@ -154,45 +150,6 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options, regist
154
150
return fmt .Errorf ("unable to register configz: %s" , err )
155
151
}
156
152
157
- ctx , cancel := context .WithCancel (context .Background ())
158
- defer cancel ()
159
-
160
- return Run (ctx , cc , registryOptions ... )
161
- }
162
-
163
- // Run executes the scheduler based on the given configuration. It only returns on error or when context is done.
164
- func Run (ctx context.Context , cc schedulerserverconfig.CompletedConfig , outOfTreeRegistryOptions ... Option ) error {
165
- // To help debugging, immediately log version
166
- klog .V (1 ).Infof ("Starting Kubernetes Scheduler version %+v" , version .Get ())
167
-
168
- outOfTreeRegistry := make (framework.Registry )
169
- for _ , option := range outOfTreeRegistryOptions {
170
- if err := option (outOfTreeRegistry ); err != nil {
171
- return err
172
- }
173
- }
174
-
175
- recorderFactory := getRecorderFactory (& cc )
176
- // Create the scheduler.
177
- sched , err := scheduler .New (cc .Client ,
178
- cc .InformerFactory ,
179
- cc .PodInformer ,
180
- recorderFactory ,
181
- ctx .Done (),
182
- scheduler .WithProfiles (cc .ComponentConfig .Profiles ... ),
183
- scheduler .WithAlgorithmSource (cc .ComponentConfig .AlgorithmSource ),
184
- scheduler .WithPreemptionDisabled (cc .ComponentConfig .DisablePreemption ),
185
- scheduler .WithPercentageOfNodesToScore (cc .ComponentConfig .PercentageOfNodesToScore ),
186
- scheduler .WithBindTimeoutSeconds (cc .ComponentConfig .BindTimeoutSeconds ),
187
- scheduler .WithFrameworkOutOfTreeRegistry (outOfTreeRegistry ),
188
- scheduler .WithPodMaxBackoffSeconds (cc .ComponentConfig .PodMaxBackoffSeconds ),
189
- scheduler .WithPodInitialBackoffSeconds (cc .ComponentConfig .PodInitialBackoffSeconds ),
190
- scheduler .WithExtenders (cc .ComponentConfig .Extenders ... ),
191
- )
192
- if err != nil {
193
- return err
194
- }
195
-
196
153
// Prepare the event broadcaster.
197
154
if cc .Broadcaster != nil && cc .EventClient != nil {
198
155
cc .Broadcaster .StartRecordingToSink (ctx .Done ())
@@ -340,3 +297,52 @@ func WithPlugin(name string, factory framework.PluginFactory) Option {
340
297
return registry .Register (name , factory )
341
298
}
342
299
}
300
+
301
+ // Setup creates a completed config and a scheduler based on the command args and options
302
+ func Setup (ctx context.Context , args []string , opts * options.Options , outOfTreeRegistryOptions ... Option ) (* schedulerserverconfig.CompletedConfig , * scheduler.Scheduler , error ) {
303
+ if len (args ) != 0 {
304
+ fmt .Fprint (os .Stderr , "arguments are not supported\n " )
305
+ }
306
+
307
+ if errs := opts .Validate (); len (errs ) > 0 {
308
+ return nil , nil , utilerrors .NewAggregate (errs )
309
+ }
310
+
311
+ c , err := opts .Config ()
312
+ if err != nil {
313
+ return nil , nil , err
314
+ }
315
+
316
+ // Get the completed config
317
+ cc := c .Complete ()
318
+
319
+ outOfTreeRegistry := make (framework.Registry )
320
+ for _ , option := range outOfTreeRegistryOptions {
321
+ if err := option (outOfTreeRegistry ); err != nil {
322
+ return nil , nil , err
323
+ }
324
+ }
325
+
326
+ recorderFactory := getRecorderFactory (& cc )
327
+ // Create the scheduler.
328
+ sched , err := scheduler .New (cc .Client ,
329
+ cc .InformerFactory ,
330
+ cc .PodInformer ,
331
+ recorderFactory ,
332
+ ctx .Done (),
333
+ scheduler .WithProfiles (cc .ComponentConfig .Profiles ... ),
334
+ scheduler .WithAlgorithmSource (cc .ComponentConfig .AlgorithmSource ),
335
+ scheduler .WithPreemptionDisabled (cc .ComponentConfig .DisablePreemption ),
336
+ scheduler .WithPercentageOfNodesToScore (cc .ComponentConfig .PercentageOfNodesToScore ),
337
+ scheduler .WithBindTimeoutSeconds (cc .ComponentConfig .BindTimeoutSeconds ),
338
+ scheduler .WithFrameworkOutOfTreeRegistry (outOfTreeRegistry ),
339
+ scheduler .WithPodMaxBackoffSeconds (cc .ComponentConfig .PodMaxBackoffSeconds ),
340
+ scheduler .WithPodInitialBackoffSeconds (cc .ComponentConfig .PodInitialBackoffSeconds ),
341
+ scheduler .WithExtenders (cc .ComponentConfig .Extenders ... ),
342
+ )
343
+ if err != nil {
344
+ return nil , nil , err
345
+ }
346
+
347
+ return & cc , sched , nil
348
+ }
0 commit comments