Skip to content

Commit

Permalink
[deckhouse] Start HTTP API debug server if "DebugHttpPort" is set. (#520
Browse files Browse the repository at this point in the history
)

Signed-off-by: dmitry.koba <[email protected]>
Co-authored-by: dmitry.koba <[email protected]>
  • Loading branch information
Dmitrykob and dmitry.koba authored Aug 25, 2023
1 parent 1d4c4d9 commit ce92c1c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
10 changes: 10 additions & 0 deletions pkg/app/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

var DebugUnixSocket = "/var/run/shell-operator/debug.socket"

var DebugHttpServerAddr = ""

var DebugKeepTmpFiles = "no"

var DebugKubernetesAPI = false
Expand Down Expand Up @@ -75,3 +77,11 @@ func DefineDebugUnixSocketFlag(cmd *kingpin.CmdClause) {
Default(DebugUnixSocket).
StringVar(&DebugUnixSocket)
}

func DefineDebugHttpPortFlag(cmd *kingpin.CmdClause) {
cmd.Flag("debug-http-port", "http port for a debug endpoint").
Envar("DEBUG_HTTP_SERVER_ADDR").
Hidden().
Default(DebugHttpServerAddr).
StringVar(&DebugHttpServerAddr)
}
30 changes: 18 additions & 12 deletions pkg/debug/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@ import (
)

type Server struct {
SocketPath string
Prefix string
SocketPath string
HttpAddr string

Router chi.Router
}

func NewServer() *Server {
return &Server{}
}

func (s *Server) WithSocketPath(path string) {
s.SocketPath = path
}

func (s *Server) WithPrefix(prefix string) {
s.Prefix = prefix
func NewServer(prefix, socketPath, httpAddr string) *Server {
return &Server{
Prefix: prefix,
SocketPath: socketPath,
HttpAddr: httpAddr,
}
}

func (s *Server) Init() (err error) {
Expand Down Expand Up @@ -73,11 +70,20 @@ func (s *Server) Init() (err error) {

go func() {
if err := http.Serve(listener, s.Router); err != nil {
log.Errorf("Error starting Debug HTTP server: %s", err)
log.Errorf("Error starting Debug socket server: %s", err)
os.Exit(1)
}
}()

if s.HttpAddr != "" {
go func() {
if err := http.ListenAndServe(s.HttpAddr, s.Router); err != nil {
log.Errorf("Error starting Debug HTTP server: %s", err)
os.Exit(1)
}
}()
}

return nil
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/shell-operator/debug_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import (
)

func DefaultDebugServer() *debug.Server {
dbgSrv := debug.NewServer()
dbgSrv.WithPrefix("/debug")
dbgSrv.WithSocketPath(app.DebugUnixSocket)
dbgSrv := debug.NewServer("/debug", app.DebugUnixSocket, app.DebugHttpServerAddr)

return dbgSrv
}

Expand Down

0 comments on commit ce92c1c

Please sign in to comment.