(接上篇)
在Register()函數中對EdgeHub?struct的初始化只是對EdgeHub?struct中的controller進行初始化。controller的初始化函數具體如下所示。
KubeEdge/edge/pkg/edgehub/controller.go |
//NewEdgeHubController creates and returns a EdgeHubController?object func?NewEdgeHubController() *Controller { return &Controller{ config: ????&config.GetConfig().CtrConfig, stopChan: ??make(chan struct{}), syncKeeper: make(map[string]chan model.Message), } } |
NewEdgeHubController()函數中嵌套了一個獲取配置信息的函數調用,具體如下所示。
config: ????&config.GetConfig().CtrConfig |
config.GetConfig()函數定義具體如下所示。
KubeEdge/edge/pkg/edgehub/config/config.go |
var edgeHubConfig?EdgeHubConfig ... //GetConfig?returns the EdgeHub?configuration object func?GetConfig() *EdgeHubConfig?{ return &edgeHubConfig } |
GetConfig()函數只返回了&edgeHubConfig,而edgeHubConfig是一個EdgeHubConfig?struct類型的全局變量。至于該變量是在哪里被賦值,怎么賦值的,暫且留個疑問——* EdgeHubConfig賦值疑問。
到此,EdgeHub?struct的初始化就告一段落了。下面分析EdgeHub的啟動函數,具體如下所示。
KubeEdge/edge/pkg/edgehub/module.go |
//Start sets context and starts the controller func?(eh *EdgeHub) Start(c *context.Context) { eh.context = c eh.controller.Start(c) } |
EdgeHub啟動函數Start()只做了兩件事。
- 接收并存儲傳入的消息管道;
- 啟動EdgeHub的controller。
由前面的分析可知,EdgeHub的controller作為EdgeHub功能的主要載體。其啟動函數囊括EdgeHub絕大部分啟動邏輯。繼續進入controller的啟動函數,具體如下所示。
KubeEdge/edge/pkg/edgehub/controller.go |
//Start will start EdgeHub func?(ehc?*Controller) Start(ctx?*context.Context) { config.InitEdgehubConfig() for { err := ehc.initial(ctx) ... err = ehc.chClient.Init() ... // execute hook func?after connect ehc.pubConnectInfo(true) go ehc.routeToEdge() go ehc.routeToCloud() go ehc.keepalive() // wait the stop singal // stop authinfo?manager/websocket?connection <-ehc.stopChan ehc.chClient.Uninit() // execute hook fun after disconnect ehc.pubConnectInfo(false) // sleep one period of heartbeat, then try to connect cloud hub again time.Sleep(ehc.config.HeartbeatPeriod?* 2) // clean channel clean: for { select { case <-ehc.stopChan: default: break clean } } } } |
從Controller的啟動函數Start()的定義,可以清楚地看到其包含了EdgehubConfig初始化、各種業務go routine的啟動和最后的退出清理。下面逐個深入剖析。
「未完待續……」??