Remove report squash logic.

This commit is contained in:
Tom Wilkie
2015-06-18 16:38:10 +00:00
parent e8a9c3c6e1
commit f32d2b5a5e
7 changed files with 30 additions and 150 deletions

View File

@@ -170,5 +170,5 @@ func (s StaticReport) Report() report.Report {
},
},
}
return testReport.Squash()
return testReport
}

View File

@@ -54,7 +54,6 @@ func NewReportLIFO(r reporter, maxAge time.Duration) *ReportLIFO {
for _, r := range l.reports {
report.Merge(r.Report)
}
report = report.Squash() // TODO?: make this a CLI argument.
req <- report
case q := <-l.quit:

View File

@@ -112,6 +112,34 @@ func makeAvoid(fixed []string) map[string]struct{} {
return avoid
}
// LocalNetworks returns a superset of the networks (think: CIDRs) that are
// "local" from the perspective of each host represented in the report. It's
// used to determine which nodes in the report are "remote", i.e. outside of
// our infrastructure.
func LocalNetworks(r report.Report) []*net.IPNet {
var ipNets []*net.IPNet
for _, md := range r.Host.NodeMetadatas {
val, ok := md["local_networks"]
if !ok {
continue
}
outer:
for _, s := range strings.Fields(val) {
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
continue
}
for _, existing := range ipNets {
if ipNet.String() == existing.String() {
continue outer
}
}
ipNets = append(ipNets, ipNet)
}
}
return ipNets
}
// discover reads reports from a collector and republishes them on the
// publisher, while scanning the reports for IPs to connect to. Only addresses
// in the network topology of the report are considered. IPs listed in fixed
@@ -127,7 +155,7 @@ func discover(c collector, p publisher, fixed []string) {
var (
now = time.Now()
localNets = r.LocalNetworks()
localNets = LocalNetworks(r)
)
for _, adjacent := range r.Address.Adjacency {

View File

@@ -41,7 +41,6 @@ func NewReportLIFO(r reporter, maxAge time.Duration) *ReportLIFO {
for {
select {
case report := <-r.Reports():
report = report.Squash()
tr := timedReport{
Timestamp: time.Now(),
Report: report,

View File

@@ -1,10 +1,5 @@
package report
import (
"net"
"strings"
)
// Report is the core data type. It's produced by probes, and consumed and
// stored by apps. It's composed of multiple topologies, each representing
// a different (related, but not equivalent) view of the network.
@@ -91,48 +86,6 @@ func MakeReport() Report {
}
}
// Squash squashes all non-local nodes in the report to a super-node called
// the Internet.
func (r Report) Squash() Report {
localNetworks := r.LocalNetworks()
r.Endpoint = r.Endpoint.Squash(EndpointIDAddresser, localNetworks)
r.Address = r.Address.Squash(AddressIDAddresser, localNetworks)
r.Process = r.Process.Squash(PanicIDAddresser, localNetworks)
r.Container = r.Container.Squash(PanicIDAddresser, localNetworks)
r.ContainerImage = r.ContainerImage.Squash(PanicIDAddresser, localNetworks)
r.Host = r.Host.Squash(PanicIDAddresser, localNetworks)
r.Overlay = r.Overlay.Squash(PanicIDAddresser, localNetworks)
return r
}
// LocalNetworks returns a superset of the networks (think: CIDRs) that are
// "local" from the perspective of each host represented in the report. It's
// used to determine which nodes in the report are "remote", i.e. outside of
// our infrastructure.
func (r Report) LocalNetworks() []*net.IPNet {
var ipNets []*net.IPNet
for _, md := range r.Host.NodeMetadatas {
val, ok := md["local_networks"]
if !ok {
continue
}
outer:
for _, s := range strings.Fields(val) {
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
continue
}
for _, existing := range ipNets {
if ipNet.String() == existing.String() {
continue outer
}
}
ipNets = append(ipNets, ipNet)
}
}
return ipNets
}
// Topologies returns a slice of Topologies in this report
func (r Report) Topologies() []Topology {
return []Topology{r.Endpoint, r.Address, r.Process, r.Container,

View File

@@ -1,55 +0,0 @@
package report_test
import (
"net"
"reflect"
"testing"
"github.com/weaveworks/scope/report"
)
func TestReportLocalNetworks(t *testing.T) {
r := report.MakeReport()
r.Merge(report.Report{Host: report.Topology{NodeMetadatas: report.NodeMetadatas{
"nonets": {},
"foo": {"local_networks": "10.0.0.1/8 192.168.1.1/24 10.0.0.1/8 badnet/33"},
}}})
if want, have := []*net.IPNet{
mustParseCIDR("10.0.0.1/8"),
mustParseCIDR("192.168.1.1/24"),
}, r.LocalNetworks(); !reflect.DeepEqual(want, have) {
t.Errorf("want %+v, have %+v", want, have)
}
}
func TestReportSquash(t *testing.T) {
{
want := report.Adjacency{
report.MakeAdjacencyID(client54001EndpointNodeID): report.MakeIDList(server80EndpointNodeID),
report.MakeAdjacencyID(client54002EndpointNodeID): report.MakeIDList(server80EndpointNodeID),
report.MakeAdjacencyID(server80EndpointNodeID): report.MakeIDList(client54001EndpointNodeID, client54002EndpointNodeID, report.TheInternet),
}
have := reportFixture.Squash().Endpoint.Adjacency
if !reflect.DeepEqual(want, have) {
t.Error(diff(want, have))
}
}
{
want := report.Adjacency{
report.MakeAdjacencyID(clientAddressNodeID): report.MakeIDList(serverAddressNodeID),
report.MakeAdjacencyID(serverAddressNodeID): report.MakeIDList(clientAddressNodeID, report.TheInternet),
}
have := reportFixture.Squash().Address.Adjacency
if !reflect.DeepEqual(want, have) {
t.Error(diff(want, have))
}
}
}
func mustParseCIDR(s string) *net.IPNet {
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
panic(err)
}
return ipNet
}

View File

@@ -2,7 +2,6 @@ package report
import (
"fmt"
"net"
"strings"
)
@@ -75,49 +74,6 @@ func NewTopology() Topology {
}
}
// Squash squashes all non-local nodes in the topology to a super-node called
// the Internet.
// We rely on the values in the t.Adjacency lists being valid keys in
// t.NodeMetadata (or t.Adjacency).
func (t Topology) Squash(f IDAddresser, localNets []*net.IPNet) Topology {
isRemote := func(id string) bool {
if _, ok := t.NodeMetadatas[id]; ok {
return false // it is a node, cannot possibly be remote
}
if _, ok := t.Adjacency[MakeAdjacencyID(id)]; ok {
return false // it is in our adjacency list, cannot possibly be remote
}
if ip := f(id); ip != nil && netsContain(localNets, ip) {
return false // it is in our local nets, so it is not remote
}
return true
}
for srcID, dstIDs := range t.Adjacency {
newDstIDs := make(IDList, 0, len(dstIDs))
for _, dstID := range dstIDs {
if isRemote(dstID) {
dstID = TheInternet
}
newDstIDs = newDstIDs.Add(dstID)
}
t.Adjacency[srcID] = newDstIDs
}
return t
}
func netsContain(nets []*net.IPNet, ip net.IP) bool {
for _, net := range nets {
if net.Contains(ip) {
return true
}
}
return false
}
// Validate checks the topology for various inconsistencies.
func (t Topology) Validate() error {
// Check all edge metadata keys must have the appropriate entries in