From 47563af08c3127fa1f93af2ae00e457b9f2bc194 Mon Sep 17 00:00:00 2001 From: LiHui Date: Wed, 1 Jun 2022 15:02:20 +0800 Subject: [PATCH] add gops agent to ks-apiserver&&controller-manager --- cmd/controller-manager/app/options/options.go | 6 ++++++ cmd/controller-manager/app/server.go | 10 ++++++++++ cmd/ks-apiserver/app/options/options.go | 5 +++++ cmd/ks-apiserver/app/server.go | 10 ++++++++++ 4 files changed, 31 insertions(+) diff --git a/cmd/controller-manager/app/options/options.go b/cmd/controller-manager/app/options/options.go index 6f542397b..b7dc0bb5b 100644 --- a/cmd/controller-manager/app/options/options.go +++ b/cmd/controller-manager/app/options/options.go @@ -82,6 +82,9 @@ type KubeSphereControllerManagerOptions struct { // * has the lowest priority. // e.g. *,-foo, means "disable 'foo'" ControllerGates []string + + // Enable gops or not. + GOPSEnabled bool } func NewKubeSphereControllerManagerOptions() *KubeSphereControllerManagerOptions { @@ -144,6 +147,9 @@ func (s *KubeSphereControllerManagerOptions) Flags(allControllerNameSelectors [] "named 'foo', '-foo' disables the controller named 'foo'.\nAll controllers: %s", strings.Join(allControllerNameSelectors, ", "))) + gfs.BoolVar(&s.GOPSEnabled, "gops", s.GOPSEnabled, "Whether to enable gops or not. When enabled this option, "+ + "controller-manager will listen on a random port on 127.0.0.1, then you can use the gops tool to list and diagnose the controller-manager currently running.") + kfs := fss.FlagSet("klog") local := flag.NewFlagSet("klog", flag.ExitOnError) klog.InitFlags(local) diff --git a/cmd/controller-manager/app/server.go b/cmd/controller-manager/app/server.go index 654a2b154..597b67cc5 100644 --- a/cmd/controller-manager/app/server.go +++ b/cmd/controller-manager/app/server.go @@ -21,6 +21,7 @@ import ( "fmt" "os" + "github.com/google/gops/agent" "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" utilerrors "k8s.io/apimachinery/pkg/util/errors" @@ -79,6 +80,15 @@ func NewControllerManagerCommand() *cobra.Command { klog.Error(utilerrors.NewAggregate(errs)) os.Exit(1) } + + if s.GOPSEnabled { + // Add agent to report additional information such as the current stack trace, Go version, memory stats, etc. + // Bind to a random port on address 127.0.0.1 + if err := agent.Listen(agent.Options{}); err != nil { + klog.Fatal(err) + } + } + if err = Run(s, controllerconfig.WatchConfigChange(), signals.SetupSignalHandler()); err != nil { klog.Error(err) os.Exit(1) diff --git a/cmd/ks-apiserver/app/options/options.go b/cmd/ks-apiserver/app/options/options.go index a8e8905a5..a5cebd046 100644 --- a/cmd/ks-apiserver/app/options/options.go +++ b/cmd/ks-apiserver/app/options/options.go @@ -62,6 +62,9 @@ type ServerRunOptions struct { // DebugMode bool + + // Enable gops or not. + GOPSEnabled bool } func NewServerRunOptions() *ServerRunOptions { @@ -76,6 +79,8 @@ func NewServerRunOptions() *ServerRunOptions { func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) { fs := fss.FlagSet("generic") fs.BoolVar(&s.DebugMode, "debug", false, "Don't enable this if you don't know what it means.") + fs.BoolVar(&s.GOPSEnabled, "gops", false, "Whether to enable gops or not. When enabled this option, "+ + "ks-apiserver will listen on a random port on 127.0.0.1, then you can use the gops tool to list and diagnose the ks-apiserver currently running.") s.GenericServerRunOptions.AddFlags(fs, s.GenericServerRunOptions) s.KubernetesOptions.AddFlags(fss.FlagSet("kubernetes"), s.KubernetesOptions) s.AuthenticationOptions.AddFlags(fss.FlagSet("authentication"), s.AuthenticationOptions) diff --git a/cmd/ks-apiserver/app/server.go b/cmd/ks-apiserver/app/server.go index 05d6b30da..2265e1494 100644 --- a/cmd/ks-apiserver/app/server.go +++ b/cmd/ks-apiserver/app/server.go @@ -21,6 +21,7 @@ import ( "fmt" "net/http" + "github.com/google/gops/agent" "github.com/spf13/cobra" utilerrors "k8s.io/apimachinery/pkg/util/errors" cliflag "k8s.io/component-base/cli/flag" @@ -57,6 +58,15 @@ cluster's shared state through which all other components interact.`, if errs := s.Validate(); len(errs) != 0 { return utilerrors.NewAggregate(errs) } + + if s.GOPSEnabled { + // Add agent to report additional information such as the current stack trace, Go version, memory stats, etc. + // Bind to a random port on address 127.0.0.1. + if err := agent.Listen(agent.Options{}); err != nil { + klog.Fatal(err) + } + } + return Run(s, apiserverconfig.WatchConfigChange(), signals.SetupSignalHandler()) }, SilenceUsage: true,