From e8ca249b29d89e22b4a985f18e95c5915621d047 Mon Sep 17 00:00:00 2001 From: dongming Date: Thu, 15 Dec 2022 15:58:13 +0800 Subject: [PATCH] l-35 --- controllers/app_controller.go | 4 ++-- .../controller-runtime/pkg/manager/internal.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/controllers/app_controller.go b/controllers/app_controller.go index 1e87286..ace0765 100644 --- a/controllers/app_controller.go +++ b/controllers/app_controller.go @@ -58,7 +58,7 @@ func (r *AppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R // 完成了将 reconciler 加入 controller中,之后将controller加入manager中 // SetupWithManager sets up the controller with the Manager. func (r *AppReconciler) SetupWithManager(mgr ctrl.Manager) error { - return ctrl.NewControllerManagedBy(mgr). + return ctrl.NewControllerManagedBy(mgr). // 创建 controller For(&demov1.App{}). // 将我们当前的crd加入“过滤器”可以让controller订阅到crd的变化 - Complete(r) + Complete(r) // 最终将 controller 加入,完成创建过程 } diff --git a/vendor/sigs.k8s.io/controller-runtime/pkg/manager/internal.go b/vendor/sigs.k8s.io/controller-runtime/pkg/manager/internal.go index 6a6665b..946d34c 100644 --- a/vendor/sigs.k8s.io/controller-runtime/pkg/manager/internal.go +++ b/vendor/sigs.k8s.io/controller-runtime/pkg/manager/internal.go @@ -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