Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix netstat -f error on linux distros #3592

Merged
merged 3 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions pkg/minikube/tunnel/route_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("ip", "r")
cmd.Env = append(cmd.Env, "LC_ALL=C")
stdInAndOut, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -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)
} else {
//assuming "10.96.0.0/12 via 192.168.39.47 dev virbr1"
dstCIDRString := fields[0]
gatewayIPString := fields[2]
gatewayIP := net.ParseIP(gatewayIPString)

dstCIDR := &net.IPNet{
IP: dstCIDRIP,
Mask: net.IPv4Mask(dstMaskIP[12], dstMaskIP[13], dstMaskIP[14], dstMaskIP[15]),
}
//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 {

tableLine := routingTableLine{
route: &Route{
DestCIDR: dstCIDR,
DestCIDR: ipNet,
Gateway: gatewayIP,
},
line: line,
Expand Down
25 changes: 16 additions & 9 deletions pkg/minikube/tunnel/route_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,30 @@ 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
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) {
Expand Down