Files
weave-scope/render/endpoint.go
Matthias Radestock fd5fa7a962 refactor: extract common code in endpoint mapping
This isn't quite as neat as we'd want to make it, because two of the
three call sites still require a closure, but it's still an
improvement.

Note that every instance of MapEndpoints only ever maps to one
topology, which, furthermore, is a core report topology. Hence we can
just parameterise MapEndpoints with that topology, and then the map
functions can return just the node ids.
2018-01-07 08:20:06 +00:00

50 lines
1.4 KiB
Go

package render
import (
"github.com/weaveworks/scope/report"
)
// Pseudo is the topology for nodes that aren't "real" nodes inside a
// cluster, such as nodes representing the internet, external
// services, and artificial grouping such as "uncontained processes"
// and "unmanaged containers".
const Pseudo = "pseudo"
// EndpointRenderer is a Renderer which produces a renderable endpoint graph.
var EndpointRenderer = SelectEndpoint
type endpointMapFunc func(report.Node) string
type mapEndpoints struct {
f endpointMapFunc
topology string
}
// MapEndpoints creates a renderer for the endpoint topology. Each
// endpoint is either turned into a pseudo node, or mapped to a node
// in the specified topology by the supplied function.
func MapEndpoints(f endpointMapFunc, topology string) Renderer {
return mapEndpoints{f: f, topology: topology}
}
func (e mapEndpoints) Render(rpt report.Report) Nodes {
local := LocalNetworks(rpt)
endpoints := SelectEndpoint.Render(rpt)
ret := newJoinResults(TopologySelector(e.topology).Render(rpt).Nodes)
for _, n := range endpoints.Nodes {
// Nodes without a hostid are mapped to pseudo nodes, if
// possible.
if _, ok := n.Latest.Lookup(report.HostNodeID); !ok {
if id, ok := pseudoNodeID(n, local); ok {
ret.addChild(n, id, Pseudo)
continue
}
}
if id := e.f(n); id != "" {
ret.addChild(n, id, e.topology)
}
}
return ret.result(endpoints)
}