Use controller-runtime's pprof server rather than our own

I believe this implementation is identical:

https://github.com/kubernetes-sigs/controller-runtime/blob/v0.16.3/pkg/manager/internal.go#L295

Signed-off-by: Nic Cope <nicc@rk0n.org>
This commit is contained in:
Nic Cope 2023-11-11 01:09:46 -08:00
parent 1a1333fc85
commit a872c034c9
2 changed files with 2 additions and 62 deletions

View File

@ -21,8 +21,6 @@ import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/http/pprof"
"os"
"path/filepath"
"time"
@ -62,8 +60,6 @@ import (
"github.com/crossplane/crossplane/internal/xpkg"
)
const pprofPath = "/debug/pprof/"
// Command runs the core crossplane controllers
type Command struct {
Start startCommand `cmd:"" help:"Start Crossplane controllers."`
@ -126,33 +122,6 @@ type startCommand struct {
// Run core Crossplane controllers.
func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error { //nolint:gocyclo // Only slightly over.
if c.Profile != "" {
// NOTE(negz): These log messages attempt to match those emitted by
// controller-runtime's metrics HTTP server when it starts.
log.Debug("Profiling server is starting to listen", "addr", c.Profile)
go func() {
// Registering these explicitly ensures they're only served by the
// HTTP server we start explicitly for profiling.
mux := http.NewServeMux()
mux.HandleFunc(pprofPath, pprof.Index)
mux.HandleFunc(filepath.Join(pprofPath, "cmdline"), pprof.Cmdline)
mux.HandleFunc(filepath.Join(pprofPath, "profile"), pprof.Profile)
mux.HandleFunc(filepath.Join(pprofPath, "symbol"), pprof.Symbol)
mux.HandleFunc(filepath.Join(pprofPath, "trace"), pprof.Trace)
s := &http.Server{
Addr: c.Profile,
ReadTimeout: 2 * time.Minute,
WriteTimeout: 2 * time.Minute,
Handler: mux,
}
log.Debug("Starting server", "type", "pprof", "path", pprofPath, "addr", s.Addr)
err := s.ListenAndServe()
log.Debug("Profiling server has stopped listening", "error", err)
}()
}
cfg, err := ctrl.GetConfig()
if err != nil {
return errors.Wrap(err, "cannot get config")
@ -199,6 +168,7 @@ func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error { //noli
LeaseDuration: func() *time.Duration { d := 60 * time.Second; return &d }(),
RenewDeadline: func() *time.Duration { d := 50 * time.Second; return &d }(),
PprofBindAddress: c.Profile,
HealthProbeBindAddress: ":8081",
})
if err != nil {

View File

@ -18,9 +18,6 @@ limitations under the License.
package rbac
import (
"net/http"
"net/http/pprof"
"path/filepath"
"strings"
"time"
@ -46,8 +43,6 @@ const (
ManagementPolicyBasic = string(rbaccontroller.ManagementPolicyBasic)
)
const pprofPath = "/debug/pprof/"
// KongVars represent the kong variables associated with the CLI parser
// required for the RBAC enum interpolation.
var KongVars = kong.Vars{
@ -90,32 +85,6 @@ type startCommand struct {
// Run the RBAC manager.
func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error {
log.Debug("Starting", "policy", c.ManagementPolicy)
if c.Profile != "" {
// NOTE(negz): These log messages attempt to match those emitted by
// controller-runtime's metrics HTTP server when it starts.
log.Debug("Profiling server is starting to listen", "addr", c.Profile)
go func() {
// Registering these explicitly ensures they're only served by the
// HTTP server we start specifically for profiling.
mux := http.NewServeMux()
mux.HandleFunc(pprofPath, pprof.Index)
mux.HandleFunc(filepath.Join(pprofPath, "cmdline"), pprof.Cmdline)
mux.HandleFunc(filepath.Join(pprofPath, "profile"), pprof.Profile)
mux.HandleFunc(filepath.Join(pprofPath, "symbol"), pprof.Symbol)
mux.HandleFunc(filepath.Join(pprofPath, "trace"), pprof.Trace)
s := &http.Server{
Addr: c.Profile,
ReadTimeout: 2 * time.Minute,
WriteTimeout: 2 * time.Minute,
Handler: mux,
}
log.Debug("Starting server", "type", "pprof", "path", pprofPath, "addr", s.Addr)
err := s.ListenAndServe()
log.Debug("Profiling server has stopped listening", "error", err)
}()
}
cfg, err := ctrl.GetConfig()
if err != nil {
@ -130,6 +99,7 @@ func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error {
Cache: cache.Options{
SyncPeriod: &c.SyncInterval,
},
PprofBindAddress: c.Profile,
})
if err != nil {
return errors.Wrap(err, "cannot create manager")