Skip to content

Commit

Permalink
Add persistent-mac option to v6 too
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Buil <mbuil@suse.com>
  • Loading branch information
manuelbuil committed Apr 17, 2024
1 parent f0215be commit 53b2ecb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
15 changes: 12 additions & 3 deletions pkg/backend/vxlan/vxlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ 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 {
Expand Down Expand Up @@ -174,6 +174,15 @@ 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("Setup flannel-v6.1 mac address to %s when flannel restarts", macStrv6)
}

if config.EnableIPv6 {
v6DevAttrs := vxlanDeviceAttrs{
vni: uint32(cfg.VNI),
Expand All @@ -184,7 +193,7 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup,
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

0 comments on commit 53b2ecb

Please sign in to comment.