New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
scheduler: refactor main entry Run() #38572
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,97 +73,46 @@ through the API as necessary.`, | |
|
||
// Run runs the specified SchedulerServer. This should never exit. | ||
func Run(s *options.SchedulerServer) error { | ||
if c, err := configz.New("componentconfig"); err == nil { | ||
c.Set(s.KubeSchedulerConfiguration) | ||
} else { | ||
glog.Errorf("unable to register configz: %s", err) | ||
} | ||
kubeconfig, err := clientcmd.BuildConfigFromFlags(s.Master, s.Kubeconfig) | ||
if err != nil { | ||
glog.Errorf("unable to build config from flags: %v", err) | ||
return err | ||
} | ||
|
||
kubeconfig.ContentType = s.ContentType | ||
// Override kubeconfig qps/burst settings from flags | ||
kubeconfig.QPS = s.KubeAPIQPS | ||
kubeconfig.Burst = int(s.KubeAPIBurst) | ||
|
||
kubecli, err := createClient(s) | ||
if err != nil { | ||
glog.Fatalf("Invalid API configuration: %v", err) | ||
return fmt.Errorf("unable to create kube client: %v", err) | ||
} | ||
leaderElectionClient, err := clientset.NewForConfig(restclient.AddUserAgent(kubeconfig, "leader-election")) | ||
config, err := createConfig(s, kubecli) | ||
if err != nil { | ||
glog.Fatalf("Invalid API configuration: %v", err) | ||
return fmt.Errorf("failed to create scheduler configuration: %v", err) | ||
} | ||
|
||
go func() { | ||
mux := http.NewServeMux() | ||
healthz.InstallHandler(mux) | ||
if s.EnableProfiling { | ||
mux.HandleFunc("/debug/pprof/", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile) | ||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | ||
if s.EnableContentionProfiling { | ||
goruntime.SetBlockProfileRate(1) | ||
} | ||
} | ||
configz.InstallHandler(mux) | ||
mux.Handle("/metrics", prometheus.Handler()) | ||
|
||
server := &http.Server{ | ||
Addr: net.JoinHostPort(s.Address, strconv.Itoa(int(s.Port))), | ||
Handler: mux, | ||
} | ||
glog.Fatal(server.ListenAndServe()) | ||
}() | ||
|
||
configFactory := factory.NewConfigFactory(leaderElectionClient, s.SchedulerName, s.HardPodAffinitySymmetricWeight, s.FailureDomains) | ||
config, err := createConfig(s, configFactory) | ||
|
||
if err != nil { | ||
glog.Fatalf("Failed to create scheduler configuration: %v", err) | ||
} | ||
|
||
eventBroadcaster := record.NewBroadcaster() | ||
config.Recorder = eventBroadcaster.NewRecorder(v1.EventSource{Component: s.SchedulerName}) | ||
eventBroadcaster.StartLogging(glog.Infof) | ||
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: leaderElectionClient.Core().Events("")}) | ||
|
||
sched := scheduler.New(config) | ||
|
||
go startHTTP(s) | ||
|
||
run := func(_ <-chan struct{}) { | ||
sched.Run() | ||
select {} | ||
} | ||
|
||
if !s.LeaderElection.LeaderElect { | ||
run(nil) | ||
glog.Fatal("this statement is unreachable") | ||
panic("unreachable") | ||
} | ||
|
||
id, err := os.Hostname() | ||
if err != nil { | ||
glog.Errorf("unable to get hostname: %v", err) | ||
return err | ||
return fmt.Errorf("unable to get hostname: %v", err) | ||
} | ||
|
||
// TODO: enable other lock types | ||
rl := resourcelock.EndpointsLock{ | ||
rl := &resourcelock.EndpointsLock{ | ||
EndpointsMeta: v1.ObjectMeta{ | ||
Namespace: "kube-system", | ||
Name: "kube-scheduler", | ||
}, | ||
Client: leaderElectionClient, | ||
Client: kubecli, | ||
LockConfig: resourcelock.ResourceLockConfig{ | ||
Identity: id, | ||
EventRecorder: config.Recorder, | ||
}, | ||
} | ||
|
||
leaderelection.RunOrDie(leaderelection.LeaderElectionConfig{ | ||
Lock: &rl, | ||
Lock: rl, | ||
LeaseDuration: s.LeaderElection.LeaseDuration.Duration, | ||
RenewDeadline: s.LeaderElection.RenewDeadline.Duration, | ||
RetryPeriod: s.LeaderElection.RetryPeriod.Duration, | ||
|
@@ -174,12 +123,55 @@ func Run(s *options.SchedulerServer) error { | |
}, | ||
}, | ||
}) | ||
|
||
glog.Fatal("this statement is unreachable") | ||
panic("unreachable") | ||
} | ||
|
||
func createConfig(s *options.SchedulerServer, configFactory *factory.ConfigFactory) (*scheduler.Config, error) { | ||
func startHTTP(s *options.SchedulerServer) { | ||
mux := http.NewServeMux() | ||
healthz.InstallHandler(mux) | ||
if s.EnableProfiling { | ||
mux.HandleFunc("/debug/pprof/", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile) | ||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | ||
if s.EnableContentionProfiling { | ||
goruntime.SetBlockProfileRate(1) | ||
} | ||
} | ||
if c, err := configz.New("componentconfig"); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's difficult to tell, but did you ensure this was the 1st place configz was used in the shuffling. B4 it was the 1st line before refactoring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure it's fine. Tested and verified. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why it needs to be put 1st line. |
||
c.Set(s.KubeSchedulerConfiguration) | ||
} else { | ||
glog.Errorf("unable to register configz: %s", err) | ||
} | ||
configz.InstallHandler(mux) | ||
mux.Handle("/metrics", prometheus.Handler()) | ||
|
||
server := &http.Server{ | ||
Addr: net.JoinHostPort(s.Address, strconv.Itoa(int(s.Port))), | ||
Handler: mux, | ||
} | ||
glog.Fatal(server.ListenAndServe()) | ||
} | ||
|
||
func createClient(s *options.SchedulerServer) (*clientset.Clientset, error) { | ||
kubeconfig, err := clientcmd.BuildConfigFromFlags(s.Master, s.Kubeconfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to build config from flags: %v", err) | ||
} | ||
|
||
kubeconfig.ContentType = s.ContentType | ||
// Override kubeconfig qps/burst settings from flags | ||
kubeconfig.QPS = s.KubeAPIQPS | ||
kubeconfig.Burst = int(s.KubeAPIBurst) | ||
|
||
cli, err := clientset.NewForConfig(restclient.AddUserAgent(kubeconfig, "leader-election")) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid API configuration: %v", err) | ||
} | ||
return cli, nil | ||
} | ||
|
||
func createConfig(s *options.SchedulerServer, kubecli *clientset.Clientset) (*scheduler.Config, error) { | ||
configFactory := factory.NewConfigFactory(kubecli, s.SchedulerName, s.HardPodAffinitySymmetricWeight, s.FailureDomains) | ||
if _, err := os.Stat(s.PolicyConfigFile); err == nil { | ||
var ( | ||
policy schedulerapi.Policy | ||
|
@@ -196,5 +188,15 @@ func createConfig(s *options.SchedulerServer, configFactory *factory.ConfigFacto | |
} | ||
|
||
// if the config file isn't provided, use the specified (or default) provider | ||
return configFactory.CreateFromProvider(s.AlgorithmProvider) | ||
config, err := configFactory.CreateFromProvider(s.AlgorithmProvider) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
eventBroadcaster := record.NewBroadcaster() | ||
config.Recorder = eventBroadcaster.NewRecorder(v1.EventSource{Component: s.SchedulerName}) | ||
eventBroadcaster.StartLogging(glog.Infof) | ||
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: kubecli.Core().Events("")}) | ||
|
||
return config, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to re-wrap the error or just return them? I just debate the utility if we're refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous code usually Fatalf() right there. Now We Fatalf() outside, and thus it's better to wrap them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k.