From bbe9fe13e565d5abf287b784146ff73b8d550746 Mon Sep 17 00:00:00 2001 From: Evan Baker Date: Fri, 20 May 2022 17:16:53 +0000 Subject: [PATCH 1/2] add readyz and pprof listeners Signed-off-by: Evan Baker --- cns/configuration/configuration.go | 1 + cns/healthserver/server.go | 7 ++----- cns/service/main.go | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cns/configuration/configuration.go b/cns/configuration/configuration.go index b1414a2fc7..9a05d8b950 100644 --- a/cns/configuration/configuration.go +++ b/cns/configuration/configuration.go @@ -20,6 +20,7 @@ const ( type CNSConfig struct { ChannelMode string + EnablePprof bool InitializeFromCNI bool ManagedSettings ManagedSettings MetricsBindAddress string diff --git a/cns/healthserver/server.go b/cns/healthserver/server.go index 1bc323833d..779b259bd8 100644 --- a/cns/healthserver/server.go +++ b/cns/healthserver/server.go @@ -6,13 +6,14 @@ import ( "github.com/labstack/echo/v4" "github.com/prometheus/client_golang/prometheus/promhttp" "go.uber.org/zap" + "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/metrics" ) func Start(log *zap.Logger, addr string) { e := echo.New() - e.GET("/healthz", healthz) + e.GET("/healthz", echo.WrapHandler(http.StripPrefix("/healthz", &healthz.Handler{}))) e.GET("/metrics", echo.WrapHandler(promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{ ErrorHandling: promhttp.HTTPErrorOnError, }))) @@ -20,7 +21,3 @@ func Start(log *zap.Logger, addr string) { log.Error("failed to run healthserver", zap.Error(err)) } } - -func healthz(c echo.Context) error { - return c.NoContent(http.StatusOK) //nolint:wrapcheck // ignore -} diff --git a/cns/service/main.go b/cns/service/main.go index c2b1609ebd..6707258ed3 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "net/http" + "net/http/pprof" "os" "os/signal" "runtime" @@ -55,6 +56,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/healthz" ) const ( @@ -1040,6 +1042,18 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn return errors.Wrapf(err, "failed to setup reconciler with manager") } + // adding some routes to the root service mux + mux := httpRestServiceImplementation.Listener.GetMux() + mux.Handle("/readyz", http.StripPrefix("/readyz", &healthz.Handler{})) + if cnsconfig.EnablePprof { + // add pprof endpoints + mux.HandleFunc("/debug/pprof/", pprof.Index) + mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + mux.HandleFunc("/debug/pprof/profile", pprof.Profile) + mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + mux.HandleFunc("/debug/pprof/trace", pprof.Trace) + } + // Start the Manager which starts the reconcile loop. // The Reconciler will send an initial NodeNetworkConfig update to the PoolMonitor, starting the // Monitor's internal loop. From fb55b2a34bef73cfc039bc83fa9a2b1208f48957 Mon Sep 17 00:00:00 2001 From: Evan Baker Date: Wed, 1 Jun 2022 21:46:56 +0000 Subject: [PATCH 2/2] add more pprof handlers Signed-off-by: Evan Baker --- cns/service/main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cns/service/main.go b/cns/service/main.go index 6707258ed3..9beebbce03 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -1047,6 +1047,12 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn mux.Handle("/readyz", http.StripPrefix("/readyz", &healthz.Handler{})) if cnsconfig.EnablePprof { // add pprof endpoints + mux.Handle("/debug/pprof/allocs", pprof.Handler("allocs")) + mux.Handle("/debug/pprof/block", pprof.Handler("block")) + mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine")) + mux.Handle("/debug/pprof/heap", pprof.Handler("heap")) + mux.Handle("/debug/pprof/mutex", pprof.Handler("mutex")) + mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate")) mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) mux.HandleFunc("/debug/pprof/profile", pprof.Profile)