Skip to content

Commit 72102c0

Browse files
authored
Merge pull request kubernetes#88728 from notpad/feature/scheduler_e2e_test
Add test for kube-scheduler command setup
2 parents 1a85614 + 16015a6 commit 72102c0

File tree

5 files changed

+393
-72
lines changed

5 files changed

+393
-72
lines changed

cmd/kube-scheduler/app/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
33
load(
44
"@io_bazel_rules_go//go:def.bzl",
55
"go_library",
6+
"go_test",
67
)
78

89
go_library(
@@ -65,3 +66,15 @@ filegroup(
6566
],
6667
tags = ["automanaged"],
6768
)
69+
70+
go_test(
71+
name = "go_default_test",
72+
srcs = ["server_test.go"],
73+
embed = [":go_default_library"],
74+
deps = [
75+
"//cmd/kube-scheduler/app/options:go_default_library",
76+
"//pkg/scheduler/apis/config:go_default_library",
77+
"//vendor/github.com/google/go-cmp/cmp:go_default_library",
78+
"//vendor/github.com/spf13/pflag:go_default_library",
79+
],
80+
)

cmd/kube-scheduler/app/server.go

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,29 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options, regist
119119
verflag.PrintAndExitIfRequested()
120120
utilflag.PrintFlags(cmd.Flags())
121121

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()
125124

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
128128
}
129129

130130
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 {
136132
return err
137133
}
138134
klog.Infof("Wrote configuration to: %s\n", opts.WriteConfigTo)
139135
return nil
140136
}
141137

142-
c, err := opts.Config()
143-
if err != nil {
144-
return err
145-
}
138+
return Run(ctx, cc, sched)
139+
}
146140

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())
149145

150146
// Configz registration.
151147
if cz, err := configz.New("componentconfig"); err == nil {
@@ -154,45 +150,6 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options, regist
154150
return fmt.Errorf("unable to register configz: %s", err)
155151
}
156152

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-
196153
// Prepare the event broadcaster.
197154
if cc.Broadcaster != nil && cc.EventClient != nil {
198155
cc.Broadcaster.StartRecordingToSink(ctx.Done())
@@ -340,3 +297,52 @@ func WithPlugin(name string, factory framework.PluginFactory) Option {
340297
return registry.Register(name, factory)
341298
}
342299
}
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

Comments
 (0)