Skip to content

Commit 8a103b6

Browse files
committed
Add graph text description handler
Signed-off-by: Yuriy Losev <yuriy.losev@flant.com>
1 parent 6cc8378 commit 8a103b6

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

pkg/addon-operator/debug_server.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,39 @@ func (op *AddonOperator) RegisterDebugGlobalRoutes(dbgSrv *debug.Server) {
5151
}
5252

5353
func (op *AddonOperator) RegisterDebugGraphRoutes(dbgSrv *debug.Server) {
54-
dbgSrv.RegisterHandler(http.MethodGet, "/graph", func(_ *http.Request) (interface{}, error) {
54+
dbgSrv.Router.Get("/graph", func(w http.ResponseWriter, req *http.Request) {
55+
format := req.URL.Query().Get("format")
56+
if format == "text" {
57+
dotDesc, err := op.ModuleManager.GetGraphDOTDescription()
58+
if err != nil {
59+
w.WriteHeader(http.StatusInternalServerError)
60+
_, _ = w.Write([]byte(err.Error()))
61+
return
62+
}
63+
64+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
65+
w.WriteHeader(http.StatusOK)
66+
_, _ = w.Write(dotDesc)
67+
return
68+
}
69+
5570
image, err := op.ModuleManager.GetGraphImage()
5671
if err != nil {
57-
return nil, fmt.Errorf("couldn't get graph's image")
72+
w.WriteHeader(http.StatusInternalServerError)
73+
_, _ = w.Write([]byte(err.Error()))
74+
return
5875
}
5976

6077
buf := new(bytes.Buffer)
6178
if err = png.Encode(buf, image); err != nil {
62-
return nil, fmt.Errorf("couldn't encode graph's image")
79+
w.WriteHeader(http.StatusInternalServerError)
80+
_, _ = w.Write([]byte(fmt.Errorf("couldn't encode png graph's image").Error()))
81+
return
6382
}
6483

65-
return buf.String(), nil
84+
w.Header().Set("Content-Type", "image/png")
85+
w.WriteHeader(http.StatusOK)
86+
_, _ = w.Write(buf.Bytes())
6687
})
6788
}
6889

pkg/module_manager/module_manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ func (mm *ModuleManager) GetGraphImage() (image.Image, error) {
436436
return mm.moduleScheduler.GetGraphImage()
437437
}
438438

439+
func (mm *ModuleManager) GetGraphDOTDescription() ([]byte, error) {
440+
return mm.moduleScheduler.GetGraphDOTDescription()
441+
}
442+
439443
// SetGlobalDiscoveryAPIVersions applies global values patch to .global.discovery.apiVersions key
440444
// if non-default moduleLoader is in use
441445
func (mm *ModuleManager) SetGlobalDiscoveryAPIVersions(apiVersions []string) {

pkg/module_manager/scheduler/scheduler.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,48 @@ func (s *Scheduler) EventCh() chan extenders.ExtenderEvent {
108108
return s.extCh
109109
}
110110

111-
// GetGraphImage draws current graph's image
112-
func (s *Scheduler) GetGraphImage() (image.Image, error) {
113-
if s.graphImage != nil {
114-
return s.graphImage, nil
115-
}
111+
// GetGraphDOTDescription returns DOT(graph description language) description of the current graph
112+
func (s *Scheduler) GetGraphDOTDescription() ([]byte, error) {
113+
return s.getGraphDOTDescription()
114+
}
116115

116+
func (s *Scheduler) getGraphDOTDescription() ([]byte, error) {
117117
var b bytes.Buffer
118118
writer := bufio.NewWriter(&b)
119119

120120
if err := draw.DOT(s.dag, writer, draw.GraphAttribute("label", "Module Scheduler's Graph")); err != nil {
121-
return nil, fmt.Errorf("Couldn't write graph file: %w", err)
121+
return nil, fmt.Errorf("couldn't write graph file: %w", err)
122122
}
123123
writer.Flush()
124124

125-
graph, err := graphviz.ParseBytes(b.Bytes())
125+
return b.Bytes(), nil
126+
}
127+
128+
// GetGraphImage draws current graph's image
129+
func (s *Scheduler) GetGraphImage() (image.Image, error) {
130+
if s.graphImage != nil {
131+
return s.graphImage, nil
132+
}
133+
134+
dotDesc, err := s.getGraphDOTDescription()
135+
if err != nil {
136+
return nil, err
137+
}
138+
139+
graphvizGraph, err := graphviz.ParseBytes(dotDesc)
126140
if err != nil {
127-
return nil, fmt.Errorf("Couldn't parse graph file: %w", err)
141+
return nil, fmt.Errorf("couldn't parse graph file: %w", err)
128142
}
129143

130144
g := graphviz.New()
131145

132-
image, err := g.RenderImage(graph)
146+
graphvizImage, err := g.RenderImage(graphvizGraph)
133147
if err != nil {
134-
return nil, fmt.Errorf("Couldn't render graph image: %w", err)
148+
return nil, fmt.Errorf("couldn't render graph image: %w", err)
135149
}
136-
s.graphImage = image
150+
s.graphImage = graphvizImage
137151

138-
return image, nil
152+
return graphvizImage, nil
139153
}
140154

141155
// AddModuleVertex adds a new vertex of type Module to the graph

0 commit comments

Comments
 (0)