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

fast network membership check #2625

Merged
merged 1 commit into from
Jun 21, 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
6 changes: 2 additions & 4 deletions render/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package render_test

import (
"fmt"
"net"
"testing"

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

func testMap(t *testing.T, f render.MapFunc, input testcase) {
_, ipNet, err := net.ParseCIDR("1.2.3.0/16")
if err != nil {
localNetworks := report.NewNetworks()
if err := localNetworks.AddCIDR("1.2.3.0/16"); 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: 3 additions & 15 deletions render/theinternet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package render

import (
"net"
"regexp"
"strings"

Expand Down Expand Up @@ -68,26 +67,15 @@ 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 {
var (
result = report.Networks{}
networks = map[string]struct{}{}
)
networks := report.NewNetworks()

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

import (
"net"
"reflect"
"testing"

Expand Down Expand Up @@ -30,21 +29,14 @@ func TestReportLocalNetworks(t *testing.T) {
},
},
})
want := report.Networks([]*net.IPNet{
mustParseCIDR("10.0.0.1/8"),
mustParseCIDR("192.168.1.1/24"),
mustParseCIDR("10.32.0.1/12"),
})
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)
}
}
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: 25 additions & 10 deletions report/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,38 @@ package report
import (
"net"
"strings"

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

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

// 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{}
var LocalNetworks = NewNetworks()

// NewNetworks creates a datastructure representing a set of networks.
func NewNetworks() Networks {
return Networks{critbitgo.NewNet()}
}

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

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

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

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

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

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

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

func TestContains(t *testing.T) {
networks := report.Networks([]*net.IPNet{
mustParseCIDR("10.0.0.1/8"),
mustParseCIDR("192.168.1.1/24"),
})
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)
}
}

if networks.Contains(net.ParseIP("52.52.52.52")) {
t.Errorf("52.52.52.52 not in %v", networks)
Expand All @@ -21,11 +23,3 @@ 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: 22 additions & 0 deletions vendor/github.com/k-sone/critbitgo/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading