Skip to content

Commit 7ccfe51

Browse files
authored
Merge pull request #3592 from balopat/fix_netstat
fix netstat -f error on linux distros
2 parents dbd4711 + f3034d3 commit 7ccfe51

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

Diff for: pkg/minikube/tunnel/route_linux.go

+23-21
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (router *osRouter) EnsureRouteIsAdded(route *Route) error {
5454
}
5555

5656
func (router *osRouter) Inspect(route *Route) (exists bool, conflict string, overlaps []string, err error) {
57-
cmd := exec.Command("netstat", "-nr", "-f", "inet")
57+
cmd := exec.Command("ip", "r")
5858
cmd.Env = append(cmd.Env, "LC_ALL=C")
5959
stdInAndOut, err := cmd.CombinedOutput()
6060
if err != nil {
@@ -70,37 +70,39 @@ func (router *osRouter) Inspect(route *Route) (exists bool, conflict string, ove
7070

7171
func (router *osRouter) parseTable(table []byte) routingTable {
7272
t := routingTable{}
73-
skip := true
7473
for _, line := range strings.Split(string(table), "\n") {
75-
//after first line of header we can start consuming
76-
if strings.HasPrefix(line, "Destination") {
77-
skip = false
78-
continue
79-
}
8074

8175
fields := strings.Fields(line)
82-
//don't care about the 0.0.0.0 routes
83-
if skip || len(fields) == 0 || len(fields) > 0 && (fields[0] == "default" || fields[0] == "0.0.0.0") {
76+
77+
//don't care about the routes that 0.0.0.0
78+
if len(fields) == 0 ||
79+
len(fields) > 0 && (fields[0] == "default" || fields[0] == "0.0.0.0") {
8480
continue
8581
}
82+
8683
if len(fields) > 2 {
87-
dstCIDRIP := net.ParseIP(fields[0])
88-
dstCIDRMask := fields[2]
89-
dstMaskIP := net.ParseIP(dstCIDRMask)
90-
gatewayIP := net.ParseIP(fields[1])
9184

92-
if dstCIDRIP == nil || gatewayIP == nil || dstMaskIP == nil {
93-
glog.V(8).Infof("skipping line: can't parse: %s", line)
94-
} else {
85+
//assuming "10.96.0.0/12 via 192.168.39.47 dev virbr1"
86+
dstCIDRString := fields[0]
87+
gatewayIPString := fields[2]
88+
gatewayIP := net.ParseIP(gatewayIPString)
9589

96-
dstCIDR := &net.IPNet{
97-
IP: dstCIDRIP,
98-
Mask: net.IPv4Mask(dstMaskIP[12], dstMaskIP[13], dstMaskIP[14], dstMaskIP[15]),
99-
}
90+
//if not via format, then gateway is assumed to be 0.0.0.0
91+
// "1.2.3.0/24 dev eno1 proto kernel scope link src 1.2.3.54 metric 100"
92+
if fields[1] != "via" {
93+
gatewayIP = net.ParseIP("0.0.0.0")
94+
}
95+
96+
_, ipNet, err := net.ParseCIDR(dstCIDRString)
97+
if err != nil {
98+
glog.V(4).Infof("skipping line: can't parse CIDR from routing table: %s", dstCIDRString)
99+
} else if gatewayIP == nil {
100+
glog.V(4).Infof("skipping line: can't parse IP from routing table: %s", gatewayIPString)
101+
} else {
100102

101103
tableLine := routingTableLine{
102104
route: &Route{
103-
DestCIDR: dstCIDR,
105+
DestCIDR: ipNet,
104106
Gateway: gatewayIP,
105107
},
106108
line: line,

Diff for: pkg/minikube/tunnel/route_linux_test.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,30 @@ func TestLinuxRouteCleanupIdempontentIntegrationTest(t *testing.T) {
9090

9191
func TestParseTable(t *testing.T) {
9292

93-
const table = `Kernel IP routing table
94-
Destination Gateway Genmask Flags MSS Window irtt Iface
95-
0.0.0.0 172.31.126.254 0.0.0.0 UG 0 0 0 eno1
96-
10.96.0.0 127.0.0.1 255.240.0.0 UG 0 0 0 eno1
97-
172.31.126.0 0.0.0.0 255.255.255.0 U 0 0 0 eno1
98-
`
93+
const table = `default via 172.31.126.254 dev eno1 proto dhcp metric 100
94+
10.96.0.0/12 via 192.168.39.47 dev virbr1
95+
10.110.0.0/16 via 127.0.0.1 dev lo
96+
172.31.126.0/24 dev eno1 proto kernel scope link src 172.31.126.54 metric 100
97+
192.168.9.0/24 dev docker0 proto kernel scope link src 192.168.9.1`
9998

10099
rt := (&osRouter{}).parseTable([]byte(table))
101100

102101
expectedRt := routingTable{
103102
routingTableLine{
104-
route: unsafeParseRoute("127.0.0.1", "10.96.0.0/12"),
105-
line: "10.96.0.0 127.0.0.1 255.240.0.0 UG 0 0 0 eno1",
103+
route: unsafeParseRoute("192.168.39.47", "10.96.0.0/12"),
104+
line: "10.96.0.0/12 via 192.168.39.47 dev virbr1",
105+
},
106+
routingTableLine{
107+
route: unsafeParseRoute("127.0.0.1", "10.110.0.0/16"),
108+
line: "10.110.0.0/16 via 127.0.0.1 dev lo",
106109
},
107110
routingTableLine{
108111
route: unsafeParseRoute("0.0.0.0", "172.31.126.0/24"),
109-
line: "172.31.126.0 0.0.0.0 255.255.255.0 U 0 0 0 eno1",
112+
line: "172.31.126.0/24 dev eno1 proto kernel scope link src 172.31.126.54 metric 100",
113+
},
114+
routingTableLine{
115+
route: unsafeParseRoute("0.0.0.0", "192.168.9.0/24"),
116+
line: "192.168.9.0/24 dev docker0 proto kernel scope link src 192.168.9.1",
110117
},
111118
}
112119
if !expectedRt.Equal(&rt) {

0 commit comments

Comments
 (0)