From 4b1ab76695691a8bf79c2418c7be5fa4a2213ca0 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Sun, 24 Dec 2017 15:18:01 +0000 Subject: [PATCH] render sensible labels for parent nodes with little/no metadata We eliminate the custom parent renderer, which was a very partial implementation MakeBasicNodeSummary. We also ensure that parents are always rendered in the same, sensible order. Previously the order was an alphabetic sort of the parent topology IDs. Now lower level topologies come before higher level topologies. --- render/detailed/node_test.go | 20 ++++----- render/detailed/parents.go | 79 ++++++++++----------------------- render/detailed/parents_test.go | 6 +-- 3 files changed, 37 insertions(+), 68 deletions(-) diff --git a/render/detailed/node_test.go b/render/detailed/node_test.go index 63c9ce7100..1a43fc6f6a 100644 --- a/render/detailed/node_test.go +++ b/render/detailed/node_test.go @@ -229,16 +229,16 @@ func TestMakeDetailedContainerNode(t *testing.T) { Label: fixture.ServerContainerImageName, TopologyID: "containers-by-image", }, - { - ID: fixture.ServerHostNodeID, - Label: fixture.ServerHostName, - TopologyID: "hosts", - }, { ID: fixture.ServerPodNodeID, Label: "pong-b", TopologyID: "pods", }, + { + ID: fixture.ServerHostNodeID, + Label: "server", + TopologyID: "hosts", + }, }, }, Controls: []detailed.ControlInstance{}, @@ -339,16 +339,16 @@ func TestMakeDetailedPodNode(t *testing.T) { {ID: "kubernetes_namespace", Label: "Namespace", Value: "ping", Priority: 5}, }, Parents: []detailed.Parent{ - { - ID: fixture.ServerHostNodeID, - Label: fixture.ServerHostName, - TopologyID: "hosts", - }, { ID: fixture.ServiceNodeID, Label: fixture.ServiceName, TopologyID: "services", }, + { + ID: fixture.ServerHostNodeID, + Label: "server", + TopologyID: "hosts", + }, }, }, Controls: []detailed.ControlInstance{}, diff --git a/render/detailed/parents.go b/render/detailed/parents.go index e7fa2ccca8..0ad1a44c4a 100644 --- a/render/detailed/parents.go +++ b/render/detailed/parents.go @@ -1,12 +1,6 @@ package detailed import ( - "sort" - - "github.com/weaveworks/scope/probe/awsecs" - "github.com/weaveworks/scope/probe/docker" - "github.com/weaveworks/scope/probe/host" - "github.com/weaveworks/scope/probe/kubernetes" "github.com/weaveworks/scope/report" ) @@ -17,24 +11,21 @@ type Parent struct { TopologyID string `json:"topologyId"` } -var ( - kubernetesParentLabel = latestLookup(kubernetes.Name) - - getLabelForTopology = map[string]func(report.Node) string{ - report.Container: getRenderableContainerName, - report.Pod: kubernetesParentLabel, - report.Deployment: kubernetesParentLabel, - report.DaemonSet: kubernetesParentLabel, - report.StatefulSet: kubernetesParentLabel, - report.CronJob: kubernetesParentLabel, - report.Service: kubernetesParentLabel, - report.ECSTask: latestLookup(awsecs.TaskFamily), - report.ECSService: ecsServiceParentLabel, - report.SwarmService: latestLookup(docker.ServiceName), - report.ContainerImage: containerImageParentLabel, - report.Host: latestLookup(host.HostName), - } -) +// parent topologies, in the order we want to show them +var parentTopologies = []string{ + report.Container, + report.ContainerImage, + report.Pod, + report.Deployment, + report.DaemonSet, + report.StatefulSet, + report.CronJob, + report.Service, + report.ECSTask, + report.ECSService, + report.SwarmService, + report.Host, +} // Parents renders the parents of this report.Node, which have been aggregated // from the probe reports. @@ -43,13 +34,7 @@ func Parents(r report.Report, n report.Node) []Parent { return nil } result := make([]Parent, 0, n.Parents.Size()) - topologyIDs := []string{} - for topologyID := range getLabelForTopology { - topologyIDs = append(topologyIDs, topologyID) - } - sort.Strings(topologyIDs) - for _, topologyID := range topologyIDs { - getLabel := getLabelForTopology[topologyID] + for _, topologyID := range parentTopologies { topology, ok := r.Topology(topologyID) if !ok { continue @@ -63,7 +48,7 @@ func Parents(r report.Report, n report.Node) []Parent { var parentNode report.Node // Special case: container image parents should be empty nodes for some reason if topologyID == report.ContainerImage { - parentNode = report.MakeNode(id) + parentNode = report.MakeNode(id).WithTopology(topologyID) } else { if parent, ok := topology.Nodes[id]; ok { parentNode = parent @@ -76,12 +61,13 @@ func Parents(r report.Report, n report.Node) []Parent { if !ok { continue } - - result = append(result, Parent{ - ID: id, - Label: getLabel(parentNode), - TopologyID: apiTopologyID, - }) + if summary, ok := MakeBasicNodeSummary(r, parentNode); ok { + result = append(result, Parent{ + ID: summary.ID, + Label: summary.Label, + TopologyID: apiTopologyID, + }) + } } } if len(result) == 0 { @@ -89,20 +75,3 @@ func Parents(r report.Report, n report.Node) []Parent { } return result } - -func latestLookup(key string) func(report.Node) string { - return func(n report.Node) string { - value, _ := n.Latest.Lookup(key) - return value - } -} - -func ecsServiceParentLabel(n report.Node) string { - _, name, _ := report.ParseECSServiceNodeID(n.ID) - return name -} - -func containerImageParentLabel(n report.Node) string { - name, _ := report.ParseContainerImageNodeID(n.ID) - return name -} diff --git a/render/detailed/parents_test.go b/render/detailed/parents_test.go index 1a6cdf1d37..3332d651da 100644 --- a/render/detailed/parents_test.go +++ b/render/detailed/parents_test.go @@ -34,7 +34,7 @@ func TestParents(t *testing.T) { name: "Container image", node: render.ContainerImageRenderer.Render(fixture.Report).Nodes[expected.ClientContainerImageNodeID], want: []detailed.Parent{ - {ID: fixture.ClientHostNodeID, Label: fixture.ClientHostName, TopologyID: "hosts"}, + {ID: fixture.ClientHostNodeID, Label: "client", TopologyID: "hosts"}, }, }, { @@ -42,15 +42,15 @@ func TestParents(t *testing.T) { node: render.ContainerWithImageNameRenderer.Render(fixture.Report).Nodes[fixture.ClientContainerNodeID], want: []detailed.Parent{ {ID: expected.ClientContainerImageNodeID, Label: fixture.ClientContainerImageName, TopologyID: "containers-by-image"}, - {ID: fixture.ClientHostNodeID, Label: fixture.ClientHostName, TopologyID: "hosts"}, {ID: fixture.ClientPodNodeID, Label: "pong-a", TopologyID: "pods"}, + {ID: fixture.ClientHostNodeID, Label: "client", TopologyID: "hosts"}, }, }, { node: render.ProcessRenderer.Render(fixture.Report).Nodes[fixture.ClientProcess1NodeID], want: []detailed.Parent{ {ID: fixture.ClientContainerNodeID, Label: fixture.ClientContainerName, TopologyID: "containers"}, - {ID: fixture.ClientHostNodeID, Label: fixture.ClientHostName, TopologyID: "hosts"}, + {ID: fixture.ClientHostNodeID, Label: "client", TopologyID: "hosts"}, }, }, } {