Skip to content

Commit 8cf468b

Browse files
authored
Merge pull request #248 from coroot/fix_net_interface_filter
fix net interface name filtering
2 parents f331454 + 8813110 commit 8cf468b

2 files changed

Lines changed: 22 additions & 26 deletions

File tree

node/net.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package node
22

33
import (
44
"regexp"
5+
"strings"
56

67
"github.com/coroot/coroot-node-agent/proc"
78
"github.com/vishvananda/netlink"
89
"golang.org/x/sys/unix"
910
"inet.af/netaddr"
1011
)
1112

12-
var netDeviceFilterRe = regexp.MustCompile(`^(en(P\d+)?p\d+s\d+(f\d+)?|eth\d+|eno\d+|ens\d+|em\d+|bond\d+|p\d+p\d+|enx[0-9a-f]{12})`)
13+
var netDeviceFilterRe = regexp.MustCompile(`^(en(p\d+)?p\d+s\d+(f\d+)?|eth\d+|eno\d+|ens\d+|em\d+|bond\d+|p\d+p\d+|enx[0-9a-f]+)`)
1314

1415
func netDeviceFilter(name string) bool {
15-
return netDeviceFilterRe.MatchString(name)
16+
return netDeviceFilterRe.MatchString(strings.ToLower(name))
1617
}
1718

1819
type NetDeviceInfo struct {

node/net_test.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,24 @@ import (
77
)
88

99
func TestNetDeviceFilter(t *testing.T) {
10-
cases := map[string]bool{
11-
"eth0": true,
12-
"eth0@if699": true,
13-
"enp2s0": true,
14-
"bond0": true,
15-
"ens1": true,
16-
"p1p1": true,
17-
"eno2": true,
18-
"em1": true,
19-
"enx78e7d1ea46da": true,
20-
"enP4p65s0": true,
21-
"enP2p33s0": true,
10+
assert.True(t, netDeviceFilter("eth0"))
11+
assert.True(t, netDeviceFilter("eth0@if699"))
12+
assert.True(t, netDeviceFilter("enp2s0"))
13+
assert.True(t, netDeviceFilter("bond0"))
14+
assert.True(t, netDeviceFilter("ens1"))
15+
assert.True(t, netDeviceFilter("p1p1"))
16+
assert.True(t, netDeviceFilter("eno2"))
17+
assert.True(t, netDeviceFilter("em1"))
18+
assert.True(t, netDeviceFilter("enx78e7d1ea46da"))
19+
assert.True(t, netDeviceFilter("enP4p65s0"))
20+
assert.True(t, netDeviceFilter("enP2p33s0"))
21+
assert.True(t, netDeviceFilter("enX0"))
2222

23-
"dummy0": false,
24-
"docker0": false,
25-
"kube-ipvs0": false,
26-
"veth1b0c947@if2": false,
27-
"flannel.1": false,
28-
"cni0": false,
29-
"lxc00aa@if698": false,
30-
}
31-
32-
for name, ok := range cases {
33-
assert.Equal(t, ok, netDeviceFilter(name), name)
34-
}
23+
assert.False(t, netDeviceFilter("dummy0"))
24+
assert.False(t, netDeviceFilter("docker0"))
25+
assert.False(t, netDeviceFilter("kube-ipvs0"))
26+
assert.False(t, netDeviceFilter("veth1b0c947@if2"))
27+
assert.False(t, netDeviceFilter("flannel.1"))
28+
assert.False(t, netDeviceFilter("cni0"))
29+
assert.False(t, netDeviceFilter("lxc00aa@if698"))
3530
}

0 commit comments

Comments
 (0)