Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add persistent-mac option to v6 #1946

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions pkg/backend/vxlan/vxlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,21 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup,
var err error

// When flannel is restarted, it will get the MAC address from the node annotations to set flannel.1 MAC address
var hwAddr net.HardwareAddr
var hwAddr, hwAddrv6 net.HardwareAddr

macStr := be.subnetMgr.GetStoredMacAddress(ctx)
macStr, macStrv6 := be.subnetMgr.GetStoredMacAddresses(ctx)
if macStr != "" {
hwAddr, err = net.ParseMAC(macStr)
if err != nil {
log.Errorf("Failed to parse mac addr(%s): %v", macStr, err)
}
log.Infof("Setup flannel.1 mac address to %s when flannel restarts", macStr)
log.Infof("Interface flannel.%d mac address set to: %s", cfg.VNI, macStr)
}

if config.EnableIPv4 {
devAttrs := vxlanDeviceAttrs{
vni: uint32(cfg.VNI),
name: fmt.Sprintf("flannel.%v", cfg.VNI),
name: fmt.Sprintf("flannel.%d", cfg.VNI),
MTU: cfg.MTU,
vtepIndex: be.extIface.Iface.Index,
vtepAddr: be.extIface.IfaceAddr,
Expand All @@ -174,17 +174,26 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup,
}
dev.directRouting = cfg.DirectRouting
}

if macStrv6 != "" {
hwAddrv6, err = net.ParseMAC(macStrv6)
if err != nil {
log.Errorf("Failed to parse mac addr(%s): %v", macStrv6, err)
}
log.Infof("Interface flannel-v6.%d mac address set to: %s", cfg.VNI, macStrv6)
}

if config.EnableIPv6 {
v6DevAttrs := vxlanDeviceAttrs{
vni: uint32(cfg.VNI),
name: fmt.Sprintf("flannel-v6.%v", cfg.VNI),
name: fmt.Sprintf("flannel-v6.%d", cfg.VNI),
MTU: cfg.MTU,
vtepIndex: be.extIface.Iface.Index,
vtepAddr: be.extIface.IfaceV6Addr,
vtepPort: cfg.Port,
gbp: cfg.GBP,
learning: cfg.Learning,
hwAddr: nil,
hwAddr: hwAddrv6,
}
v6Dev, err = newVXLANDevice(&v6DevAttrs)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/subnet/etcd/local_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func newLocalManager(r Registry, prevSubnet ip.IP4Net, prevIPv6Subnet ip.IP6Net,
}
}

func (m *LocalManager) GetStoredMacAddress(ctx context.Context) string {
return ""
func (m *LocalManager) GetStoredMacAddresses(ctx context.Context) (string, string) {
return "", ""
}

func (m *LocalManager) GetNetworkConfig(ctx context.Context) (*subnet.Config, error) {
Expand Down
20 changes: 15 additions & 5 deletions pkg/subnet/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,14 @@ func (m *kubeSubnetManager) HandleSubnetFile(path string, config *subnet.Config,
return subnet.WriteSubnetFile(path, config, ipMasq, sn, ipv6sn, mtu)
}

// GetStoredMacAddress reads MAC address from node annotations when flannel restarts
func (ksm *kubeSubnetManager) GetStoredMacAddress(ctx context.Context) string {
// GetStoredMacAddresses reads MAC addresses from node annotations when flannel restarts
func (ksm *kubeSubnetManager) GetStoredMacAddresses(ctx context.Context) (string, string) {
var macv4, macv6 string
// get mac info from Name func.
node, err := ksm.client.CoreV1().Nodes().Get(ctx, ksm.nodeName, metav1.GetOptions{})
if err != nil {
log.Errorf("Failed to get node for backend data: %v", err)
return ""
return "", ""
}

// node backend data format: `{"VNI":1,"VtepMAC":"12:c6:65:89:b4:e3"}`
Expand All @@ -624,10 +625,19 @@ func (ksm *kubeSubnetManager) GetStoredMacAddress(ctx context.Context) string {
macStr := strings.Trim(backendData, "\"}")
macInfoSlice := strings.Split(macStr, ":\"")
if len(macInfoSlice) == 2 {
return macInfoSlice[1]
macv4 = macInfoSlice[1]
}
}
backendDatav6, okv6 := node.Annotations[fmt.Sprintf("%s/backend-v6-data", ksm.annotationPrefix)]
if okv6 {
macStr := strings.Trim(backendDatav6, "\"}")
macInfoSlice := strings.Split(macStr, ":\"")
if len(macInfoSlice) == 2 {
macv6 = macInfoSlice[1]
}
}
return macv4, macv6
}

return ""
return "", ""
}
2 changes: 1 addition & 1 deletion pkg/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type Manager interface {
WatchLeases(ctx context.Context, receiver chan []lease.LeaseWatchResult) error
CompleteLease(ctx context.Context, lease *lease.Lease, wg *sync.WaitGroup) error

GetStoredMacAddress(ctx context.Context) string
GetStoredMacAddresses(ctx context.Context) (string, string)
Name() string
}

Expand Down
Loading