Skip to content

Commit

Permalink
Removing report.Node.WithID (#1315)
Browse files Browse the repository at this point in the history
* removing usage of report.Node.WithID

* report.Topology.AddNode can use the node's ID field
  • Loading branch information
paulbellamy authored and tomwilkie committed Apr 19, 2016
1 parent 7a6ca39 commit 1edeb8d
Show file tree
Hide file tree
Showing 45 changed files with 349 additions and 311 deletions.
6 changes: 3 additions & 3 deletions app/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func TestCollector(t *testing.T) {
c := app.NewCollector(window)

r1 := report.MakeReport()
r1.Endpoint.AddNode("foo", report.MakeNode())
r1.Endpoint.AddNode(report.MakeNode("foo"))

r2 := report.MakeReport()
r2.Endpoint.AddNode("bar", report.MakeNode())
r2.Endpoint.AddNode(report.MakeNode("foo"))

have, err := c.Report(ctx)
if err != nil {
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestCollectorExpire(t *testing.T) {

// Now check an added report is returned
r1 := report.MakeReport()
r1.Endpoint.AddNode("foo", report.MakeNode())
r1.Endpoint.AddNode(report.MakeNode("foo"))
c.Add(ctx, r1)
have, err = c.Report(ctx)
if err != nil {
Expand Down
14 changes: 9 additions & 5 deletions experimental/demoprobe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,28 @@ func demoReport(nodeCount int) report.Report {
)

// Endpoint topology
r.Endpoint = r.Endpoint.AddNode(srcPortID, report.MakeNode().WithLatests(map[string]string{
r.Endpoint = r.Endpoint.AddNode(report.MakeNodeWith(srcPortID, map[string]string{
process.PID: "4000",
"name": c.srcProc,
"domain": "node-" + src,
}).WithEdge(dstPortID, report.EdgeMetadata{}))
r.Endpoint = r.Endpoint.AddNode(dstPortID, report.MakeNode().WithLatests(map[string]string{
}).
WithEdge(dstPortID, report.EdgeMetadata{}))

r.Endpoint = r.Endpoint.AddNode(report.MakeNodeWith(dstPortID, map[string]string{
process.PID: "4000",
"name": c.dstProc,
"domain": "node-" + dst,
}).WithEdge(srcPortID, report.EdgeMetadata{}))
}).
WithEdge(srcPortID, report.EdgeMetadata{}))

// Host data
r.Host = r.Host.AddNode("hostX", report.MakeNodeWith(map[string]string{
r.Host = r.Host.AddNode(report.MakeNodeWith("hostX", map[string]string{
"ts": time.Now().UTC().Format(time.RFC3339Nano),
"host_name": "host-x",
"local_networks": localNet.String(),
"os": "linux",
}))

}

return r
Expand Down
14 changes: 9 additions & 5 deletions experimental/genreport/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,28 @@ func DemoReport(nodeCount int) report.Report {
)

// Endpoint topology
r.Endpoint = r.Endpoint.AddNode(srcPortID, report.MakeNode().WithLatests(map[string]string{
r.Endpoint = r.Endpoint.AddNode(report.MakeNodeWith(srcPortID, map[string]string{
"pid": "4000",
"name": c.srcProc,
"domain": "node-" + src,
}).WithEdge(dstPortID, report.EdgeMetadata{}))
r.Endpoint = r.Endpoint.AddNode(dstPortID, report.MakeNode().WithLatests(map[string]string{
}).
WithEdge(dstPortID, report.EdgeMetadata{}))

r.Endpoint = r.Endpoint.AddNode(report.MakeNodeWith(dstPortID, map[string]string{
"pid": "4000",
"name": c.dstProc,
"domain": "node-" + dst,
}).WithEdge(srcPortID, report.EdgeMetadata{}))
}).
WithEdge(srcPortID, report.EdgeMetadata{}))

// Host data
r.Host = r.Host.AddNode("hostX", report.MakeNodeWith(map[string]string{
r.Host = r.Host.AddNode(report.MakeNodeWith("hostX", map[string]string{
"ts": time.Now().UTC().Format(time.RFC3339Nano),
"host_name": "host-x",
"local_networks": localNet.String(),
"os": "linux",
}))

}

return r
Expand Down
4 changes: 2 additions & 2 deletions experimental/sniff/sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ func (s *Sniffer) Merge(p Packet, rpt *report.Report) {
}

addAdjacency := func(t report.Topology, srcNodeID, dstNodeID string) report.Topology {
result := t.AddNode(srcNodeID, report.MakeNode().WithAdjacent(dstNodeID))
result = result.AddNode(dstNodeID, report.MakeNode())
result := t.AddNode(report.MakeNode(srcNodeID).WithAdjacent(dstNodeID))
result = result.AddNode(report.MakeNode(dstNodeID))
return result
}

Expand Down
2 changes: 1 addition & 1 deletion experimental/sniff/sniffer_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestInterpolateCounts(t *testing.T) {
r := report.MakeReport()
r.Sampling.Count = samplingCount
r.Sampling.Total = samplingTotal
r.Endpoint.AddNode(srcNodeID, report.MakeNode().WithEdge(dstNodeID, report.EdgeMetadata{
r.Endpoint.AddNode(report.MakeNode(srcNodeID).WithEdge(dstNodeID, report.EdgeMetadata{
EgressPacketCount: newu64(packetCount),
IngressPacketCount: newu64(packetCount),
EgressByteCount: newu64(byteCount),
Expand Down
8 changes: 4 additions & 4 deletions experimental/sniff/sniffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func TestMerge(t *testing.T) {
)
if want, have := (report.Topology{
Nodes: report.Nodes{
srcEndpointNodeID: report.MakeNode().WithEdge(dstEndpointNodeID, report.EdgeMetadata{
srcEndpointNodeID: report.MakeNode(srcEndpointNodeID).WithEdge(dstEndpointNodeID, report.EdgeMetadata{
EgressPacketCount: newu64(1),
EgressByteCount: newu64(256),
}),
dstEndpointNodeID: report.MakeNode(),
dstEndpointNodeID: report.MakeNode(dstEndpointNodeID),
},
}), rpt.Endpoint; !reflect.DeepEqual(want, have) {
t.Errorf("%s", test.Diff(want, have))
Expand All @@ -82,11 +82,11 @@ func TestMerge(t *testing.T) {
)
if want, have := (report.Topology{
Nodes: report.Nodes{
srcAddressNodeID: report.MakeNode().WithEdge(dstAddressNodeID, report.EdgeMetadata{
srcAddressNodeID: report.MakeNode(srcAddressNodeID).WithEdge(dstAddressNodeID, report.EdgeMetadata{
EgressPacketCount: newu64(1),
EgressByteCount: newu64(512),
}),
dstAddressNodeID: report.MakeNode(),
dstAddressNodeID: report.MakeNode(dstAddressNodeID),
},
}), rpt.Address; !reflect.DeepEqual(want, have) {
t.Errorf("%s", test.Diff(want, have))
Expand Down
2 changes: 1 addition & 1 deletion probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node {
ipsWithScopes = append(ipsWithScopes, report.MakeScopedAddressNodeID(hostID, ip))
}

result := report.MakeNodeWith(map[string]string{
result := report.MakeNodeWith(report.MakeContainerNodeID(c.ID()), map[string]string{
ContainerID: c.ID(),
ContainerName: strings.TrimPrefix(c.container.Name, "/"),
ContainerCreated: c.container.Created.Format(time.RFC822),
Expand Down
13 changes: 7 additions & 6 deletions probe/docker/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestContainer(t *testing.T) {

// Now see if we go them
uptime := (now.Sub(startTime) / time.Second) * time.Second
want := report.MakeNode().WithLatests(map[string]string{
want := report.MakeNodeWith("ping;<container>", map[string]string{
"docker_container_command": " ",
"docker_container_created": "01 Jan 01 00:00 UTC",
"docker_container_id": "ping",
Expand All @@ -85,11 +85,12 @@ func TestContainer(t *testing.T) {
"docker_container_state": "running",
"docker_container_state_human": "Up 6 years",
"docker_container_uptime": uptime.String(),
}).WithSets(report.EmptySets.
Add("docker_container_ports", report.MakeStringSet("1.2.3.4:80->80/tcp", "81/tcp")).
Add("docker_container_ips", report.MakeStringSet("1.2.3.4")).
Add("docker_container_ips_with_scopes", report.MakeStringSet("scope;1.2.3.4")),
).WithControls(
}).
WithSets(report.EmptySets.
Add("docker_container_ports", report.MakeStringSet("1.2.3.4:80->80/tcp", "81/tcp")).
Add("docker_container_ips", report.MakeStringSet("1.2.3.4")).
Add("docker_container_ips_with_scopes", report.MakeStringSet("scope;1.2.3.4")),
).WithControls(
docker.RestartContainer, docker.StopContainer, docker.PauseContainer,
docker.AttachContainer, docker.ExecContainer,
).WithMetrics(report.Metrics{
Expand Down
2 changes: 1 addition & 1 deletion probe/docker/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestEnv(t *testing.T) {
"FOO1=\"foo=bar\"",
"FOO2",
}
nmd := report.MakeNode()
nmd := report.MakeNode("foo")

nmd = docker.AddEnv(nmd, given)
have := docker.ExtractEnv(nmd)
Expand Down
2 changes: 1 addition & 1 deletion probe/docker/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestLabels(t *testing.T) {
"foo1": "bar1",
"foo2": "bar2",
}
nmd := report.MakeNode()
nmd := report.MakeNode("foo1")

nmd = docker.AddLabels(nmd, want)
have := docker.ExtractLabels(nmd)
Expand Down
2 changes: 1 addition & 1 deletion probe/docker/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *mockContainer) StartGatheringStats() error {
func (c *mockContainer) StopGatheringStats() {}

func (c *mockContainer) GetNode(_ string, _ []net.IP) report.Node {
return report.MakeNodeWith(map[string]string{
return report.MakeNodeWith(report.MakeContainerNodeID(c.c.ID), map[string]string{
docker.ContainerID: c.c.ID,
docker.ContainerName: c.c.Name,
docker.ImageID: c.c.Image,
Expand Down
11 changes: 5 additions & 6 deletions probe/docker/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *Reporter) ContainerUpdated(c Container) {
// Publish a 'short cut' report container just this container
rpt := report.MakeReport()
rpt.Shortcut = true
rpt.Container.AddNode(report.MakeContainerNodeID(c.ID()), c.GetNode(r.hostID, localAddrs))
rpt.Container.AddNode(c.GetNode(r.hostID, localAddrs))
r.probe.Publish(rpt)
}

Expand Down Expand Up @@ -136,8 +136,7 @@ func (r *Reporter) containerTopology(localAddrs []net.IP) report.Topology {
metadata := map[string]string{report.ControlProbeID: r.probeID}

r.registry.WalkContainers(func(c Container) {
nodeID := report.MakeContainerNodeID(c.ID())
result.AddNode(nodeID, c.GetNode(r.hostID, localAddrs).WithLatests(metadata))
result.AddNode(c.GetNode(r.hostID, localAddrs).WithLatests(metadata))
})

return result
Expand All @@ -148,7 +147,8 @@ func (r *Reporter) containerImageTopology() report.Topology {

r.registry.WalkImages(func(image *docker_client.APIImages) {
imageID := trimImageID(image.ID)
node := report.MakeNodeWith(map[string]string{
nodeID := report.MakeContainerImageNodeID(imageID)
node := report.MakeNodeWith(nodeID, map[string]string{
ImageID: imageID,
})
node = AddLabels(node, image.Labels)
Expand All @@ -157,8 +157,7 @@ func (r *Reporter) containerImageTopology() report.Topology {
node = node.WithLatests(map[string]string{ImageName: image.RepoTags[0]})
}

nodeID := report.MakeContainerImageNodeID(imageID)
result.AddNode(nodeID, node)
result.AddNode(node)
})

return result
Expand Down
3 changes: 2 additions & 1 deletion probe/docker/tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ func (t *Tagger) tag(tree process.Tree, topology *report.Topology) {
continue
}

topology.AddNode(nodeID, report.MakeNodeWith(map[string]string{
topology.AddNode(report.MakeNodeWith(nodeID, map[string]string{
ContainerID: c.ID(),
}).WithParents(report.EmptySets.
Add(report.Container, report.MakeStringSet(report.MakeContainerNodeID(c.ID()))).
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(c.Image()))),
))

}
}
4 changes: 2 additions & 2 deletions probe/docker/tagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestTagger(t *testing.T) {
)

input := report.MakeReport()
input.Process.AddNode(pid1NodeID, report.MakeNodeWith(map[string]string{process.PID: "2"}))
input.Process.AddNode(pid2NodeID, report.MakeNodeWith(map[string]string{process.PID: "3"}))
input.Process.AddNode(report.MakeNodeWith(pid1NodeID, map[string]string{process.PID: "2"}))
input.Process.AddNode(report.MakeNodeWith(pid2NodeID, map[string]string{process.PID: "3"}))

have, err := docker.NewTagger(mockRegistryInstance, nil).Tag(input)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion probe/endpoint/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ func (n natMapper) applyNAT(rpt report.Report, scope string) {
return
}

rpt.Endpoint.AddNode(copyEndpointID, node.WithLatests(map[string]string{
rpt.Endpoint.AddNode(node.WithID(copyEndpointID).WithLatests(map[string]string{
Addr: mapping.rewrittenIP,
Port: copyEndpointPort,
"copy_of": realEndpointID,
}))

})
}
9 changes: 5 additions & 4 deletions probe/endpoint/nat_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ func TestNat(t *testing.T) {

have := report.MakeReport()
originalID := report.MakeEndpointNodeID("host1", "10.0.47.1", "80")
have.Endpoint.AddNode(originalID, report.MakeNodeWith(map[string]string{
have.Endpoint.AddNode(report.MakeNodeWith(originalID, map[string]string{
Addr: "10.0.47.1",
Port: "80",
"foo": "bar",
}))

want := have.Copy()
want.Endpoint.AddNode(report.MakeEndpointNodeID("host1", "1.2.3.4", "80"), report.MakeNodeWith(map[string]string{
wantID := report.MakeEndpointNodeID("host1", "1.2.3.4", "80")
want.Endpoint.AddNode(report.MakeNodeWith(wantID, map[string]string{
Addr: "1.2.3.4",
Port: "80",
"copy_of": originalID,
Expand All @@ -76,14 +77,14 @@ func TestNat(t *testing.T) {

have := report.MakeReport()
originalID := report.MakeEndpointNodeID("host2", "10.0.47.2", "22222")
have.Endpoint.AddNode(originalID, report.MakeNodeWith(map[string]string{
have.Endpoint.AddNode(report.MakeNodeWith(originalID, map[string]string{
Addr: "10.0.47.2",
Port: "22222",
"foo": "baz",
}))

want := have.Copy()
want.Endpoint.AddNode(report.MakeEndpointNodeID("host2", "2.3.4.5", "22223"), report.MakeNodeWith(map[string]string{
want.Endpoint.AddNode(report.MakeNodeWith(report.MakeEndpointNodeID("host2", "2.3.4.5", "22223"), map[string]string{
Addr: "2.3.4.5",
Port: "22223",
"copy_of": originalID,
Expand Down
Loading

0 comments on commit 1edeb8d

Please sign in to comment.