您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關從源碼層面解析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的默認配置如下:
port = 10251
address = "0.0.0.0"
algorithm-provider = "DefaultProvider"
content-type = "application/vnd.kubernetes.protobuf"
kube-api-qps = 50
kube-api-burst = 100
scheduler-name = "default-scheduler"
hard-pod-affinity-symmetric-weight = 1
failure-domains = "kubernetes.io/hostname,failure-domain.beta.kubernetes.io/zone,failure-domain.beta.kubernetes.io/region"
關于“從源碼層面解析kube-scheduler的默認配置是怎么做的”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。