llhuii / dive-into-k8s

Apache License 2.0
0 stars 0 forks source link

cni学习 #18

Open llhuii opened 2 years ago

llhuii commented 2 years ago

配置文件加载顺序

https://code.k8s.io/pkg/kubelet/dockershim/network/cni/cni.go func getDefaultCNINetwork

foreach从/etc/cni/net.d/ 加载".conf", ".conflist", ".json" 后缀的文件,按名字排序,选择满足以下条件的第一个:

  1. 配置格式没有问题
  2. binary存在/opt/cni/bin
  3. 依赖的binary存在/opt/cni/bin

conf或者json格式:

// NetConf describes a network.
type NetConf struct {
    CNIVersion string `json:"cniVersion,omitempty"`

    Name         string          `json:"name,omitempty"`
    Type         string          `json:"type,omitempty"`
    Capabilities map[string]bool `json:"capabilities,omitempty"`
    IPAM         IPAM            `json:"ipam,omitempty"`
    DNS          DNS             `json:"dns"`

    RawPrevResult map[string]interface{} `json:"prevResult,omitempty"`
}

conflist格式, 其中plugins元素是NetConf格式:

// NetConfList describes an ordered list of networks.
type NetConfList struct {
    CNIVersion string `json:"cniVersion,omitempty"`

    Name         string     `json:"name,omitempty"`
    DisableCheck bool       `json:"disableCheck,omitempty"`
    Plugins      []*NetConf `json:"plugins,omitempty"`
}

例子:

{
   "cniVersion": "0.3.0",
   "name": "terway",
   "type": "terway",
   "eniip_virtual_type": "Veth"
}
{
    "name": "asi_alinet",
    "cniVersion": "0.2.0",
    "plugins": [
        {
            "cniServiceAddress": "https://meta-cni.alibaba-inc.com:6643",
            "type": "asi_alinet"
        },
        {
            "ServiceItem": [
                {
                    "type": "DirectAccess",
                    "virServiceCIDR": "169.254.20.10/32"
                }
            ],
            "capabilities": {
                "annotations": true
            },
            "cniVersion": "0.3.0",
            "ipam": {
                "subnet": "169.254.240.0/20",
                "type": "lite-linker-host-local"
            },
            "type": "lite-linker"
        }
    ]
}