mirror of
https://github.com/rancher/os.git
synced 2025-09-01 14:48:55 +00:00
Added wildcard matching on MAC addresses.
This commit is contained in:
committed by
niusmallnan
parent
66c5f6130a
commit
a6c1645d47
@@ -111,6 +111,11 @@ func findMatch(link netlink.Link, netCfg *NetworkConfig) (InterfaceConfig, bool)
|
||||
}
|
||||
|
||||
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:])
|
||||
if err != nil {
|
||||
log.Errorf("Failed to parse mac %s: %v", netConf.Match[4:], err)
|
||||
|
70
pkg/netconf/netconf_linux_test.go
Normal file
70
pkg/netconf/netconf_linux_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user