Clarify use of 'id' in addToResults

Pass 'id' through to the create function and expect that the result Node has that ID.
Extract a function newPseudoNode for common calls.
This commit is contained in:
Bryan Boreham
2017-11-06 21:22:33 +00:00
parent 322aa76e02
commit cbba3c0fd3
4 changed files with 14 additions and 16 deletions

View File

@@ -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
}
}
@@ -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]
})
}

View File

@@ -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)
})
@@ -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

View File

@@ -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 {

View File

@@ -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)
@@ -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
}