Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 1 addition & 31 deletions cmd/crossplane/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/http/pprof"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -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."`
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 1 addition & 31 deletions cmd/crossplane/rbac/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ limitations under the License.
package rbac

import (
"net/http"
"net/http/pprof"
"path/filepath"
"strings"
"time"

Expand All @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down