cri-o 与 cni的集成分析
Posted Monster-Z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cri-o 与 cni的集成分析相关的知识,希望对你有一定的参考价值。
1、// cri-o/server/sandbox.go
func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest) (*pb.RunPodSandboxResponse, error)
在该函数中先后调用了:
(1)、container, err := oci.NewContainer(containerID, containerName, podSandboxDir, podSandboxDir, labels, nil, id, false)
sb.infraContainer = container
s.runtime.CreateContainer(container)
s.runtime.UpdateStatus(containers) 创建infra容器
(2)、再调用 podNamespace := "" 和 netnsPath, err := container.NetNsPath(),该函数的作用只是返回路径/proc/infra-container-id/ns/net
(3)、调用s.netPlugin.SetUpPod(netnsPath, podNamespace, id, containerName),为容器创建network
-------------------------------------------------------------------------- cni 初始化 --------------------------------------------------------------------------------------------------
2、// cri-o/vendor/src/github.com/rajatchopra/ocicni.go
server的netPlugin字段初始化为:netPlugin, err := ocicni.InitCNI("")
func InitCNI(pluginDir string) (CNIPlugin, error)
(1)、首先调用plugin := probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, "")和plugin.nsenterPath, err = exec.LookPath(”nsenter“)
(2)、检查默认的network是否存在,如果不存在则停止CNI的查找,直接返回一个noop plugin,调用_, err = getDefaultCNINetwork(plugin.pluginDir, plugin.vendorCNIDirPrefix)
(3)、当有默认的network存在时,周期性地从pluginDir中读取网络配置的更新。即生成一个goroutine,每隔10s调用一次plugin.syncNetworkConfig()
3、// cri-o/vendor/src/github.com/rajatchopra/ocicni.go
func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix string) (*cniNetworkPlugin)
配置获得 plugin := &cniNetworkPlugin {
defaultNetwork: nil,
loNetwork: getLoNetwork(vendorCNIDirPrefix),
pluginDir: pluginDir,
vendorCNIDirPrefix: vendorCNIDirPrefix,
}
最后调用plugin.syncNetworkConfig()并返回 return plugin,其中syncNetworkConfig首先调用network, err := getDefaultCNINetwork(plugin.pluginDir, plugin.vendorCNIDirPrefix),然后调用plugin.setDefaultNetwork(network)设置为plugin.defaultNetwork为network
4、//cri-o/vendor/src/github.com/rajachopra/ocicni.go
(1)、手动添加loConfig, err := libcni.ConfFromBytes([]byte(`{"cniVersion": "0.1.0", "name": "cni-loopback", "type": "loopback"}`))
(2)、调用cninet := &libcni.CNIConfig{Path: []string{vendorCNIDir(vendorDirPrefix, loConfig.Network.Type), DefaultCNIDir}}
并返回 reutrn loNetwork := &cniNetwork{name: "lo", NetworkConfig: loConfig, CNIConfig: cninet}
5、//cri-o/vendor/src/github.com/rajatchopra/ocicni.go
func getDefaultCNINetwork(pluginDir, vendorCNIDirPrefix string) (*cniNetwork, error)
(1)、当pluginDir为空时,将pluginDir设置为DefaultNetDir,为/etc/cni/net.d
(2)、调用files, err := libcni.ConfFiles(pluginDir),加载配置文件
(3)、若files不为空,则调用for循环,for _, confFile := range files
对于confFile,先调用conf, err := libcni.ConfFromFile(confFile)
再调用vendorDir := vendorCNIDir(vendorCNIDirPrefix, conf.Network.Type),cninet := &libcni.CNIConfig{Path: []string{DefaultCNIDir, vendorDir }}
其中vendorDir为"/opt/pluginType/bin"
最后,返回 return network := &cniNetwork{name: conf.Network.Name, NetworkConfig: conf, CNIConfig: cninet}
------------------------------------------------------------------------------ 设置Pod的network ---------------------------------------------------------------------------------------------------
6、//cni-o/vendor/src/github.com/rajatchopra/ocicni.go
func (plugin *cniNetworkPlugin) SetUpPod(netnsPath string, namespace string, name string, id string) error
(1)、调用 plugin.checkInitialized(),判断plugin.defaultNetwork是否为空,若为空,返回错误
(2)、分别调用plugin.loNetwork.addToNetwork(name, namespace, id, netnsPath)和plugin.getDefaultNetwork().addToNetwork(name, namespace, id, netnsPath)
7、//cni-o/vendor/src/github.com/rajatchopra/ocicni.go
func (network *cniNetwork) addToNetwork(podName string, podNamespace string, podInfraContainerID string, podNetnsPath string) (*cnitypes.Result, error)
(1)、调用rt, err := buildCNIRuntimeConf(podName, podNamespace, podInfaraContainerID, podNetnsPath)
(2)、再调用netconf, cninet := network.NetworkConfig, network.CNIConfig,最后调用res, err := cninet.AddNetwork(netconf, rt)
8、//cni-o/vendor/src/github.com/rajatchopra/ocicni.go
func buildCNIRuntimeConf(podName string, podNs string, podInfraContainerID string, podNetnsPath string) (*libcni.RuntimeConf, error)
该函数只是简单地填充libcni.RuntimeConf并返回
rt := &libcni.RuntimeConf {
ContainerID: podInfraContainerID,
NetNS: podNetnsPath,
IfName: DefaultInterfaceName,
Args: [][2]string {
{"IgnoreUnknown", "1"},
{"K8S_POD_NAMESPACE", podNs},
{"K8S_POD_NAME", podName},
{"K8S_POD_INFRA_CONTAINER_ID", podInfraContainerID},
}
}
以上是关于cri-o 与 cni的集成分析的主要内容,如果未能解决你的问题,请参考以下文章