From bbce9e3d69f2ab49e4170fb4eeff205321a85a57 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 28 Oct 2019 08:57:52 +0000 Subject: [PATCH 1/4] refactor: simplify ContainerImageRenderer Crunch a Map and Reduce into a single Renderer that has the same function. --- render/container.go | 61 +++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/render/container.go b/render/container.go index 5e78fddfa..e6d533aa8 100644 --- a/render/container.go +++ b/render/container.go @@ -157,18 +157,12 @@ func (r containerWithImageNameRenderer) Render(ctx context.Context, rpt report.R // graph where the ranks are the image names, not their IDs var ContainerWithImageNameRenderer = Memoise(containerWithImageNameRenderer{ContainerRenderer}) -// ContainerImageRenderer is a Renderer which produces a renderable container -// image graph by merging the container graph and the container image topology. +// ContainerImageRenderer produces a graph where each node is a container image +// with the original containers as children var ContainerImageRenderer = Memoise(FilterEmpty(report.Container, MakeMap( MapContainerImage2Name, - MakeReduce( - MakeMap( - MapContainer2ContainerImage, - ContainerWithImageNameRenderer, - ), - SelectContainerImage, - ), + containerImageRenderer{}, ), )) @@ -276,37 +270,28 @@ func MapProcess2Container(n report.Node) report.Node { return node } -// MapContainer2ContainerImage maps container Nodes to container -// image Nodes. -// -// Pseudo nodes are passed straight through. -// -// If this function is given a node without a docker_image_id -// it will drop that node. -// -// Otherwise, this function will produce a node with the correct ID -// format for a container image, but without any Major or Minor -// labels. It does not have enough info to do that, and the resulting -// graph must be merged with a container image graph to get that info. -func MapContainer2ContainerImage(n report.Node) report.Node { - // Propagate all pseudo nodes - if n.Topology == Pseudo { - return n - } +// containerImageRenderer produces a graph where each node is a container image +// with the original containers as children +type containerImageRenderer struct{} - // Otherwise, if some some reason the container doesn't have a image_id - // (maybe slightly out of sync reports), just drop it - imageID, ok := n.Latest.Lookup(report.DockerImageID) - if !ok { - return report.Node{} - } +func (m containerImageRenderer) Render(ctx context.Context, rpt report.Report) Nodes { + containers := ContainerWithImageNameRenderer.Render(ctx, rpt) + ret := newJoinResults(rpt.ContainerImage.Nodes) - // Add container id key to the counters, which will later be - // counted to produce the minor label - id := report.MakeContainerImageNodeID(imageID) - result := NewDerivedNode(id, n).WithTopology(report.ContainerImage) - result.Counters = result.Counters.Add(n.Topology, 1) - return result + for _, n := range containers.Nodes { + if n.Topology == Pseudo { + ret.passThrough(n) + continue + } + // If some some reason the container doesn't have a image_id, just drop it + imageID, ok := n.Latest.Lookup(report.DockerImageID) + if !ok { + continue + } + id := report.MakeContainerImageNodeID(imageID) + ret.addChildAndChildren(n, id, report.ContainerImage) + } + return ret.result(containers) } func containerImageNodeID(n report.Node) string { From 875fdba35254cc67f2f400eee7127fde43095df1 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 1 Nov 2019 19:14:35 +0000 Subject: [PATCH 2/4] rendering: simplify ContainerHostnameRenderer The second half of the render added all the same nodes, except for those with no Docker image information, so we could show a figure for those filtered out on that basis. This just isn't worth the effort. --- render/container.go | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/render/container.go b/render/container.go index e6d533aa8..791bc299d 100644 --- a/render/container.go +++ b/render/container.go @@ -171,20 +171,9 @@ var ContainerImageRenderer = Memoise(FilterEmpty(report.Container, // // not memoised var ContainerHostnameRenderer = FilterEmpty(report.Container, - MakeReduce( - MakeMap( - MapContainer2Hostname, - ContainerWithImageNameRenderer, - ), - // Grab *all* the hostnames, so we can count the number which were empty - // for accurate stats. - MakeMap( - MapToEmpty, - MakeMap( - MapContainer2Hostname, - ContainerRenderer, - ), - ), + MakeMap( + MapContainer2Hostname, + ContainerWithImageNameRenderer, ), ) From f732eed3a5ff4aa3c066280997b7c2ef5b2dea95 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sun, 27 Oct 2019 14:27:44 +0000 Subject: [PATCH 3/4] refactor: remove unused parameterisation of containerWithImageNameRenderer --- render/container.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/render/container.go b/render/container.go index 791bc299d..da536accb 100644 --- a/render/container.go +++ b/render/container.go @@ -116,14 +116,12 @@ func HasChildren(topology string) FilterFunc { } } -type containerWithImageNameRenderer struct { - Renderer -} +type containerWithImageNameRenderer struct{} // Render produces a container graph where the the latest metadata contains the // container image name, if found. func (r containerWithImageNameRenderer) Render(ctx context.Context, rpt report.Report) Nodes { - containers := r.Renderer.Render(ctx, rpt) + containers := ContainerRenderer.Render(ctx, rpt) images := SelectContainerImage.Render(ctx, rpt) outputs := make(report.Nodes, len(containers.Nodes)) @@ -155,7 +153,7 @@ func (r containerWithImageNameRenderer) Render(ctx context.Context, rpt report.R // ContainerWithImageNameRenderer is a Renderer which produces a container // graph where the ranks are the image names, not their IDs -var ContainerWithImageNameRenderer = Memoise(containerWithImageNameRenderer{ContainerRenderer}) +var ContainerWithImageNameRenderer = Memoise(containerWithImageNameRenderer{}) // ContainerImageRenderer produces a graph where each node is a container image // with the original containers as children From 8aca4d6452b49320f23f1e9e0af2d5f99caaee9e Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 18 Feb 2020 12:14:18 +0000 Subject: [PATCH 4/4] Remove MapToEmpty() - no longer used --- render/container.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/render/container.go b/render/container.go index da536accb..f40ca8227 100644 --- a/render/container.go +++ b/render/container.go @@ -330,9 +330,3 @@ func MapContainer2Hostname(n report.Node) report.Node { node.Counters = node.Counters.Add(n.Topology, 1) return node } - -// MapToEmpty removes all the attributes, children, etc, of a node. Useful when -// we just want to count the presence of nodes. -func MapToEmpty(n report.Node) report.Node { - return report.MakeNode(n.ID).WithTopology(n.Topology) -}