1. 程式人生 > >從原始碼解析kube-scheduler預設的配置

從原始碼解析kube-scheduler預設的配置

本文作為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 }

上面NewSchedulerServerapi.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”