Files
weave-scope/report/networks.go
Peter Bourgon 0aadf6447b Revert to correct edge construction
Another implicit invariant in the data model is that edges are always of the
form (local -> remote). That is, the source of an edge must always be a node
that originates from within Scope's domain of visibility. This was evident by
the presence of ingress and egress fields in edge/aggregate metadata.

When building the sniffer, I accidentally and incorrectly violated this
invariant, by constructing distinct edges for (local -> remote) and (remote ->
local), and collapsing ingress and egress byte counts to a single scalar. I
experienced a variety of subtle undefined behavior as a result. See #339.

This change reverts to the old, correct methodology. Consequently the sniffer
needs to be able to find out which side of the sniffed packet is local v.
remote, and to do that it needs access to local networks. I moved the
discovery from the probe/host package into probe/main.go.

As part of that work I discovered that package report also maintains its own,
independent "cache" of local networks. Except it contains only the (optional)
Docker bridge network, if it's been populated by the probe, and it's only used
by the report.Make{Endpoint,Address}NodeID constructors to scope local
addresses. Normally, scoping happens during rendering, and only for pseudo
nodes -- see current LeafMap Render localNetworks. This is pretty convoluted
and should be either be made consistent or heavily commented.
2015-08-03 10:55:59 +02:00

60 lines
1.2 KiB
Go

package report
import (
"net"
)
// 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.
// TODO this design is broken, make it consistent with probe networks.
var (
LocalNetworks = Networks{}
InterfaceByNameStub = func(name string) (Interface, error) { return net.InterfaceByName(name) }
)
// 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
}
// AddLocalBridge records the subnet address associated with the bridge name
// supplied, such that MakeAddressNodeID will scope addresses in this subnet
// as local.
func AddLocalBridge(name string) error {
inf, err := InterfaceByNameStub(name)
if err != nil {
return err
}
addrs, err := inf.Addrs()
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 = append(LocalNetworks, network)
}
return nil
}