From 2999289e68c31b8e668a9163147f341311caa159 Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 14 Jun 2022 07:06:55 +0200 Subject: [PATCH] add support for pprof server (#5527) Signed-off-by: igor --- pkg/cli/cmds/server.go | 6 ++++++ pkg/cli/server/server.go | 1 + pkg/cluster/https.go | 13 +++++++++++++ pkg/daemons/config/types.go | 1 + 4 files changed, 21 insertions(+) diff --git a/pkg/cli/cmds/server.go b/pkg/cli/cmds/server.go index 09a60e5a73..2c4af93595 100644 --- a/pkg/cli/cmds/server.go +++ b/pkg/cli/cmds/server.go @@ -47,6 +47,7 @@ type Server struct { KubeConfigMode string TLSSan cli.StringSlice BindAddress string + EnablePProf bool ExtraAPIArgs cli.StringSlice ExtraEtcdArgs cli.StringSlice ExtraSchedulerArgs cli.StringSlice @@ -239,6 +240,11 @@ var ServerFlags = []cli.Flag{ Destination: &ServerConfig.KubeConfigMode, EnvVar: version.ProgramUpper + "_KUBECONFIG_MODE", }, + cli.BoolFlag{ + Name: "enable-pprof", + Usage: "(experimental) Enable pprof endpoint on supervisor port", + Destination: &ServerConfig.EnablePProf, + }, ExtraAPIArgs, ExtraEtcdArgs, ExtraControllerArgs, diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index 2397f767b1..ec444630d7 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -121,6 +121,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont serverConfig.ControlConfig.HTTPSPort = cfg.HTTPSPort serverConfig.ControlConfig.APIServerPort = cfg.APIServerPort serverConfig.ControlConfig.APIServerBindAddress = cfg.APIServerBindAddress + serverConfig.ControlConfig.EnablePProf = cfg.EnablePProf serverConfig.ControlConfig.ExtraAPIArgs = cfg.ExtraAPIArgs serverConfig.ControlConfig.ExtraControllerArgs = cfg.ExtraControllerArgs serverConfig.ControlConfig.ExtraEtcdArgs = cfg.ExtraEtcdArgs diff --git a/pkg/cluster/https.go b/pkg/cluster/https.go index 8c307faa65..6900595763 100644 --- a/pkg/cluster/https.go +++ b/pkg/cluster/https.go @@ -9,9 +9,11 @@ import ( "log" "net" "net/http" + "net/http/pprof" "os" "path/filepath" + "github.com/gorilla/mux" "github.com/k3s-io/k3s/pkg/daemons/config" "github.com/k3s-io/k3s/pkg/etcd" "github.com/k3s-io/k3s/pkg/version" @@ -94,6 +96,17 @@ func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error { return err } + if c.config.EnablePProf { + mux := mux.NewRouter() + 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) + mux.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index) + mux.NotFoundHandler = handler + handler = mux + } + // Create a HTTP server with the registered request handlers, using logrus for logging server := http.Server{ Handler: handler, diff --git a/pkg/daemons/config/types.go b/pkg/daemons/config/types.go index f9adc25497..1259ccfaeb 100644 --- a/pkg/daemons/config/types.go +++ b/pkg/daemons/config/types.go @@ -166,6 +166,7 @@ type Control struct { DisableETCD bool DisableKubeProxy bool DisableScheduler bool + EnablePProf bool ExtraAPIArgs []string ExtraControllerArgs []string ExtraCloudControllerArgs []string