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

Ignore ipv6 #2622

Merged
merged 4 commits into from
Jun 20, 2017
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
16 changes: 1 addition & 15 deletions probe/host/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package host

import (
"fmt"
"net"
"runtime"
"sync"
"time"
Expand Down Expand Up @@ -86,20 +85,7 @@ func NewReporter(hostID, hostName, probeID, version string, pipes controls.PipeC
func (*Reporter) Name() string { return "Host" }

// GetLocalNetworks is exported for mocking
var GetLocalNetworks = func() ([]*net.IPNet, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
localNets := report.Networks{}
for _, addr := range addrs {
// Not all addrs are IPNets.
if ipNet, ok := addr.(*net.IPNet); ok {
localNets = append(localNets, ipNet)
}
}
return localNets, nil
}
var GetLocalNetworks = report.GetLocalNetworks

// Report implements Reporter.
func (r *Reporter) Report() (report.Report, error) {
Expand Down
52 changes: 25 additions & 27 deletions report/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ import (
// Networks represent a set of subnets
type Networks []*net.IPNet

// Interface is exported for testing.
type Interface interface {
Addrs() ([]net.Addr, error)
}

// Variables exposed for testing.
// LocalNetworks helps in determining which addresses a probe reports
// as being host-scoped.
//
// TODO this design is broken, make it consistent with probe networks.
var (
LocalNetworks = Networks{}
InterfaceByNameStub = func(name string) (Interface, error) { return net.InterfaceByName(name) }
)
var LocalNetworks = Networks{}

// Contains returns true if IP is in Networks.
func (n Networks) Contains(ip net.IP) bool {
Expand Down Expand Up @@ -51,12 +45,7 @@ func LocalAddresses() ([]net.IP, error) {
return []net.IP{}, err
}

for _, addr := range addrs {
ipnet, ok := addr.(*net.IPNet)
if !ok {
continue
}

for _, ipnet := range ipv4Nets(addrs) {
result = append(result, ipnet.IP)
}
}
Expand All @@ -68,7 +57,7 @@ func LocalAddresses() ([]net.IP, error) {
// supplied, such that MakeAddressNodeID will scope addresses in this subnet
// as local.
func AddLocalBridge(name string) error {
inf, err := InterfaceByNameStub(name)
inf, err := net.InterfaceByName(name)
if err != nil {
return err
}
Expand All @@ -77,18 +66,27 @@ func AddLocalBridge(name string) error {
if err != nil {
return err
}
for _, addr := range addrs {
_, network, err := net.ParseCIDR(addr.String())
if err != nil {
return err
}

if network == nil {
continue
}
LocalNetworks = ipv4Nets(addrs)

LocalNetworks = append(LocalNetworks, network)
return nil
}

// GetLocalNetworks returns all the local networks.
func GetLocalNetworks() ([]*net.IPNet, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
return ipv4Nets(addrs), nil
}

return nil
func ipv4Nets(addrs []net.Addr) []*net.IPNet {
nets := Networks{}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.To4() != nil {
nets = append(nets, ipnet)
}
}
return nets
}
41 changes: 0 additions & 41 deletions report/networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"net"
"testing"

"github.com/weaveworks/common/test"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test/reflect"
)

func TestContains(t *testing.T) {
Expand All @@ -31,42 +29,3 @@ func mustParseCIDR(s string) *net.IPNet {
}
return ipNet
}

type mockInterface struct {
addrs []net.Addr
}

type mockAddr string

func (m mockInterface) Addrs() ([]net.Addr, error) {
return m.addrs, nil
}

func (m mockAddr) Network() string {
return "ip+net"
}

func (m mockAddr) String() string {
return string(m)
}

func TestAddLocal(t *testing.T) {
oldInterfaceByNameStub := report.InterfaceByNameStub
defer func() { report.InterfaceByNameStub = oldInterfaceByNameStub }()

report.InterfaceByNameStub = func(name string) (report.Interface, error) {
return mockInterface{[]net.Addr{mockAddr("52.53.54.55/16")}}, nil
}

err := report.AddLocalBridge("foo")
if err != nil {
t.Errorf("%v", err)
}

want := report.Networks([]*net.IPNet{mustParseCIDR("52.53.54.55/16")})
have := report.LocalNetworks

if !reflect.DeepEqual(want, have) {
t.Errorf("%s", test.Diff(want, have))
}
}