(接上篇)
下面对EdgeCore组件进行剖析,因为EdgeCore中的功能组件比较多,共包括devicetwin、edged、edgehub、eventbus、edgemesh、metamanager、servicebus和test共8个功能模块。限于篇幅,本文只对edged的具体逻辑以及edged调用容器运行时进行剖析。
1.edged的具体逻辑剖析
从EdgeCore模块注册函数入手,具体如下所示。
KubeEdge/edge/cmd/EdgeCore/app/server.go |
// registerModules register all the modules started in EdgeCore func registerModules() { ... edged.Register() ... } |
进入registerModules()函数中的edged.Register(),具体如下所示。
KubeEdge/edge/pkg/edged/edged.go |
// Register register edged func Register() { edged, err := newEdged() if err != nil { klog.Errorf("init new edged error, %v", err) return } core.Register(edged) } |
Register()函数中主要做了如下两件事:
1)初始化edged(edged, err := newEdged());
2)注册将已经实例化的edged struct(core.Register(edged))。
下面深入剖析初始化edged过程中具体做了哪些事情。进入newEdged()函数,具体内容如下所示。
KubeEdge/edge/pkg/edged/edged.go |
//newEdged creates new edged object and initialises it func newEdged() (*edged, error) { conf := getConfig() backoff := flowcontrol.NewBackOff(backOffPeriod, MaxContainerBackOff) podManager := podmanager.NewpodManager() policy := images.ImageGCPolicy{ ... } // build new object to match interface recorder := record.NewEventRecorder() ed := &edged{ ... } ... ed.livenessManager = proberesults.NewManager() ... statsProvider := edgeimages.NewStatsProvider() ... //create and start the docker shim running as a grpc server if conf.remoteRuntimeEndpoint == dockerShimEndpoint || conf.remoteRuntimeEndpoint == dockerShimEndpointDeprecated { streamingConfig := &streaming.Config{} dockerClientConfig := &dockershim.ClientConfig{ dockerEndpoint: conf.dockerAddress, ImagePullProgressDeadline: time.Duration(conf.imagePullProgressDeadline) * time.Second, EnableSleep: true, WithTraceDisabled: true, } pluginConfigs := dockershim.NetworkPluginSettings{ ... } ... ds, err := dockershim.NewdockerService(dockerClientConfig, conf.podSandboxImage, streamingConfig, &pluginConfigs, cgroupName, cgroupDriver, dockershimRootDir, redirectContainerStream) if err != nil { return nil, err } klog.Infof("RemoteRuntimeEndpoint: %q, remoteImageEndpoint: %q", conf.remoteRuntimeEndpoint, conf.remoteRuntimeEndpoint) klog.Info("Starting the GRPC server for the docker CRI shim.") server := dockerremote.NewdockerServer(conf.remoteRuntimeEndpoint, ds) if err := server.Start(); err != nil { return nil, err } } ed.clusterDNS = convertStrToIP(conf.clusterDNS) ed.dnsConfigurer = kubedns.NewConfigurer(recorder, nodeRef, ed.nodeIP, ed.clusterDNS, conf.clusterDomain, ResolvConfDefault) containerRefManager := kubecontainer.NewRefManager() httpClient := &http.Client{} runtimeService, imageService, err := getRuntimeAndImageServices(conf.remoteRuntimeEndpoint, conf.remoteRuntimeEndpoint, conf.RuntimeRequestTimeout) if err != nil { return nil, err } if ed.os == nil { ed.os = kubecontainer.RealOS{} } ed.clcm, err = clcm.NewContainerLifecycleManager(DefaultRootDir) var machineInfo cadvisorapi.MachineInfo machineInfo.MemoryCapacity = uint64(conf.memoryCapacity) containerRuntime, err := kuberuntime.NewKubeGenericRuntimeManager( ... ) cadvisorInterface, err := cadvisor.New("") containerManager, err := cm.NewContainerManager(mount.New(""), cadvisorInterface, cm.NodeConfig{ ... }, false, conf.devicePluginEnabled, recorder) ed.containerRuntime = containerRuntime ed.containerRuntimeName = RemoteContainerRuntime ed.containerManager = containerManager ed.runtimeService = runtimeService imageGCManager, err := images.NewImageGCManager(ed.containerRuntime, statsProvider, recorder, nodeRef, policy, conf.podSandboxImage) ... ed.imageGCManager = imageGCManager containerGCManager, err := kubecontainer.NewContainerGC(containerRuntime, containerGCPolicy, &containers.KubeSourcesReady{}) ... ed.containerGCManager = containerGCManager ed.server = server.NewServer(ed.podManager) ed.volumePluginMgr, err = NewInitializedVolumePluginMgr(ed, ProbeVolumePlugins("")) ... return ed, nil } |
从newEdged()函数的定义中,我们可以知道其做很多事情,具体如下。
1)获取edged相关配置(conf := getConfig());
2)初始化podmanager(podManager := podmanager.NewpodManager());
3)初始化edged struct(ed := &edged{});
4)初始化 edged的livenessManager;
5)初始化edged的镜像存放地;
6)创建并启动dockershim的grpc server;
7) 初始化运行时服务和镜像服务;
8)初始化通用容器运行时服务;
9)初始化镜像垃圾回收管理器;
10)初始化容器垃圾回收器;
11)初始化edged的server;
12)初始化edged的volume plugin管理器。
针对以上动作,笔者重点分析创建并启动dockershim的grpc server。dockershim是edged与容器运行时交互的管道,所以edged对容器操作在dockershim的方法中都会得到体现。dockershim服务的初始化函数定义具体如下所示。
「未完待续……」