Browse Source

Merge pull request #48 from coroot/net_device_filter

node: do not ignore `em*`, `bond*`, `p*p*`, and `enx*` network devices
Nikolay Sivko 2 years ago
parent
commit
ade6728403
2 changed files with 38 additions and 3 deletions
  1. 6 3
      node/net.go
  2. 32 0
      node/net_test.go

+ 6 - 3
node/net.go

@@ -8,7 +8,11 @@ import (
 	"regexp"
 )
 
-var includeNetDev = regexp.MustCompile(`^(enp\d+s\d+(f\d+)?|eth\d+|eno\d+|ens\d+)`)
+var netDeviceFilterRe = regexp.MustCompile(`^(enp\d+s\d+(f\d+)?|eth\d+|eno\d+|ens\d+|em\d+|bond\d+|p\d+p\d+|enx[0-9a-f]{12})`)
+
+func netDeviceFilter(name string) bool {
+	return netDeviceFilterRe.MatchString(name)
+}
 
 type NetDeviceInfo struct {
 	Name       string
@@ -38,7 +42,7 @@ func NetDevices() ([]NetDeviceInfo, error) {
 	var res []NetDeviceInfo
 	for _, link := range links {
 		attrs := link.Attrs()
-		if !includeNetDev.MatchString(attrs.Name) {
+		if !netDeviceFilter(attrs.Name) {
 			continue
 		}
 		info := NetDeviceInfo{
@@ -68,5 +72,4 @@ func NetDevices() ([]NetDeviceInfo, error) {
 		res = append(res, info)
 	}
 	return res, nil
-
 }

+ 32 - 0
node/net_test.go

@@ -0,0 +1,32 @@
+package node
+
+import (
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestNetDeviceFilter(t *testing.T) {
+	cases := map[string]bool{
+		"eth0":            true,
+		"eth0@if699":      true,
+		"enp2s0":          true,
+		"bond0":           true,
+		"ens1":            true,
+		"p1p1":            true,
+		"eno2":            true,
+		"em1":             true,
+		"enx78e7d1ea46da": true,
+
+		"dummy0":          false,
+		"docker0":         false,
+		"kube-ipvs0":      false,
+		"veth1b0c947@if2": false,
+		"flannel.1":       false,
+		"cni0":            false,
+		"lxc00aa@if698":   false,
+	}
+
+	for name, ok := range cases {
+		assert.Equal(t, ok, netDeviceFilter(name), name)
+	}
+}