handlers.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2019 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "mime"
  20. "net"
  21. "net/http"
  22. "os"
  23. "os/exec"
  24. "github.com/kilo-io/kilo/pkg/mesh"
  25. )
  26. type graphHandler struct {
  27. mesh *mesh.Mesh
  28. granularity mesh.Granularity
  29. hostname *string
  30. subnet *net.IPNet
  31. }
  32. func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  33. ns, err := h.mesh.Nodes().List()
  34. if err != nil {
  35. http.Error(w, fmt.Sprintf("failed to list nodes: %v", err), http.StatusInternalServerError)
  36. return
  37. }
  38. ps, err := h.mesh.Peers().List()
  39. if err != nil {
  40. http.Error(w, fmt.Sprintf("failed to list peers: %v", err), http.StatusInternalServerError)
  41. return
  42. }
  43. nodes := make(map[string]*mesh.Node)
  44. for _, n := range ns {
  45. if n.Ready() {
  46. nodes[n.Name] = n
  47. }
  48. }
  49. if len(nodes) == 0 {
  50. http.Error(w, "did not find any valid Kilo nodes in the cluster", http.StatusInternalServerError)
  51. return
  52. }
  53. peers := make(map[string]*mesh.Peer)
  54. for _, p := range ps {
  55. if p.Ready() {
  56. peers[p.Name] = p
  57. }
  58. }
  59. topo, err := mesh.NewTopology(nodes, peers, h.granularity, *h.hostname, 0, []byte{}, h.subnet, nodes[*h.hostname].PersistentKeepalive, nil)
  60. if err != nil {
  61. http.Error(w, fmt.Sprintf("failed to create topology: %v", err), http.StatusInternalServerError)
  62. return
  63. }
  64. dot, err := topo.Dot()
  65. if err != nil {
  66. http.Error(w, fmt.Sprintf("failed to generate graph: %v", err), http.StatusInternalServerError)
  67. }
  68. buf := bytes.NewBufferString(dot)
  69. format := r.URL.Query().Get("format")
  70. switch format {
  71. case "":
  72. format = "svg"
  73. case "dot", "gv":
  74. // If the raw dot data is requested, return it as string.
  75. // This allows client-side rendering rather than server-side.
  76. w.Write(buf.Bytes())
  77. return
  78. case "svg", "png", "bmp", "fig", "gif", "json", "ps":
  79. // Accepted format
  80. default:
  81. http.Error(w, "unsupported format", http.StatusInternalServerError)
  82. return
  83. }
  84. layout := r.URL.Query().Get("layout")
  85. switch layout {
  86. case "":
  87. layout = "circo"
  88. case "circo", "dot", "neato", "twopi", "fdp":
  89. // Accepted layout
  90. default:
  91. http.Error(w, "unsupported layout", http.StatusInternalServerError)
  92. return
  93. }
  94. command := exec.Command("dot", "-K"+layout, "-T"+format)
  95. command.Stderr = os.Stderr
  96. stdin, err := command.StdinPipe()
  97. if err != nil {
  98. http.Error(w, err.Error(), http.StatusInternalServerError)
  99. return
  100. }
  101. if _, err = io.Copy(stdin, buf); err != nil {
  102. http.Error(w, err.Error(), http.StatusInternalServerError)
  103. return
  104. }
  105. if err = stdin.Close(); err != nil {
  106. http.Error(w, err.Error(), http.StatusInternalServerError)
  107. return
  108. }
  109. output, err := command.Output()
  110. if err != nil {
  111. http.Error(w, "unable to render graph", http.StatusInternalServerError)
  112. return
  113. }
  114. mimeType := mime.TypeByExtension("." + format)
  115. if mimeType == "" {
  116. mimeType = "application/octet-stream"
  117. }
  118. w.Header().Add("content-type", mimeType)
  119. w.Write(output)
  120. }
  121. func healthHandler(w http.ResponseWriter, _ *http.Request) {
  122. w.WriteHeader(http.StatusOK)
  123. }