首页 > 代码库 > flannel源码分析---backend为vxlan

flannel源码分析---backend为vxlan

// backend/vxlan/vxlan.go

func (be *VXLANBackend) RegisterNetwork(ctx context.Context, network string, config *subnet.Config) (backend.Network, error)

  • 创建cfg变量,它的结构体中包含VIN int, Port int, GBP bool三个字段,其中VNI设置为默认的defaultVNI = 1,并且调用json.Unmarshal(config.Backend, &cfg)解析
  • 创建变量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,

  }

  • 调用dev, err := newVXLANDevice(&devAttrs) ---> 调用netlink库添加vlan设备,并且可以处理设备已经存在的情况
  • 调用subnetAttrs, err := newSubnetAttrs(be.extIface.ExtAddr, dev.MACAddr())
  • 调用lease, err := be.subnetMgr.AcquireLease(ctx, network, subnetAttrs)
  • 创建vxlanNet := ip.IP4Net{

    IP:      lease.Subnet.IP,

    PrefixLen:   config.Network.PrefixLen,

  }

  • 调用err = dev.Configure(vxlanNet)
  • 最后return newNetwork(network, be.subnetMgr, be.extIface, dev, vxlanNet, lease)

 

flannel源码分析---backend为vxlan