master
dongming 2 years ago
parent e7a3b07bb2
commit e8ca249b29

@ -58,7 +58,7 @@ func (r *AppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
// 完成了将 reconciler 加入 controller中之后将controller加入manager中 // 完成了将 reconciler 加入 controller中之后将controller加入manager中
// SetupWithManager sets up the controller with the Manager. // SetupWithManager sets up the controller with the Manager.
func (r *AppReconciler) SetupWithManager(mgr ctrl.Manager) error { func (r *AppReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr). return ctrl.NewControllerManagedBy(mgr). // 创建 controller
For(&demov1.App{}). // 将我们当前的crd加入“过滤器”可以让controller订阅到crd的变化 For(&demov1.App{}). // 将我们当前的crd加入“过滤器”可以让controller订阅到crd的变化
Complete(r) Complete(r) // 最终将 controller 加入,完成创建过程
} }

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

Loading…
Cancel
Save