render/detailed/parents: Refactor for less repeated information

We replace the existing data structure with a simpler one that
only specifies how to get the parent label, which is the only
part of the Parent struct that can't be generated from the node info alone.

Future work: Standardize this concept of a label and put it in the topology instead.
Though that already exists...so just use it?
This commit is contained in:
Mike Lang
2017-04-03 15:37:35 -07:00
parent 2a74883cce
commit 9c88ad85e9

View File

@@ -16,117 +16,82 @@ type Parent struct {
TopologyID string `json:"topologyId"`
}
func node(t report.Topology) func(string) (report.Node, bool) {
return func(id string) (report.Node, bool) {
n, ok := t.Nodes[id]
return n, ok
}
}
var (
kubernetesParentLabel = latestLookup(kubernetes.Name)
func fake(id string) (report.Node, bool) {
return report.MakeNode(id), true
}
getLabelForTopology = map[string]func(report.Node) string{
report.Container: getRenderableContainerName,
report.Pod: kubernetesParentLabel,
report.ReplicaSet: kubernetesParentLabel,
report.Deployment: kubernetesParentLabel,
report.Service: kubernetesParentLabel,
report.ECSTask: latestLookup(awsecs.TaskFamily),
report.ECSService: ecsServiceParentLabel,
report.ContainerImage: containerImageParentLabel,
report.Host: latestLookup(host.HostName),
}
)
// Parents renders the parents of this report.Node, which have been aggregated
// from the probe reports.
func Parents(r report.Report, n report.Node) (result []Parent) {
topologies := map[string]struct {
node func(id string) (report.Node, bool)
render func(report.Node) Parent
}{
report.Container: {node(r.Container), containerParent},
report.Pod: {node(r.Pod), podParent},
report.ReplicaSet: {node(r.ReplicaSet), replicaSetParent},
report.Deployment: {node(r.Deployment), deploymentParent},
report.Service: {node(r.Service), serviceParent},
report.ECSTask: {node(r.ECSTask), ecsTaskParent},
report.ECSService: {node(r.ECSService), ecsServiceParent},
report.ContainerImage: {fake, containerImageParent},
report.Host: {node(r.Host), hostParent},
}
topologyIDs := []string{}
for topologyID := range topologies {
for topologyID := range getLabelForTopology {
topologyIDs = append(topologyIDs, topologyID)
}
sort.Strings(topologyIDs)
for _, topologyID := range topologyIDs {
t := topologies[topologyID]
getLabel := getLabelForTopology[topologyID]
topology, ok := r.Topology(topologyID)
if !ok {
continue
}
parents, _ := n.Parents.Lookup(topologyID)
for _, id := range parents {
if topologyID == n.Topology && id == n.ID {
continue
}
parent, ok := t.node(id)
var parentNode report.Node
// Special case: container image parents should be empty nodes for some reason
if topologyID == report.ContainerImage {
parentNode = report.MakeNode(id)
} else {
if parent, ok := topology.Nodes[id]; ok {
parentNode = parent
} else {
continue
}
}
apiTopologyID, ok := primaryAPITopology[topologyID]
if !ok {
continue
}
result = append(result, t.render(parent))
result = append(result, Parent{
ID: id,
Label: getLabel(parentNode),
TopologyID: apiTopologyID,
})
}
}
return result
}
func containerParent(n report.Node) Parent {
label := getRenderableContainerName(n)
return Parent{
ID: n.ID,
Label: label,
TopologyID: "containers",
func latestLookup(key string) func(report.Node) string {
return func(n report.Node) string {
value, _ := n.Latest.Lookup(key)
return value
}
}
var (
podParent = kubernetesParent("pods")
replicaSetParent = kubernetesParent("replica-sets")
deploymentParent = kubernetesParent("deployments")
serviceParent = kubernetesParent("services")
)
func kubernetesParent(topology string) func(report.Node) Parent {
return func(n report.Node) Parent {
name, _ := n.Latest.Lookup(kubernetes.Name)
return Parent{
ID: n.ID,
Label: name,
TopologyID: topology,
}
}
}
func ecsTaskParent(n report.Node) Parent {
family, _ := n.Latest.Lookup(awsecs.TaskFamily)
return Parent{
ID: n.ID,
Label: family,
TopologyID: "ecs-tasks",
}
}
func ecsServiceParent(n report.Node) Parent {
func ecsServiceParentLabel(n report.Node) string {
_, name, _ := report.ParseECSServiceNodeID(n.ID)
return Parent{
ID: n.ID,
Label: name,
TopologyID: "ecs-services",
}
return name
}
func containerImageParent(n report.Node) Parent {
func containerImageParentLabel(n report.Node) string {
name, _ := report.ParseContainerImageNodeID(n.ID)
return Parent{
ID: n.ID,
Label: name,
TopologyID: "containers-by-image",
}
}
func hostParent(n report.Node) Parent {
hostName, _ := n.Latest.Lookup(host.HostName)
return Parent{
ID: n.ID,
Label: hostName,
TopologyID: "hosts",
}
return name
}