(接上篇)
edgecontroller剖析
edgecontroller功能模块启动函数的具体内容如下所示。
KubeEdge/cloud/pkg/edgecontroller/controller.go |
// Start controller func (ctl *Controller) Start(c *beehiveContext.Context) { var ctx context.Context config.Context = c ctx, ctl.cancel = context.WithCancel(context.Background()) initConfig() upstream, err := controller.NewUpstreamController() if err != nil { klog.Errorf("new upstream controller failed with error: %s", err) os.Exit(1) } upstream.Start(ctx) downstream, err := controller.NewDownstreamController() if err != nil { klog.Warningf("new downstream controller failed with error: %s", err) os.Exit(1) } downstream.Start(ctx) } |
从以上edgecontroller的启动函数Start()定义中,可以清晰地看出cloudhub在启动时主要做了如下几件事。
1)接收beehiveContext.Context的通信框架实例,并保存(config.Context = c);
2)初始化edgecontroller的配置(initHubConfig());
3) 实例化并启动UpstreamController;
4)实例化并启动DownstreamController。
下面深入分析UpstreamController和DownstreamController都具体做了哪些事。
UpstreamController
顺着UpstreamController的实例化函数找到UpstreamController struct定义,具体如下所示。
KubeEdge/cloud/pkg/edgecontroller/upstream.go |
// UpstreamController subscribe messages from edge and sync to k8s api server type UpstreamController struct { kubeClient *Kubernetes.Clientset messageLayer messagelayer.MessageLayer // message channel nodeStatusChan chan model.Message podStatusChan chan model.Message secretChan chan model.Message configMapChan chan model.Message serviceChan chan model.Message endpointsChan chan model.Message persistentVolumeChan chan model.Message persistentVolumeClaimChan chan model.Message volumeAttachmentChan chan model.Message queryNodeChan chan model.Message updateNodeChan chan model.Message } |
至此,读者可能疑惑UpstreamController是不是负责处理edge节点上报的nodeStatus、podStatus、secret、configMap、service、endpoints、persistentVolume、persistentVolumeClaim、volumeAttachment等资源的信息。恭喜你猜对了,UpstreamController的作用就在于此。
DownstreamController
顺着DownstreamController的实例化函数找到DownstreamController struct定义,具体如下所示。
KubeEdge/cloud/pkg/edgecontroller/downstream.go |
// DownstreamController watch Kubernetes api server and send change to edge type DownstreamController struct { kubeClient *Kubernetes.Clientset messageLayer messagelayer.MessageLayer podManager *manager.podManager configmapManager *manager.ConfigMapManager secretManager *manager.SecretManager nodeManager *manager.NodesManager serviceManager *manager.ServiceManager endpointsManager *manager.EndpointsManager lc *manager.LocationCache } |
DownstreamController的功能是监听cloud端pod、configmap、secret、node、service和endpoints等资源的事件,并下发到edge节点。
devicecontroller剖析
devicecontroller功能模块启动函数的具体内容如下所示。
KubeEdge/cloud/pkg/devicecontroller/module.go |
// Start controller func (dctl *DeviceController) Start(c *beehiveContext.Context) { var ctx context.Context config.Context = c ctx, dctl.cancel = context.WithCancel(context.Background()) initConfig() downstream, err := controller.NewDownstreamController() if err != nil { klog.Errorf("New downstream controller failed with error: %s", err) os.Exit(1) } upstream, err := controller.NewUpstreamController(downstream) if err != nil { klog.Errorf("new upstream controller failed with error: %s", err) os.Exit(1) } downstream.Start(ctx) // wait for downstream controller to start and load deviceModels and devices // TODO think about sync time.Sleep(1 * time.Second) upstream.Start(ctx) } |
devicecontroller的启动函数和edgecontroller的启动函数的逻辑基本相同,所以对于devicecontroller的剖析,读者可以参考edgecontrller剖析。
到此,KubeEdge源码分析系列之CloudCore就全部结束了。大家在阅读KubeEdge的源码时,一定要时刻提醒自己CloudCore中的模块之间时可以通过beehive的context消息通信框架相互通信。
「未完待续……」