Skip to content

Commit

Permalink
Clarify use of 'id' in addToResults
Browse files Browse the repository at this point in the history
Pass 'id' through to the create function and expect that the result Node has that ID.
Extract a function newPseudoNode for common calls.
  • Loading branch information
bboreham committed Nov 6, 2017
1 parent 8f22634 commit 4f29349
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
6 changes: 2 additions & 4 deletions render/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ func (c connectionJoin) Render(rpt report.Report, dct Decorator) report.Nodes {
// Nodes without a hostid may be pseudo nodes - if so, pass through to result
if _, ok := m.Latest.Lookup(report.HostNodeID); !ok {
if id, ok := externalNodeID(m, addr, local); ok {
addToResults(m, id, ret, mapped, func() report.Node {
return report.MakeNode(id).WithTopology(Pseudo)
})
addToResults(m, id, ret, mapped, newPseudoNode)
continue
}
}
Expand All @@ -97,7 +95,7 @@ func (c connectionJoin) Render(rpt report.Report, dct Decorator) report.Nodes {
id, found = ipNodes[report.MakeScopedEndpointNodeID(scope, addr, port)]
}
if found && id != "" { // not one we blanked out earlier
addToResults(m, id, ret, mapped, func() report.Node {
addToResults(m, id, ret, mapped, func(id string) report.Node {
return inputNodes[id]
})
}
Expand Down
14 changes: 6 additions & 8 deletions render/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ func (e endpoints2Hosts) Render(rpt report.Report, dct Decorator) report.Nodes {
if !ok {
continue
}
addToResults(n, id, ret, mapped, func() report.Node {
return report.MakeNode(id).WithTopology(Pseudo)
})
addToResults(n, id, ret, mapped, newPseudoNode)
} else {
id := report.MakeHostNodeID(report.ExtractHostID(n))
addToResults(n, id, ret, mapped, func() report.Node {
addToResults(n, id, ret, mapped, func(id string) report.Node {
return report.MakeNode(id).WithTopology(report.Host).
WithLatest(report.HostNodeID, timestamp, hostNodeID)
})
Expand All @@ -98,15 +96,15 @@ func (e endpoints2Hosts) Render(rpt report.Report, dct Decorator) report.Nodes {
// Add Node M to the result set ret under id, creating a new result
// node if not already there, and updating the old-id to new-id mapping
// Note we do not update any counters for child topologies here
func addToResults(m report.Node, id string, ret report.Nodes, mapped map[string]string, create func() report.Node) {
func addToResults(m report.Node, id string, ret report.Nodes, mapped map[string]string, create func(string) report.Node) {
result, exists := ret[id]
if !exists {
result = create()
result = create(id)
}
result.Children = result.Children.Add(m)
result.Children = result.Children.Merge(m.Children)
ret[result.ID] = result
mapped[m.ID] = result.ID
ret[id] = result
mapped[m.ID] = id
}

// Rewrite Adjacency for new nodes in ret, original nodes in input, and mapping old->new IDs in mapped
Expand Down
4 changes: 4 additions & 0 deletions render/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func NewDerivedPseudoNode(id string, node report.Node) report.Node {
return output
}

func newPseudoNode(id string) report.Node {
return report.MakeNode(id).WithTopology(Pseudo)
}

func pseudoNodeID(n report.Node, local report.Networks) (string, bool) {
_, addr, _, ok := report.ParseEndpointNodeID(n.ID)
if !ok {
Expand Down
6 changes: 2 additions & 4 deletions render/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ func (e endpoints2Processes) Render(rpt report.Report, dct Decorator) report.Nod
// Nodes without a hostid are treated as pseudo nodes
if hostNodeID, ok := n.Latest.Lookup(report.HostNodeID); !ok {
if id, ok := pseudoNodeID(n, local); ok {
addToResults(n, id, ret, mapped, func() report.Node {
return report.MakeNode(id).WithTopology(Pseudo)
})
addToResults(n, id, ret, mapped, newPseudoNode)
}
} else {
pid, timestamp, ok := n.Latest.LookupEntry(process.PID)
Expand All @@ -119,7 +117,7 @@ func (e endpoints2Processes) Render(rpt report.Report, dct Decorator) report.Nod

hostID, _, _ := report.ParseNodeID(hostNodeID)
id := report.MakeProcessNodeID(hostID, pid)
addToResults(n, id, ret, mapped, func() report.Node {
addToResults(n, id, ret, mapped, func(id string) report.Node {
if processNode, found := processes[id]; found {
return processNode
}
Expand Down

0 comments on commit 4f29349

Please sign in to comment.