Skip to content

Commit

Permalink
Revert "fast network membership check"
Browse files Browse the repository at this point in the history
This reverts commit 98f0363.
  • Loading branch information
rade committed Jun 20, 2017
1 parent 4a54b75 commit 9e75331
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 747 deletions.
6 changes: 4 additions & 2 deletions render/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package render_test

import (
"fmt"
"net"
"testing"

"github.com/weaveworks/common/test"
Expand Down Expand Up @@ -45,10 +46,11 @@ type testcase struct {
}

func testMap(t *testing.T, f render.MapFunc, input testcase) {
localNetworks := report.NewNetworks()
if err := localNetworks.AddCIDR("1.2.3.0/16"); err != nil {
_, ipNet, err := net.ParseCIDR("1.2.3.0/16")
if err != nil {
t.Fatalf(err.Error())
}
localNetworks := report.Networks([]*net.IPNet{ipNet})
if have := f(input.n, localNetworks); input.ok != (len(have) > 0) {
name := input.name
if name == "" {
Expand Down
18 changes: 15 additions & 3 deletions render/theinternet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package render

import (
"net"
"regexp"
"strings"

Expand Down Expand Up @@ -67,15 +68,26 @@ func isKnownService(hostname string) bool {
// used to determine which nodes in the report are "remote", i.e. outside of
// our infrastructure.
func LocalNetworks(r report.Report) report.Networks {
networks := report.NewNetworks()
var (
result = report.Networks{}
networks = map[string]struct{}{}
)

for _, topology := range []report.Topology{r.Host, r.Overlay} {
for _, md := range topology.Nodes {
nets, _ := md.Sets.Lookup(host.LocalNetworks)
for _, s := range nets {
networks.AddCIDR(s)
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
continue
}
_, ok := networks[ipNet.String()]
if !ok {
result = append(result, ipNet)
networks[ipNet.String()] = struct{}{}
}
}
}
}
return networks
return result
}
20 changes: 14 additions & 6 deletions render/theinternet_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package render_test

import (
"net"
"reflect"
"testing"

Expand Down Expand Up @@ -29,14 +30,21 @@ func TestReportLocalNetworks(t *testing.T) {
},
},
})
want := report.NewNetworks()
for _, cidr := range []string{"10.0.0.1/8", "192.168.1.1/24", "10.32.0.1/12"} {
if err := want.AddCIDR(cidr); err != nil {
panic(err)
}
}
want := report.Networks([]*net.IPNet{
mustParseCIDR("10.0.0.1/8"),
mustParseCIDR("192.168.1.1/24"),
mustParseCIDR("10.32.0.1/12"),
})
have := render.LocalNetworks(r)
if !reflect.DeepEqual(want, have) {
t.Errorf("%s", test.Diff(want, have))
}
}

func mustParseCIDR(s string) *net.IPNet {
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
panic(err)
}
return ipNet
}
35 changes: 10 additions & 25 deletions report/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,25 @@ package report
import (
"net"
"strings"

"github.com/k-sone/critbitgo"
)

// Networks represent a set of subnets
type Networks struct{ *critbitgo.Net }
type Networks []*net.IPNet

// 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 = NewNetworks()

// Create datastructure for set of subnets.
func NewNetworks() Networks {
return Networks{critbitgo.NewNet()}
}

// Add a network.
func (n Networks) Add(ipnet *net.IPNet) error {
return n.Net.Add(ipnet, struct{}{})
}

// Add a network, represented as CIDR.
func (n Networks) AddCIDR(cidr string) error {
return n.Net.AddCIDR(cidr, struct{}{})
}
var LocalNetworks = Networks{}

// Contains returns true if IP is in Networks.
func (n Networks) Contains(ip net.IP) bool {
network, _, _ := n.MatchIP(ip)
return network != nil
for _, net := range n {
if net.Contains(ip) {
return true
}
}
return false
}

// LocalAddresses returns a list of the local IP addresses.
Expand Down Expand Up @@ -80,9 +67,7 @@ func AddLocalBridge(name string) error {
return err
}

for _, ipnet := range ipv4Nets(addrs) {
LocalNetworks.Add(ipnet)
}
LocalNetworks = ipv4Nets(addrs)

return nil
}
Expand All @@ -97,7 +82,7 @@ func GetLocalNetworks() ([]*net.IPNet, error) {
}

func ipv4Nets(addrs []net.Addr) []*net.IPNet {
nets := []*net.IPNet{}
nets := Networks{}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.To4() != nil {
nets = append(nets, ipnet)
Expand Down
18 changes: 12 additions & 6 deletions report/networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (
)

func TestContains(t *testing.T) {
networks := report.NewNetworks()
for _, cidr := range []string{"10.0.0.1/8", "192.168.1.1/24"} {
if err := networks.AddCIDR(cidr); err != nil {
panic(err)
}
}
networks := report.Networks([]*net.IPNet{
mustParseCIDR("10.0.0.1/8"),
mustParseCIDR("192.168.1.1/24"),
})

if networks.Contains(net.ParseIP("52.52.52.52")) {
t.Errorf("52.52.52.52 not in %v", networks)
Expand All @@ -23,3 +21,11 @@ func TestContains(t *testing.T) {
t.Errorf("10.0.0.1 in %v", networks)
}
}

func mustParseCIDR(s string) *net.IPNet {
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
panic(err)
}
return ipNet
}
22 changes: 0 additions & 22 deletions vendor/github.com/k-sone/critbitgo/LICENSE

This file was deleted.

Loading

0 comments on commit 9e75331

Please sign in to comment.