Skip to content

Commit 7e9f6f1

Browse files
committed
Merge commit '9ebab3bc3c7bcb22e40fc247ea943b85b272b32f' into fix/helm-module-nil
2 parents 6c00d38 + 9ebab3b commit 7e9f6f1

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
@@ -52,18 +52,39 @@ func (op *AddonOperator) RegisterDebugGlobalRoutes(dbgSrv *debug.Server) {
5252
}
5353

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

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

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

pkg/module_manager/module_manager.go

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

440+
func (mm *ModuleManager) GetGraphDOTDescription() ([]byte, error) {
441+
return mm.moduleScheduler.GetGraphDOTDescription()
442+
}
443+
440444
// SetGlobalDiscoveryAPIVersions applies global values patch to .global.discovery.apiVersions key
441445
// if non-default moduleLoader is in use
442446
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)