Skip to content

Commit

Permalink
feat: mac address is persistent now
Browse files Browse the repository at this point in the history
Signed-off-by: Armin Schlegel <armin.schlegel@gmx.de>
  • Loading branch information
siredmar committed Dec 21, 2023
1 parent 9fbcf29 commit 741366a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/backend/vxlan/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type vxlanDeviceAttrs struct {
vtepPort int
gbp bool
learning bool
hwAddr net.HardwareAddr
}

type vxlanDevice struct {
Expand All @@ -46,9 +47,13 @@ type vxlanDevice struct {
}

func newVXLANDevice(devAttrs *vxlanDeviceAttrs) (*vxlanDevice, error) {
hardwareAddr, err := mac.NewHardwareAddr()
if err != nil {
return nil, err
var err error
hardwareAddr := devAttrs.hwAddr
if devAttrs.hwAddr == nil {
hardwareAddr, err = mac.NewHardwareAddr()
if err != nil {
return nil, err
}
}

link := &netlink.Vxlan{
Expand Down
15 changes: 15 additions & 0 deletions pkg/backend/vxlan/vxlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup,

var dev, v6Dev *vxlanDevice
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

macStr := be.subnetMgr.GetStoredMacAddress()
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)
}

if config.EnableIPv4 {
devAttrs := vxlanDeviceAttrs{
vni: uint32(cfg.VNI),
Expand All @@ -152,6 +165,7 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup,
vtepPort: cfg.Port,
gbp: cfg.GBP,
learning: cfg.Learning,
hwAddr: hwAddr,
}

dev, err = newVXLANDevice(&devAttrs)
Expand All @@ -170,6 +184,7 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup,
vtepPort: cfg.Port,
gbp: cfg.GBP,
learning: cfg.Learning,
hwAddr: nil,
}
v6Dev, err = newVXLANDevice(&v6DevAttrs)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/subnet/etcd/local_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func newLocalManager(r Registry, prevSubnet ip.IP4Net, prevIPv6Subnet ip.IP6Net,
}
}

func (m *LocalManager) GetStoredMacAddress() string {
return ""
}

func (m *LocalManager) GetNetworkConfig(ctx context.Context) (*subnet.Config, error) {
cfg, err := m.registry.getNetworkConfig(ctx)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions pkg/subnet/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net"
"os"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -65,6 +66,7 @@ type kubeSubnetManager struct {
enableIPv4 bool
enableIPv6 bool
annotations annotations
annotationPrefix string
client clientset.Interface
nodeName string
nodeStore listers.NodeLister
Expand Down Expand Up @@ -160,6 +162,7 @@ func NewSubnetManager(ctx context.Context, apiUrl, kubeconfig, prefix, netConfPa
func newKubeSubnetManager(ctx context.Context, c clientset.Interface, sc *subnet.Config, nodeName, prefix string, useMultiClusterCidr bool) (*kubeSubnetManager, error) {
var err error
var ksm kubeSubnetManager
ksm.annotationPrefix = prefix
ksm.annotations, err = newAnnotations(prefix)
if err != nil {
return nil, err
Expand Down Expand Up @@ -634,3 +637,29 @@ 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() string {
// get mac info from Name func.
node, err := ksm.client.CoreV1().Nodes().Get(context.TODO(), ksm.nodeName, metav1.GetOptions{})
if err != nil {
log.Errorf("Failed to get node for backend data: %v", err)
return ""
}

// node backend data format: `{"VNI":1,"VtepMAC":"12:c6:65:89:b4:e3"}`
// and we will return only mac addr str like 12:c6:65:89:b4:e3
if node != nil && node.Annotations != nil {
log.Infof("List of node(%s) annotations: %#+v", ksm.nodeName, node.Annotations)
backendData, ok := node.Annotations[fmt.Sprintf("%s/backend-data", ksm.annotationPrefix)]
if ok {
macStr := strings.Trim(backendData, "\"}")
macInfoSlice := strings.Split(macStr, ":\"")
if len(macInfoSlice) == 2 {
return macInfoSlice[1]
}
}
}

return ""
}
1 change: 1 addition & 0 deletions pkg/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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() string
Name() string
}

Expand Down

0 comments on commit 741366a

Please # to comment.