host.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package render
  2. import (
  3. "context"
  4. "github.com/weaveworks/scope/report"
  5. )
  6. // HostRenderer is a Renderer which produces a renderable host
  7. // graph from the host topology.
  8. //
  9. // not memoised
  10. var HostRenderer = MakeReduce(
  11. CustomRenderer{RenderFunc: nodes2Hosts, Renderer: ProcessRenderer},
  12. CustomRenderer{RenderFunc: nodes2Hosts, Renderer: ContainerRenderer},
  13. CustomRenderer{RenderFunc: nodes2Hosts, Renderer: ContainerImageRenderer},
  14. CustomRenderer{RenderFunc: nodes2Hosts, Renderer: PodRenderer},
  15. MapEndpoints(endpoint2Host, report.Host),
  16. )
  17. // nodes2Hosts maps any Nodes to host Nodes.
  18. //
  19. // If this function is given a node without a hostname
  20. // (including other pseudo nodes), it will drop the node.
  21. //
  22. // Otherwise, this function will produce nodes with the correct ID
  23. // format for a host, but without any Major or Minor labels. It does
  24. // not have enough info to do that, and the resulting graph must be
  25. // merged with a host graph to get that info.
  26. func nodes2Hosts(ctx context.Context, nodes Nodes) Nodes {
  27. ret := newJoinResults(nil)
  28. for _, n := range nodes.Nodes {
  29. if n.Topology == Pseudo {
  30. continue // Don't propagate pseudo nodes - we do this in endpoints2Hosts
  31. }
  32. isImage := n.Topology == report.ContainerImage
  33. hostIDs, _ := n.Parents.Lookup(report.Host)
  34. for _, id := range hostIDs {
  35. if isImage {
  36. // We need to treat image nodes specially because they
  37. // aggregate adjacencies of containers across multiple
  38. // hosts, and hence mapping these adjacencies to host
  39. // adjacencies would produce edges that aren't present
  40. // in reality.
  41. ret.addUnmappedChild(n, id, report.Host)
  42. } else {
  43. ret.addChild(n, id, report.Host)
  44. }
  45. }
  46. }
  47. return ret.result(ctx, nodes)
  48. }
  49. func endpoint2Host(n report.Node) string {
  50. if hostNodeID, ok := n.Latest.Lookup(report.HostNodeID); ok {
  51. return hostNodeID
  52. }
  53. return ""
  54. }