Skip to content

Commit

Permalink
operator: delegate to controller-runtime's pprof
Browse files Browse the repository at this point in the history
Prior to this commit the operator had it's own pprof setup. This commit
replaces that with the controller-runtime's built in support.
  • Loading branch information
chrisseto committed Dec 6, 2024
1 parent dbe0481 commit 43c3dd0
Showing 1 changed file with 5 additions and 28 deletions.
33 changes: 5 additions & 28 deletions operator/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/http/pprof"
"strings"
"time"

Expand Down Expand Up @@ -143,9 +141,6 @@ func Command() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

// Always run a pprof server to facilitate debugging.
go runPProfServer(ctx, pprofAddr)

return Run(
ctx,
clusterDomain,
Expand All @@ -168,13 +163,14 @@ func Command() *cobra.Command {
unbinderSelector.Selector,
autoDeletePVCs,
forceDefluxedMode,
pprofAddr,
)
},
}

cmd.Flags().StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
cmd.Flags().StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
cmd.Flags().StringVar(&pprofAddr, "pprof-bind-address", ":8082", "The address the metric endpoint binds to.")
cmd.Flags().StringVar(&pprofAddr, "pprof-bind-address", ":8082", "The address the metric endpoint binds to. Set to '' or 0 to disable")
cmd.Flags().StringVar(&clusterDomain, "cluster-domain", "cluster.local", "Set the Kubernetes local domain (Kubelet's --cluster-domain)")
cmd.Flags().BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
Expand Down Expand Up @@ -235,18 +231,20 @@ func Run(
unbinderSelector labels.Selector,
autoDeletePVCs bool,
forceDefluxedMode bool,
pprofAddr string,
) error {
setupLog := ctrl.LoggerFrom(ctx).WithName("setup")

// set the managedFields owner for resources reconciled from Helm charts
kube.ManagedFieldsManager = controllerName

mgrOptions := ctrl.Options{
Metrics: metricsserver.Options{BindAddress: metricsAddr},
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "aa9fc693.vectorized.io",
LeaderElectionNamespace: namespace,
Metrics: metricsserver.Options{BindAddress: metricsAddr},
PprofBindAddress: pprofAddr,
}
if namespace != "" {
mgrOptions.Cache.DefaultNamespaces = map[string]cache.Config{namespace: {}}
Expand Down Expand Up @@ -541,24 +539,3 @@ func runThisController(rc RedpandaController, controllers []string) bool {
}
return false
}

func runPProfServer(ctx context.Context, listenAddr string) {
logger := ctrl.LoggerFrom(ctx)

pprofMux := http.NewServeMux()
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
pprofServer := &http.Server{
Addr: listenAddr,
Handler: pprofMux,
ReadHeaderTimeout: 3 * time.Second,
}

logger.Info("starting pprof server...", "addr", listenAddr)
if err := pprofServer.ListenAndServe(); err != nil {
logger.Error(err, "failed to run pprof server")
}
}

0 comments on commit 43c3dd0

Please sign in to comment.