标签:.config json 网络 contain 设备 sed att rip ted
对于每个CNI 插件在执行函数cmdAdd之前的操作是完全一样的,都是从环境变量和标准输入内读取配置。这在http://www.cnblogs.com/YaoDD/p/6410725.html这篇博文里面已经有完整的叙述了。接下来就直接从CmdAdd函数开始分析CNI bridge插件的执行过程。
skel.CmdArgs数据结构如下所示
// CmdArgs captures all the arguments passed in to the plugin // via both env vars and stdin type CmdArgs struct { ContainerID string Netns string IfName string Args string Path string StdinData []byte }
// cni/plugins/main/bridge/bridge.go
1、func cmdAdd(args *skel.CmdArgs) error
NetConf的数据结构如下所示
type NetConf struct { types.NetConf BrName string `json:"bridge"` IsGW bool `json:"isGateway"` IsDefaultGW bool `json:"isDefaultGateway"` ForceAddress bool `json:"forceAddress"` IPMasq bool `json:"ipMasq"` MTU int `json:"mtu"` HairpinMode bool `json:"hairpinMode"` }
// cni/plugins/main/bridge/bridge.go
2、func loadNetConf(bytes []byte) (*NetConf, string, error)
该函数将NetConf的BrName设置为defaultBrName = "cni0",之后再将bytes中的内容unmarshal到NetConf中
// cni/plugins/main/bridge/bridge.go
3、func setupBridge(n *NetConf) (*netlink.Bridge, *current.Interface, error)
// cni/plugins/main/bridge/bridge.go
4、func ensureBridge(brName string, mtu int) (*netlink.Bridge, error)
1、构造br := &netlink.Bridge{
LinkAttrs: netlink.LinkAttrs{
Name: brName,
MTU: mtu,
TxQLen: -1, // means default txqueuelen
}
}
2、调用err := netlink.LinkAdd(br)
// Re-fetch link to read all attributes and if it already existed,
// ensure it‘s really a bridge with similar configuration -->其实只要配置相同即可
3、调动br, err := bridgeByName(brName) -->l, err := netlink.LinkByName(name)找到设备,再反向断言br, ok := l.(*netlink.Bridge)
4、最后调用netlink.LinkSetUp(br)
// cni/plugins/main/bridge/bridge.go
5、func setupVeth(netns ns.NetNS, br *netlink.Bridge, ifName string, mtu int, hairpinMode bool) (*current.Interface, *current.Interface, error)
1、首先在container中,即netns中创建veth pair,并且将host端移动到host netns
调用netns.Do(),在Do中调用hostVeth, containerVeth, err := ip.SetupVeth(ifName, mtu, hostNS),再用hostVeth和containerVeth填充contIface和hostIface的内容
// need to lookup hostVeth again as its index has changed during ns move
2、调用hostVeth, err := netlink.LinkByName(hostIface.Name)在host netns中找到veth end,接着调用netlink.LinkSetMaster(hostVeth, br)将veth连接至网桥。最后,调用netlink.LinkSetHairpin(hostVeth, hairpinMode)设置hairpinmode
// cni/p
标签:.config json 网络 contain 设备 sed att rip ted
原文地址:http://www.cnblogs.com/YaoDD/p/6412836.html