AddParent() function to optimise the case where there is only one parent

Avoids creating and discarding a StringSet just to carry the
parameters, and makes the code more readable too.
This commit is contained in:
Bryan Boreham
2018-07-17 13:51:46 +00:00
parent 0d2409c72c
commit 5edc85822b
7 changed files with 14 additions and 18 deletions
+1 -1
View File
@@ -187,7 +187,7 @@ func (r Reporter) Tag(rpt report.Report) (report.Report, error) {
serviceID := report.MakeECSServiceNodeID(cluster, serviceName)
parentsSets = parentsSets.AddString(report.ECSService, serviceID)
// in addition, make service parent of task
rpt.ECSTask.Nodes[taskID] = rpt.ECSTask.Nodes[taskID].WithParents(report.MakeSets().Add(report.ECSService, report.MakeStringSet(serviceID)))
rpt.ECSTask.Nodes[taskID] = rpt.ECSTask.Nodes[taskID].WithParent(report.ECSService, serviceID)
}
for _, containerID := range info.ContainerIDs {
if containerNode, ok := rpt.Container.Nodes[containerID]; ok {
+1 -3
View File
@@ -407,9 +407,7 @@ func (c *container) getBaseNode() report.Node {
ContainerCommand: c.getSanitizedCommand(),
ImageID: c.Image(),
ContainerHostname: c.Hostname(),
}).WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(c.Image()))),
)
}).WithParent(report.ContainerImage, report.MakeContainerImageNodeID(c.Image()))
result = result.AddPrefixPropertyList(LabelPrefix, c.container.Config.Labels)
if !c.noEnvironmentVariables {
result = result.AddPrefixPropertyList(EnvPrefix, c.env())
+3 -7
View File
@@ -74,7 +74,7 @@ func (t *Tagger) Tag(r report.Report) (report.Report, error) {
})
r.SwarmService.AddNode(node)
r.Container.Nodes[containerID] = container.WithParents(container.Parents.Add(report.SwarmService, report.MakeStringSet(nodeID)))
r.Container.Nodes[containerID] = container.WithParent(report.SwarmService, nodeID)
}
return r, nil
@@ -116,17 +116,13 @@ func (t *Tagger) tag(tree process.Tree, topology *report.Topology) {
}
node = node.WithLatest(ContainerID, mtime.Now(), c.ID())
node = node.WithParents(report.MakeSets().
Add(report.Container, report.MakeStringSet(report.MakeContainerNodeID(c.ID()))),
)
node = node.WithParent(report.Container, report.MakeContainerNodeID(c.ID()))
// If we can work out the image name, add a parent tag for it
image, ok := t.registry.GetContainerImage(c.Image())
if ok && len(image.RepoTags) > 0 {
imageName := ImageNameWithoutTag(image.RepoTags[0])
node = node.WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(imageName))),
)
node = node.WithParent(report.ContainerImage, report.MakeContainerImageNodeID(imageName))
}
topology.ReplaceNode(node)
+1 -2
View File
@@ -26,14 +26,13 @@ func (Tagger) Name() string { return "Host" }
func (t Tagger) Tag(r report.Report) (report.Report, error) {
var (
metadata = map[string]string{report.HostNodeID: t.hostNodeID}
parents = report.MakeSets().Add(report.Host, report.MakeStringSet(t.hostNodeID))
)
// Explicitly don't tag Endpoints, Addresses and Overlay nodes - These topologies include pseudo nodes,
// and as such do their own host tagging.
for _, topology := range []report.Topology{r.Process, r.Container, r.ContainerImage, r.Host, r.Pod} {
for _, node := range topology.Nodes {
topology.ReplaceNode(node.WithLatests(metadata).WithParents(parents))
topology.ReplaceNode(node.WithLatests(metadata).WithParent(report.Host, t.hostNodeID))
}
}
return r, nil
+1 -4
View File
@@ -249,10 +249,7 @@ func (r *Reporter) Tag(rpt report.Report) (report.Report, error) {
n = n.WithLatest(report.DoesNotMakeConnections, mtime.Now(), "")
}
rpt.Container.Nodes[id] = n.WithParents(report.MakeSets().Add(
report.Pod,
report.MakeStringSet(report.MakePodNodeID(uid)),
))
rpt.Container.Nodes[id] = n.WithParent(report.Pod, report.MakePodNodeID(uid))
}
return rpt, nil
}
+1 -1
View File
@@ -330,7 +330,7 @@ func (w *Weave) addCurrentPeerInfo(latests map[string]string, node report.Node)
latests[WeavePluginDriver] = w.statusCache.Plugin.DriverName
}
node = node.AddPrefixMulticolumnTable(WeaveConnectionsMulticolumnTablePrefix, getConnectionsTable(w.statusCache.Router))
node = node.WithParents(report.MakeSets().Add(report.Host, report.MakeStringSet(w.hostID)))
node = node.WithParent(report.Host, w.hostID)
return latests, node
}