1
0
mirror of https://github.com/rancher/os.git synced 2025-08-31 06:11:12 +00:00

Added wildcard matching on MAC addresses.

This commit is contained in:
Morten Møller Riis
2019-03-12 08:57:34 +01:00
committed by niusmallnan
parent 66c5f6130a
commit a6c1645d47
2 changed files with 75 additions and 0 deletions

View File

@@ -111,6 +111,11 @@ func findMatch(link netlink.Link, netCfg *NetworkConfig) (InterfaceConfig, bool)
} }
if strings.HasPrefix(netConf.Match, "mac") { if strings.HasPrefix(netConf.Match, "mac") {
if strings.Contains(netConf.Match, "*") {
// If selector contains wildcard * and MAC address matches wildcard then return
return netConf, glob.Glob(netConf.Match[4:], link.Attrs().HardwareAddr.String())
}
haAddr, err := net.ParseMAC(netConf.Match[4:]) haAddr, err := net.ParseMAC(netConf.Match[4:])
if err != nil { if err != nil {
log.Errorf("Failed to parse mac %s: %v", netConf.Match[4:], err) log.Errorf("Failed to parse mac %s: %v", netConf.Match[4:], err)

View File

@@ -0,0 +1,70 @@
package netconf
import (
"net"
"testing"
"github.com/vishvananda/netlink"
)
type mockLink struct {
attrs netlink.LinkAttrs
}
func (l mockLink) Attrs() *netlink.LinkAttrs {
return &l.attrs
}
func (l mockLink) Type() string {
return "fake"
}
func TestFindMatch(t *testing.T) {
testCases := []struct {
match string
mac string
expected bool
}{
{
"mac:aa:bb:cc:dd:ee:ff",
"aa:bb:cc:dd:ee:ff",
true,
},
{
"mac:aa:bb:cc:*",
"aa:bb:cc:12:34:56",
true,
},
{
"mac:aa:bb:cc:*",
"11:bb:cc:dd:ee:ff",
false,
},
{
"mac:aa:bb:cc:dd:ee:ff",
"aa:bb:cc:dd:ee:11",
false,
},
}
for i, tt := range testCases {
netCfg := NetworkConfig{
Interfaces: map[string]InterfaceConfig{
"eth0": InterfaceConfig{
Match: tt.match,
},
},
}
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = "eth0"
linkAttrs.HardwareAddr, _ = net.ParseMAC(tt.mac)
link := mockLink{attrs: linkAttrs}
_, match := findMatch(link, &netCfg)
if match != tt.expected {
t.Errorf("Test case %d failed: mac: '%s' match '%s' expected: '%v' got: '%v'", i, tt.mac, tt.match, tt.expected, match)
}
}
}