endpoint.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package render
  2. import (
  3. "context"
  4. opentracing "github.com/opentracing/opentracing-go"
  5. otlog "github.com/opentracing/opentracing-go/log"
  6. "github.com/weaveworks/scope/report"
  7. )
  8. // Pseudo is the topology for nodes that aren't "real" nodes inside a
  9. // cluster, such as nodes representing the internet, external
  10. // services, and artificial grouping such as "uncontained processes"
  11. // and "unmanaged containers".
  12. const Pseudo = "pseudo"
  13. // EndpointRenderer is a Renderer which produces a renderable endpoint graph.
  14. var EndpointRenderer = SelectEndpoint
  15. type endpointMapFunc func(report.Node) string
  16. type mapEndpoints struct {
  17. f endpointMapFunc
  18. topology string
  19. }
  20. // MapEndpoints creates a renderer for the endpoint topology. Each
  21. // endpoint is either turned into a pseudo node, or mapped to a node
  22. // in the specified topology by the supplied function.
  23. func MapEndpoints(f endpointMapFunc, topology string) Renderer {
  24. return mapEndpoints{f: f, topology: topology}
  25. }
  26. func (e mapEndpoints) Render(ctx context.Context, rpt report.Report) Nodes {
  27. span, ctx := opentracing.StartSpanFromContext(ctx, "mapEndpoints.Render")
  28. defer span.Finish()
  29. local := LocalNetworks(rpt)
  30. endpoints := SelectEndpoint.Render(ctx, rpt)
  31. ret := newJoinResults(TopologySelector(e.topology).Render(ctx, rpt).Nodes)
  32. for _, n := range endpoints.Nodes {
  33. // Nodes without a hostid are mapped to pseudo nodes, if
  34. // possible.
  35. if _, ok := n.Latest.Lookup(report.HostNodeID); !ok {
  36. if id, ok := pseudoNodeID(rpt, n, local); ok {
  37. ret.addChild(n, id, Pseudo)
  38. continue
  39. }
  40. }
  41. if id := e.f(n); id != "" {
  42. ret.addChild(n, id, e.topology)
  43. }
  44. }
  45. span.LogFields(otlog.Int("input.nodes", len(endpoints.Nodes)),
  46. otlog.Int("ouput.nodes", len(ret.nodes)))
  47. return ret.result(ctx, endpoints)
  48. }