If k8s objects only have one container, show that container's metrics on them (#1473)

This commit is contained in:
Paul Bellamy
2016-06-04 08:03:51 +01:00
committed by Tom Wilkie
parent 1bd8eb218d
commit 85aee27ef6
4 changed files with 235 additions and 20 deletions
+24
View File
@@ -0,0 +1,24 @@
package render
import (
"github.com/weaveworks/scope/report"
)
// PropagateSingleMetrics puts metrics from one of the children onto the parent
// iff there is only one child of that type.
func PropagateSingleMetrics(topology string) MapFunc {
return func(n report.Node, _ report.Networks) report.Nodes {
var found []report.Node
n.Children.ForEach(func(child report.Node) {
if child.Topology == topology {
if _, ok := child.Latest.Lookup(report.DoesNotMakeConnections); !ok {
found = append(found, child)
}
}
})
if len(found) == 1 {
n = n.WithMetrics(found[0].Metrics)
}
return report.Nodes{n.ID: n}
}
}
+167
View File
@@ -0,0 +1,167 @@
package render_test
import (
"testing"
"time"
"github.com/weaveworks/scope/render"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test"
"github.com/weaveworks/scope/test/reflect"
)
func TestPropagateSingleMetrics(t *testing.T) {
now := time.Now()
for _, c := range []struct {
name string
input report.Node
topology string
output report.Nodes
}{
{
name: "empty",
input: report.MakeNode("empty"),
topology: "",
output: report.Nodes{"empty": report.MakeNode("empty")},
},
{
name: "one child",
input: report.MakeNode("a").WithChildren(
report.MakeNodeSet(
report.MakeNode("child1").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}),
),
),
topology: report.Container,
output: report.Nodes{
"a": report.MakeNode("a").WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}).WithChildren(
report.MakeNodeSet(
report.MakeNode("child1").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}),
),
),
},
},
{
name: "ignores other topologies",
input: report.MakeNode("a").WithChildren(
report.MakeNodeSet(
report.MakeNode("child1").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}),
report.MakeNode("child2").
WithTopology("otherTopology").
WithMetrics(report.Metrics{
"metric2": report.MakeMetric(),
}),
),
),
topology: report.Container,
output: report.Nodes{
"a": report.MakeNode("a").WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}).WithChildren(
report.MakeNodeSet(
report.MakeNode("child1").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}),
report.MakeNode("child2").
WithTopology("otherTopology").
WithMetrics(report.Metrics{
"metric2": report.MakeMetric(),
}),
),
),
},
},
{
name: "two children",
input: report.MakeNode("a").WithChildren(
report.MakeNodeSet(
report.MakeNode("child1").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}),
report.MakeNode("child2").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric2": report.MakeMetric(),
}),
),
),
topology: report.Container,
output: report.Nodes{
"a": report.MakeNode("a").WithChildren(
report.MakeNodeSet(
report.MakeNode("child1").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}),
report.MakeNode("child2").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric2": report.MakeMetric(),
}),
),
),
},
},
{
name: "ignores k8s pause container",
input: report.MakeNode("a").WithChildren(
report.MakeNodeSet(
report.MakeNode("child1").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}),
report.MakeNode("child2").
WithLatest(report.DoesNotMakeConnections, now, "").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric2": report.MakeMetric(),
}),
),
),
topology: report.Container,
output: report.Nodes{
"a": report.MakeNode("a").WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}).WithChildren(
report.MakeNodeSet(
report.MakeNode("child1").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric1": report.MakeMetric(),
}),
report.MakeNode("child2").
WithLatest(report.DoesNotMakeConnections, now, "").
WithTopology(report.Container).
WithMetrics(report.Metrics{
"metric2": report.MakeMetric(),
}),
),
),
},
},
} {
got := render.PropagateSingleMetrics(c.topology)(c.input, report.Networks{})
if !reflect.DeepEqual(got, c.output) {
t.Errorf("[%s] Diff: %s", c.name, test.Diff(c.output, got))
}
}
}
+32 -20
View File
@@ -26,12 +26,15 @@ var PodRenderer = ConditionalRenderer(renderKubernetesTopologies,
state, ok := n.Latest.Lookup(kubernetes.State)
return (!ok || state != kubernetes.StateDeleted)
},
MakeReduce(
MakeMap(
MapContainer2Pod,
ContainerWithImageNameRenderer,
MakeMap(
PropagateSingleMetrics(report.Container),
MakeReduce(
MakeMap(
MapContainer2Pod,
ContainerWithImageNameRenderer,
),
SelectPod,
),
SelectPod,
),
)),
)
@@ -40,12 +43,15 @@ var PodRenderer = ConditionalRenderer(renderKubernetesTopologies,
// graph by merging the pods graph and the services topology.
var PodServiceRenderer = ConditionalRenderer(renderKubernetesTopologies,
ApplyDecorators(
MakeReduce(
MakeMap(
Map2Service,
PodRenderer,
MakeMap(
PropagateSingleMetrics(report.Pod),
MakeReduce(
MakeMap(
Map2Service,
PodRenderer,
),
SelectService,
),
SelectService,
),
),
)
@@ -54,12 +60,15 @@ var PodServiceRenderer = ConditionalRenderer(renderKubernetesTopologies,
// graph by merging the pods graph and the deployments topology.
var DeploymentRenderer = ConditionalRenderer(renderKubernetesTopologies,
ApplyDecorators(
MakeReduce(
MakeMap(
Map2Deployment,
ReplicaSetRenderer,
MakeMap(
PropagateSingleMetrics(report.ReplicaSet),
MakeReduce(
MakeMap(
Map2Deployment,
ReplicaSetRenderer,
),
SelectDeployment,
),
SelectDeployment,
),
),
)
@@ -68,12 +77,15 @@ var DeploymentRenderer = ConditionalRenderer(renderKubernetesTopologies,
// graph by merging the pods graph and the replica sets topology.
var ReplicaSetRenderer = ConditionalRenderer(renderKubernetesTopologies,
ApplyDecorators(
MakeReduce(
MakeMap(
Map2ReplicaSet,
PodRenderer,
MakeMap(
PropagateSingleMetrics(report.Pod),
MakeReduce(
MakeMap(
Map2ReplicaSet,
PodRenderer,
),
SelectReplicaSet,
),
SelectReplicaSet,
),
),
)