如何进行K8S源码分析启动
更新:HHH   时间:2023-1-7


这篇文章给大家介绍如何进行K8S源码分析启动,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

Controller Manager启动的过程比较简单,主要就是依次启动各个controllers。

启动的代码在/cmd/kube-controller-manager/app/controllermanager.go中,核心的是Run方法:

// Run runs the CMServer.  This should never exit.

func Run(s *options.CMServer) error {

   // 1. 验证CM配置

   if err := s.Validate(KnownControllers(), ControllersDisabledByDefault.List()); err != nil {

      return err

   }

 

   ....

   kubeClient, leaderElectionClient, kubeconfig, err := createClients(s)

   if err != nil {

      return err

   }

 

   go startHTTP(s)

 

   recorder := createRecorder(kubeClient)

 

   // 2. 创建controllers启动逻辑。run是真正用来启动controllers的函数

   run := func(stop <-chan struct{}) {

      rootClientBuilder := controller.SimpleControllerClientBuilder{

         ClientConfig: kubeconfig,

      }

      var clientBuilder controller.ControllerClientBuilder

      ....

      ctx, err := CreateControllerContext(s, rootClientBuilder, clientBuilder, stop)

      ....

      // NewControllerInitializers返回所有待启动的controllers,StartControllers则依次启动

      if err := StartControllers(ctx, saTokenControllerInitFunc, NewControllerInitializers()); err != nil {

         glog.Fatalf("error starting controllers: %v", err)

      }

 

      ctx.InformerFactory.Start(ctx.Stop)

      close(ctx.InformersStarted)

 

      select {}

   }

 

   ...

   // 3. 选主,由leader执行run函数

   leaderelection.RunOrDie(leaderelection.LeaderElectionConfig{

      Lock:          rl,

      LeaseDuration: s.LeaderElection.LeaseDuration.Duration,

      RenewDeadline: s.LeaderElection.RenewDeadline.Duration,

      RetryPeriod:   s.LeaderElection.RetryPeriod.Duration,

      Callbacks: leaderelection.LeaderCallbacks{

         OnStartedLeading: run,

         OnStoppedLeading: func() {

            glog.Fatalf("leaderelection lost")

         },

      },

   })

   panic("unreachable")

}

NewControllerInitializers()函数返回一个map[string]InitFunc的结构,包含所有需要启动的controllers的启动函数。
StartControllers则依次启动controllers:

func StartControllers(ctx ControllerContext, startSATokenController InitFunc, controllers map[string]InitFunc) error {

   // 依次调用controller init funcs

   for controllerName, initFn := range controllers {

      if !ctx.IsControllerEnabled(controllerName) {

         glog.Warningf("%q is disabled", controllerName)

         continue

      }

 

      time.Sleep(wait.Jitter(ctx.Options.ControllerStartInterval.Duration, ControllerStartJitter))

 

      glog.V(1).Infof("Starting %q", controllerName)

      started, err := initFn(ctx)

      if err != nil {

         glog.Errorf("Error starting %q", controllerName)

         return err

      }

      if !started {

         glog.Warningf("Skipping %q", controllerName)

         continue

      }

      glog.Infof("Started %q", controllerName)

   }

 

   return nil

}

关于如何进行K8S源码分析启动就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

返回云计算教程...