main函数中RegisterNetwork会根据不同的后端类型调用不同的RegisterNetwork,这里以vxlan为例:
-
定义cfg变量匿名结构体,并使用Unmarshal解析配置文件初始化cfg,结构体如下:
struct { VNI int Port int GBP bool Learning bool DirectRouting bool }
-
初始化vxlanDeviceAttrs结构体变量devAttrs
vxlanDeviceAttrs{ vni: uint32(cfg.VNI), name: fmt.Sprintf("flannel.%v", cfg.VNI), vtepIndex: be.extIface.Iface.Index, vtepAddr: be.extIface.IfaceAddr, vtepPort: cfg.Port, gbp: cfg.GBP, learning: cfg.Learning, }
-
使用newVXLANDevice新建vxlan设备,devAttrs作为参数,完成设备的初始化,vxlan设备结构体如下:
type vxlanDevice struct { link *netlink.Vxlan directRouting bool } type Vxlan struct { LinkAttrs VxlanId int VtepDevIndex int SrcAddr net.IP Group net.IP TTL int TOS int Learning bool Proxy bool RSC bool L2miss bool L3miss bool UDPCSum bool NoAge bool GBP bool Age int Limit int Port int PortLow int PortHigh int } type LinkAttrs struct { Index int MTU int TxQLen int // Transmit Queue Length Name string HardwareAddr net.HardwareAddr Flags net.Flags RawFlags uint32 ParentIndex int // index of the parent link device MasterIndex int // must be the index of a bridge Namespace interface{} // nil | NsPid | NsFd Alias string Statistics *LinkStatistics Promisc int Xdp *LinkXdp EncapType string Protinfo *Protinfo OperState LinkOperState }
-
newSubnetAttrs新建并初始化subnet.LeaseAttrs属性
-
AcquireLease分配子网租约
-
newNetwork创建网络