|
|
|
@ -391,6 +391,7 @@ func (cm *controllerManager) httpServe(kind string, log logr.Logger, server *htt
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// manager 的 start 方法没本身基本没有启动任何进程,都是管理它的子进程
|
|
|
|
|
// Start starts the manager and waits indefinitely.
|
|
|
|
|
// There is only two ways to have start return:
|
|
|
|
|
// An error has occurred during in one of the internal operations,
|
|
|
|
@ -411,12 +412,16 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// 创建了上下文及控制它的cancel方法,开控制它的子进程
|
|
|
|
|
// Initialize the internal context.
|
|
|
|
|
cm.internalCtx, cm.internalCancel = context.WithCancel(ctx)
|
|
|
|
|
|
|
|
|
|
// 定义最终停止的信号
|
|
|
|
|
// This chan indicates that stop is complete, in other words all runnables have returned or timeout on stop request
|
|
|
|
|
stopComplete := make(chan struct{})
|
|
|
|
|
defer close(stopComplete)
|
|
|
|
|
|
|
|
|
|
// 这个方法要放在 close(stopComplete) 之后,否则死锁
|
|
|
|
|
// This must be deferred after closing stopComplete, otherwise we deadlock.
|
|
|
|
|
defer func() {
|
|
|
|
|
// https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/gettyimages-459889618-1533579787.jpg
|
|
|
|
@ -433,11 +438,13 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// cluster 也是一个 runnable,会被加入 runnable 指定频道,来管理 manager 的生命周期。
|
|
|
|
|
// Add the cluster runnable.
|
|
|
|
|
if err := cm.add(cm.cluster); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to add cluster to runnables: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是否存在监控相关的 listener,存在就启动
|
|
|
|
|
// Metrics should be served whether the controller is leader or not.
|
|
|
|
|
// (If we don't serve metrics for non-leaders, prometheus will still scrape
|
|
|
|
|
// the pod but will get a connection refused).
|
|
|
|
@ -445,11 +452,13 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
|
|
|
|
|
cm.serveMetrics()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是否存在探活相关的 listener,存在就启动
|
|
|
|
|
// Serve health probes.
|
|
|
|
|
if cm.healthProbeListener != nil {
|
|
|
|
|
cm.serveHealthProbes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 启动 webhook 频道中的 runnables
|
|
|
|
|
// First start any webhook servers, which includes conversion, validation, and defaulting
|
|
|
|
|
// webhooks that are registered.
|
|
|
|
|
//
|
|
|
|
@ -462,6 +471,7 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 启动 cache 频道中的 runnables
|
|
|
|
|
// Start and wait for caches.
|
|
|
|
|
if err := cm.runnables.Caches.Start(cm.internalCtx); err != nil {
|
|
|
|
|
if !errors.Is(err, wait.ErrWaitTimeout) {
|
|
|
|
@ -469,6 +479,7 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 启动 其他 频道中的 runnables
|
|
|
|
|
// Start the non-leaderelection Runnables after the cache has synced.
|
|
|
|
|
if err := cm.runnables.Others.Start(cm.internalCtx); err != nil {
|
|
|
|
|
if !errors.Is(err, wait.ErrWaitTimeout) {
|
|
|
|
@ -482,10 +493,12 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
|
|
|
|
|
cm.leaderElectionCancel = cancel
|
|
|
|
|
go func() {
|
|
|
|
|
if cm.resourceLock != nil {
|
|
|
|
|
// 以 leaderElection 的方式启动,启动 Election 频道中的 runnables
|
|
|
|
|
if err := cm.startLeaderElection(ctx); err != nil {
|
|
|
|
|
cm.errChan <- err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 以非 leaderElection 的方式启动。所有都当作 leaderElection,启动 Election 频道中的 runnables
|
|
|
|
|
// Treat not having leader election enabled the same as being elected.
|
|
|
|
|
if err := cm.startLeaderElectionRunnables(); err != nil {
|
|
|
|
|
cm.errChan <- err
|
|
|
|
|