Skip to content

Commit

Permalink
WeaveTagger becomes Weave, both Reporter & Tagger
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon committed Jun 30, 2015
1 parent f4b3930 commit d9a37df
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 46 deletions.
1 change: 0 additions & 1 deletion probe/endpoint/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ func applyNAT(rpt report.Report, scope string) error {
func conntrackModulePresent() bool {
f, err := os.Open(modules)
if err != nil {
//log.Printf("conntrack error: %v", err) // /proc doesn't exist on Darwin
return false
}
defer f.Close()
Expand Down
30 changes: 7 additions & 23 deletions probe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,13 @@ func main() {
defer publisher.Close()

var (
hostName = hostname()
hostID = hostName // TODO: we should sanitize the hostname
)

var (
weaveTagger *overlay.WeaveTagger
hostName = hostname()
hostID = hostName // TODO: we should sanitize the hostname
taggers = []Tagger{newTopologyTagger(), host.NewTagger(hostID)}
reporters = []Reporter{host.NewReporter(hostID, hostName), endpoint.NewReporter(hostID, hostName, *spyProcs)}
processCache *process.CachingWalker
)

taggers := []Tagger{
newTopologyTagger(),
host.NewTagger(hostID),
}

reporters := []Reporter{
host.NewReporter(hostID, hostName),
endpoint.NewReporter(hostID, hostName, *spyProcs),
}

// TODO provide an alternate implementation for Darwin.
if runtime.GOOS == linux {
processCache = process.NewCachingWalker(process.NewWalker(*procRoot))
Expand All @@ -116,12 +104,12 @@ func main() {
}

if *weaveRouterAddr != "" {
var err error
weaveTagger, err = overlay.NewWeaveTagger(*weaveRouterAddr)
weave, err := overlay.NewWeave(*weaveRouterAddr)
if err != nil {
log.Fatalf("failed to start Weave tagger: %v", err)
}
taggers = append(taggers, weaveTagger)
taggers = append(taggers, weave)
reporters = append(reporters, weave)
}

log.Printf("listening on %s", *listen)
Expand Down Expand Up @@ -157,10 +145,6 @@ func main() {
r.Merge(newReport)
}

if weaveTagger != nil {
r.Overlay.Merge(weaveTagger.OverlayTopology())
}

r = Apply(r, taggers)

case <-quit:
Expand Down
36 changes: 18 additions & 18 deletions probe/overlay/weave_tagger.go → probe/overlay/weave.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,40 @@ const (
WeavePeerNickName = "weave_peer_nick_name"
)

// WeaveTagger represents a single Weave router, presumably on the same host
// as the probe. It can produce an Overlay topology, and in theory can tag
// existing topologies with foreign keys to overlay -- though I'm not sure
// what that would look like in practice right now.
type WeaveTagger struct {
// Weave represents a single Weave router, presumably on the same host
// as the probe. It is both a Reporter and a Tagger: it produces an Overlay
// topology, and (in theory) can tag existing topologies with foreign keys to
// overlay -- though I'm not sure what that would look like in practice right
// now.
type Weave struct {
url string
}

// NewWeaveTagger returns a new Weave tagger based on the Weave router at
// NewWeave returns a new Weave tagger based on the Weave router at
// address. The address should be an IP or FQDN, no port.
func NewWeaveTagger(weaveRouterAddress string) (*WeaveTagger, error) {
func NewWeave(weaveRouterAddress string) (*Weave, error) {
s, err := sanitize("http://", 6784, "/status-json")(weaveRouterAddress)
if err != nil {
return nil, err
}
return &WeaveTagger{s}, nil
return &Weave{s}, nil
}

// Tag implements Tagger.
func (t WeaveTagger) Tag(r report.Report) (report.Report, error) {
func (w Weave) Tag(r report.Report) (report.Report, error) {
// The status-json endpoint doesn't return any link information, so
// there's nothing to tag, yet.
return r, nil
}

// OverlayTopology produces an overlay topology from the Weave router.
func (t WeaveTagger) OverlayTopology() report.Topology {
topology := report.NewTopology()
// Report implements Reporter.
func (w Weave) Report() (report.Report, error) {
r := report.MakeReport()

resp, err := http.Get(t.url)
resp, err := http.Get(w.url)
if err != nil {
log.Printf("Weave Tagger: %v", err)
return topology
return r, err
}
defer resp.Body.Close()

Expand All @@ -65,17 +66,16 @@ func (t WeaveTagger) OverlayTopology() report.Topology {
}
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
log.Printf("Weave Tagger: %v", err)
return topology
return r, err
}

for _, peer := range status.Peers {
topology.NodeMetadatas[report.MakeOverlayNodeID(peer.Name)] = report.NodeMetadata{
r.Overlay.NodeMetadatas[report.MakeOverlayNodeID(peer.Name)] = report.NodeMetadata{
WeavePeerName: peer.Name,
WeavePeerNickName: peer.NickName,
}
}

return topology
return r, nil
}

func sanitize(scheme string, port int, path string) func(string) (string, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ import (

"github.com/weaveworks/scope/probe/overlay"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test"
)

func TestWeaveTaggerOverlayTopology(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(mockWeaveRouter))
defer s.Close()

w, err := overlay.NewWeaveTagger(s.URL)
w, err := overlay.NewWeave(s.URL)
if err != nil {
t.Fatal(err)
}

have, err := w.Report()
if err != nil {
t.Fatal(err)
}
if want, have := (report.Topology{
Adjacency: report.Adjacency{},
EdgeMetadatas: report.EdgeMetadatas{},
Expand All @@ -29,8 +34,8 @@ func TestWeaveTaggerOverlayTopology(t *testing.T) {
overlay.WeavePeerNickName: mockWeavePeerNickName,
},
},
}), w.OverlayTopology(); !reflect.DeepEqual(want, have) {
t.Errorf("want\n\t%#v, have\n\t%#v", want, have)
}), have.Overlay; !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}

Expand All @@ -46,6 +51,6 @@ func mockWeaveRouter(w http.ResponseWriter, r *http.Request) {
"NickName": mockWeavePeerNickName,
}},
}); err != nil {
println(err.Error())
panic(err)
}
}

0 comments on commit d9a37df

Please sign in to comment.