From ce92c1ca5c85e7b3f68557c580dc60e371d89300 Mon Sep 17 00:00:00 2001 From: Dmitrykob <52157046+Dmitrykob@users.noreply.github.com> Date: Fri, 25 Aug 2023 14:13:39 +0300 Subject: [PATCH] [deckhouse] Start HTTP API debug server if "DebugHttpPort" is set. (#520) Signed-off-by: dmitry.koba Co-authored-by: dmitry.koba --- pkg/app/debug.go | 10 ++++++++++ pkg/debug/server.go | 30 ++++++++++++++++++------------ pkg/shell-operator/debug_server.go | 5 ++--- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/pkg/app/debug.go b/pkg/app/debug.go index 7ad0b225..b7cec7c3 100644 --- a/pkg/app/debug.go +++ b/pkg/app/debug.go @@ -8,6 +8,8 @@ import ( var DebugUnixSocket = "/var/run/shell-operator/debug.socket" +var DebugHttpServerAddr = "" + var DebugKeepTmpFiles = "no" var DebugKubernetesAPI = false @@ -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) +} diff --git a/pkg/debug/server.go b/pkg/debug/server.go index d964c3c3..e3aef25a 100644 --- a/pkg/debug/server.go +++ b/pkg/debug/server.go @@ -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) { @@ -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 } diff --git a/pkg/shell-operator/debug_server.go b/pkg/shell-operator/debug_server.go index 09ff4e00..7486c5b7 100644 --- a/pkg/shell-operator/debug_server.go +++ b/pkg/shell-operator/debug_server.go @@ -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 }