From 0fb27c7f384b5fcb9a3ad0877bda9050d4fc3bc1 Mon Sep 17 00:00:00 2001 From: balopat Date: Fri, 25 Jan 2019 16:15:27 -0800 Subject: [PATCH 1/3] fix netstat -f error on linux distros --- pkg/minikube/tunnel/route_linux.go | 2 +- pkg/minikube/tunnel/route_linux_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/minikube/tunnel/route_linux.go b/pkg/minikube/tunnel/route_linux.go index 766218b62c..6e433136b5 100644 --- a/pkg/minikube/tunnel/route_linux.go +++ b/pkg/minikube/tunnel/route_linux.go @@ -54,7 +54,7 @@ func (router *osRouter) EnsureRouteIsAdded(route *Route) error { } func (router *osRouter) Inspect(route *Route) (exists bool, conflict string, overlaps []string, err error) { - cmd := exec.Command("netstat", "-nr", "-f", "inet") + cmd := exec.Command("route", "-n") cmd.Env = append(cmd.Env, "LC_ALL=C") stdInAndOut, err := cmd.CombinedOutput() if err != nil { diff --git a/pkg/minikube/tunnel/route_linux_test.go b/pkg/minikube/tunnel/route_linux_test.go index 4f039da088..8e1f059ea2 100644 --- a/pkg/minikube/tunnel/route_linux_test.go +++ b/pkg/minikube/tunnel/route_linux_test.go @@ -91,8 +91,8 @@ func TestLinuxRouteCleanupIdempontentIntegrationTest(t *testing.T) { func TestParseTable(t *testing.T) { const table = `Kernel IP routing table -Destination Gateway Genmask Flags MSS Window irtt Iface -0.0.0.0 172.31.126.254 0.0.0.0 UG 0 0 0 eno1 +Destination Gateway Genmask Flags Metric Ref Use Iface +0.0.0.0 172.31.126.254 0.0.0.0 UG 100 0 0 eno1 10.96.0.0 127.0.0.1 255.240.0.0 UG 0 0 0 eno1 172.31.126.0 0.0.0.0 255.255.255.0 U 0 0 0 eno1 ` From 2b0651b55daa00e6c2396f7b2eeb4aa7c2cd244a Mon Sep 17 00:00:00 2001 From: balopat Date: Mon, 28 Jan 2019 11:43:01 -0800 Subject: [PATCH 2/3] parsing ip r output --- pkg/minikube/tunnel/route_linux.go | 44 +++++++++++++------------ pkg/minikube/tunnel/route_linux_test.go | 25 +++++++++----- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/pkg/minikube/tunnel/route_linux.go b/pkg/minikube/tunnel/route_linux.go index 6e433136b5..022799838e 100644 --- a/pkg/minikube/tunnel/route_linux.go +++ b/pkg/minikube/tunnel/route_linux.go @@ -54,7 +54,7 @@ func (router *osRouter) EnsureRouteIsAdded(route *Route) error { } func (router *osRouter) Inspect(route *Route) (exists bool, conflict string, overlaps []string, err error) { - cmd := exec.Command("route", "-n") + cmd := exec.Command("ip", "r") cmd.Env = append(cmd.Env, "LC_ALL=C") stdInAndOut, err := cmd.CombinedOutput() if err != nil { @@ -70,37 +70,39 @@ func (router *osRouter) Inspect(route *Route) (exists bool, conflict string, ove func (router *osRouter) parseTable(table []byte) routingTable { t := routingTable{} - skip := true for _, line := range strings.Split(string(table), "\n") { - //after first line of header we can start consuming - if strings.HasPrefix(line, "Destination") { - skip = false - continue - } fields := strings.Fields(line) - //don't care about the 0.0.0.0 routes - if skip || len(fields) == 0 || len(fields) > 0 && (fields[0] == "default" || fields[0] == "0.0.0.0") { + + //don't care about the routes that 0.0.0.0 + if len(fields) == 0 || + len(fields) > 0 && (fields[0] == "default" || fields[0] == "0.0.0.0") { continue } + if len(fields) > 2 { - dstCIDRIP := net.ParseIP(fields[0]) - dstCIDRMask := fields[2] - dstMaskIP := net.ParseIP(dstCIDRMask) - gatewayIP := net.ParseIP(fields[1]) - if dstCIDRIP == nil || gatewayIP == nil || dstMaskIP == nil { - glog.V(8).Infof("skipping line: can't parse: %s", line) + //assuming "10.96.0.0/12 via 192.168.39.47 dev virbr1" + dstCIDRString := fields[0] + gatewayIPString := fields[2] + gatewayIP := net.ParseIP(gatewayIPString) + + //if not via format, then gateway is assumed to be 0.0.0.0 + // "1.2.3.0/24 dev eno1 proto kernel scope link src 1.2.3.54 metric 100" + if fields[1] != "via" { + gatewayIP = net.ParseIP("0.0.0.0") + } + + _, ipNet, err := net.ParseCIDR(dstCIDRString) + if err != nil { + glog.V(4).Infof("skipping line: can't parse CIDR from routing table: %s", dstCIDRString) + } else if gatewayIP == nil { + glog.V(4).Infof("skipping line: can't parse IP from routing table: %s", gatewayIPString) } else { - dstCIDR := &net.IPNet{ - IP: dstCIDRIP, - Mask: net.IPv4Mask(dstMaskIP[12], dstMaskIP[13], dstMaskIP[14], dstMaskIP[15]), - } - tableLine := routingTableLine{ route: &Route{ - DestCIDR: dstCIDR, + DestCIDR: ipNet, Gateway: gatewayIP, }, line: line, diff --git a/pkg/minikube/tunnel/route_linux_test.go b/pkg/minikube/tunnel/route_linux_test.go index 8e1f059ea2..52f549fa8a 100644 --- a/pkg/minikube/tunnel/route_linux_test.go +++ b/pkg/minikube/tunnel/route_linux_test.go @@ -90,23 +90,30 @@ func TestLinuxRouteCleanupIdempontentIntegrationTest(t *testing.T) { func TestParseTable(t *testing.T) { - const table = `Kernel IP routing table -Destination Gateway Genmask Flags Metric Ref Use Iface -0.0.0.0 172.31.126.254 0.0.0.0 UG 100 0 0 eno1 -10.96.0.0 127.0.0.1 255.240.0.0 UG 0 0 0 eno1 -172.31.126.0 0.0.0.0 255.255.255.0 U 0 0 0 eno1 -` + const table = `default via 172.31.126.254 dev eno1 proto dhcp metric 100 +10.96.0.0/12 via 192.168.39.47 dev virbr1 +10.110.0.0/16 via 127.0.0.1 dev lo +172.31.126.0/24 dev eno1 proto kernel scope link src 172.31.126.54 metric 100 +192.168.9.0/24 dev docker0 proto kernel scope link src 192.168.9.1` rt := (&osRouter{}).parseTable([]byte(table)) expectedRt := routingTable{ routingTableLine{ - route: unsafeParseRoute("127.0.0.1", "10.96.0.0/12"), - line: "10.96.0.0 127.0.0.1 255.240.0.0 UG 0 0 0 eno1", + route: unsafeParseRoute("192.168.39.47", "10.96.0.0/12"), + line: "10.96.0.0/12 via 192.168.39.47 dev virbr1", + }, + routingTableLine{ + route: unsafeParseRoute("127.0.0.1", "10.110.0.0/16"), + line: "10.110.0.0/16 via 127.0.0.1 dev lo", }, routingTableLine{ route: unsafeParseRoute("0.0.0.0", "172.31.126.0/24"), - line: "172.31.126.0 0.0.0.0 255.255.255.0 U 0 0 0 eno1", + line: "172.31.126.0/24 dev eno1 proto kernel scope link src 172.31.126.54 metric 100", + }, + routingTableLine{ + route: unsafeParseRoute("0.0.0.0", "192.168.9.0/24"), + line: "192.168.9.0/24 dev docker0 proto kernel scope link src 192.168.9.1", }, } if !expectedRt.Equal(&rt) { From f3034d3c5012cdf6a41f499a83f9789cba659114 Mon Sep 17 00:00:00 2001 From: balopat Date: Mon, 28 Jan 2019 11:43:44 -0800 Subject: [PATCH 3/3] parsing ip r output --- pkg/minikube/tunnel/route_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/minikube/tunnel/route_linux.go b/pkg/minikube/tunnel/route_linux.go index 022799838e..95a21052f8 100644 --- a/pkg/minikube/tunnel/route_linux.go +++ b/pkg/minikube/tunnel/route_linux.go @@ -76,7 +76,7 @@ func (router *osRouter) parseTable(table []byte) routingTable { //don't care about the routes that 0.0.0.0 if len(fields) == 0 || - len(fields) > 0 && (fields[0] == "default" || fields[0] == "0.0.0.0") { + len(fields) > 0 && (fields[0] == "default" || fields[0] == "0.0.0.0") { continue }