本文作为Kubernetes Scheduler源码分析的番外篇,补充一个方面的分析:从源码层面解析kube-scheduler的默认配置是怎么做的。
从头来看,在kube-scheduler的main函数中,s := options.NewSchedulerServer()
创建SchedulerServer时,是按照默认参数创建的。
--- plugin/cmd/kube-scheduler/scheduler.go:30 ---
func main() {
s := options.NewSchedulerServer()
s.AddFlags(pflag.CommandLine)
flag.InitFlags()
logs.InitLogs()
defer logs.FlushLogs()
verflag.PrintAndExitIfRequested()
if err := app.Run(s); err != nil {
glog.Fatalf("scheduler app failed to run: %v", err)
}
}
--- plugin/cmd/kube-scheduler/app/options/options.go:44 ---
// NewSchedulerServer creates a new SchedulerServer with default parameters
func NewSchedulerServer() *SchedulerServer {
versioned := &v1alpha1.KubeSchedulerConfiguration{}
api.Scheme.Default(versioned)
cfg := componentconfig.KubeSchedulerConfiguration{}
api.Scheme.Convert(versioned, &cfg, nil)
cfg.LeaderElection.LeaderElect = true
s := SchedulerServer{
KubeSchedulerConfiguration: cfg,
}
return &s
}
上面NewSchedulerServer
中api.Scheme.Default(versioned)
就是设置默认参数的操作。看看Default(...)的代码:
--- pkg/runtime/scheme.go:439 ---
// Default sets defaults on the provided Object.
func (s *Scheme) Default(src Object) {
if fn, ok := s.defaulterFuncs[reflect.TypeOf(src)]; ok {
fn(src)
}
}
原来Default(...)做的工作就是从Scheme.defaulterFuncs
这个Map中获取&v1alpha1.KubeSchedulerConfiguration{}
这个type对应的defaultFunc fn,并执行fn(&v1alpha1.KubeSchedulerConfiguration{})
来完成默认参数的配置。
OK,那么问题来了。这些type对应的defaultFunc是怎么register到Scheme.defaulterFuncs这个Map中的呢?
答案就在pkg/apis/componentconfig/v1alpha1/register.go
中定义的全局变量SchemeBuilder。SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes, addDefaultingFuncs)
在创建SchemeBuilder时就调用了addDefaultFuncs函数。注册defaultFunc的工作应该就是在addDefaultingFuncs方法中实现的。
看看addDefaultingFuncs
的实现,果不其然啊.SetDefaults_KubeSchedulerConfiguration
就是&v1alpha1.KubeSchedulerConfiguration{}
对应的defaultFuncs。
---- pkg/apis/componentconfig/v1alpha1/register.go ----
var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes, addDefaultingFuncs)
AddToScheme = SchemeBuilder.AddToScheme
)
---- pkg/apis/componentconfig/v1alpha1/defaults.go -----
func addDefaultingFuncs(scheme *kruntime.Scheme) error {
RegisterDefaults(scheme)
return scheme.AddDefaultingFuncs(
SetDefaults_KubeProxyConfiguration,
SetDefaults_KubeSchedulerConfiguration,
SetDefaults_LeaderElectionConfiguration,
SetDefaults_KubeletConfiguration,
)
}
func SetDefaults_KubeSchedulerConfiguration(obj *KubeSchedulerConfiguration) {
if obj.Port == 0 {
obj.Port = ports.SchedulerPort
}
if obj.Address == "" {
obj.Address = "0.0.0.0"
}
if obj.AlgorithmProvider == "" {
obj.AlgorithmProvider = "DefaultProvider"
}
if obj.ContentType == "" {
obj.ContentType = "application/vnd.kubernetes.protobuf"
}
if obj.KubeAPIQPS == 0 {
obj.KubeAPIQPS = 50.0
}
if obj.KubeAPIBurst == 0 {
obj.KubeAPIBurst = 100
}
if obj.SchedulerName == "" {
obj.SchedulerName = api.DefaultSchedulerName
}
if obj.HardPodAffinitySymmetricWeight == 0 {
obj.HardPodAffinitySymmetricWeight = api.DefaultHardPodAffinitySymmetricWeight
}
if obj.FailureDomains == "" {
obj.FailureDomains = api.DefaultFailureDomains
}
}
再结合plugin/cmd/kube-scheduler/app/options/options.go:57
定义的AddFlags,可得kube-scheduler的默认配置如下: