Skip to content

Commit 40f733a

Browse files
authored
add env var to enable pprof for mcpo (#32)
1 parent 7d0494d commit 40f733a

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

cmd/mcp-operator/app/app.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ func (o *Options) runInit(ctx context.Context) error {
104104
}
105105

106106
if o.CRDFlags.Install {
107-
if err != nil {
108-
return fmt.Errorf("error building setup client: %w", err)
109-
}
110107
setupLog.Info("CRD installation configured, deploying CRDs ...")
111108
crds := crdinstall.CRDs()
112109
for _, crd := range crds {
@@ -176,6 +173,7 @@ func (o *Options) run(ctx context.Context) error {
176173
RecoverPanic: ptr.To(true),
177174
},
178175
HealthProbeBindAddress: o.ProbeAddr,
176+
PprofBindAddress: o.PprofAddr,
179177
LeaderElection: o.EnableLeaderElection,
180178
LeaderElectionID: "mcpo.openmcp.cloud",
181179
LeaderElectionNamespace: o.LeaseNamespace,

cmd/mcp-operator/app/options.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package app
33
import (
44
goflag "flag"
55
"fmt"
6+
"os"
7+
"regexp"
68
"strings"
79
"time"
810

@@ -27,6 +29,9 @@ import (
2729

2830
const (
2931
ControllerIDManagedControlPlane = "managedcontrolplane"
32+
PprofEnabledEnvVar = "ENABLE_PROFILER"
33+
PprofAddrEnvVar = "PROFILER_ADDRESS"
34+
PprofDefaultAddr = ":8082"
3035
)
3136

3237
var (
@@ -101,6 +106,9 @@ type Options struct {
101106
ActiveControllers sets.Set[string]
102107
WebhooksFlags *webhooks.Flags
103108
CRDFlags *crds.Flags
109+
110+
// options based on env vars
111+
PprofAddr string `json:"pprofAddr"`
104112
}
105113

106114
func NewOptions() *Options {
@@ -146,6 +154,12 @@ func (o *Options) String(includeHeader bool, includeRawOptions bool) (string, er
146154
opts["authConfig"] = o.AuthConfig
147155
opts["authzConfig"] = o.AuthzConfig
148156

157+
if o.PprofAddr == "" {
158+
opts["pprof"] = "disabled"
159+
} else {
160+
opts["pprof"] = o.PprofAddr
161+
}
162+
149163
// controllers
150164
opts["activeControllers"] = sets.List(o.ActiveControllers)
151165

@@ -312,6 +326,17 @@ func (o *Options) Complete() error {
312326
}
313327
}
314328

329+
// evaluate env vars
330+
if os.Getenv(PprofEnabledEnvVar) == "true" {
331+
o.PprofAddr = os.Getenv(PprofAddrEnvVar)
332+
if o.PprofAddr == "" {
333+
o.PprofAddr = PprofDefaultAddr
334+
} else if regexp.MustCompile(`^[0-9]{1,5}$`).MatchString(o.PprofAddr) {
335+
// if only a port is given, prepend a colon
336+
o.PprofAddr = ":" + o.PprofAddr
337+
}
338+
}
339+
315340
// print options
316341
optsString, err := o.String(true, false)
317342
if err != nil {

0 commit comments

Comments
 (0)