Skip to content

Commit ac75426

Browse files
Enable profiling for operator pod
Signed-off-by: alexander-demicev <[email protected]>
1 parent 46c8fea commit ac75426

File tree

6 files changed

+68
-18
lines changed

6 files changed

+68
-18
lines changed

cmd/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ func InitFlags(fs *pflag.FlagSet) {
104104
fs.StringVar(&watchNamespace, "namespace", "",
105105
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.")
106106

107-
fs.StringVar(&profilerAddress, "profiler-address", "",
108-
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
107+
fs.StringVar(&profilerAddress, "profiler-address", "localhost:6060",
108+
"Bind address to expose the pprof profiler (e.g. localhost:6060). Set to empty string to disable.")
109109

110110
fs.BoolVar(&enableContentionProfiling, "contention-profiling", false,
111-
"Enable block profiling")
111+
"Enable block profiling when profiler is active")
112112

113113
fs.IntVar(&concurrencyNumber, "concurrency", 1,
114114
"Number of core resources to process simultaneously")
@@ -148,7 +148,7 @@ func main() {
148148
}
149149
}
150150

151-
if enableContentionProfiling {
151+
if enableContentionProfiling && profilerAddress != "" {
152152
goruntime.SetBlockProfileRate(1)
153153
}
154154

config/manager/manager.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ spec:
2525
image: controller:latest
2626
name: manager
2727
ports:
28+
- containerPort: 6060
29+
name: profiler
30+
protocol: TCP
2831
- containerPort: 8443
2932
name: metrics
3033
protocol: TCP
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Profiling
2+
3+
This section explains how to set up and use debugging endpoints like pprof for the Cluster API Operator.
4+
5+
### Configuring Helm Values
6+
7+
Profiling is enabled by default but some values can be customized. You can set the following values in your `values.yaml` file:
8+
9+
```yaml
10+
profilerAddress: ":6060"
11+
contentionProfiling: true
12+
```
13+
14+
Install with these custom values using [Helm chart installation methods](../02_installation/04_helm-chart-installation.md)
15+
16+
### Enabling Port-Forwarding
17+
18+
To access the pprof server on your local machine, run:
19+
20+
```bash
21+
kubectl port-forward deployment/capi-operator -n <namespace> 6060
22+
```
23+
24+
This will forward port 6060 from the container to your local machine.
25+
26+
### Running pprof Commands
27+
28+
With port-forwarding in place, you can run pprof commands like this:
29+
30+
```bash
31+
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
32+
```

hack/charts/cluster-api-operator/templates/deployment.yaml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ spec:
8585
{{- if .retryPeriod }}
8686
- --leader-elect-retry-period={{ .retryPeriod }}
8787
{{- end }}
88+
{{- if $.Values.profilerAddress }}
89+
- --profiler-address=localhost{{ $.Values.profilerAddress }}
90+
{{- end }}
91+
{{- if $.Values.contentionProfiling }}
92+
- --contention-profiling={{ $.Values.contentionProfiling }}
93+
{{- end }}
8894
{{- end }}
8995
command:
9096
- /manager
@@ -94,18 +100,21 @@ spec:
94100
imagePullPolicy: {{ .Values.image.manager.pullPolicy }}
95101
name: manager
96102
ports:
103+
{{- if $.Values.profilerAddress }}
104+
{{- $profilerPort := $.Values.profilerAddress | toString | trimPrefix ":" | int }}
105+
- containerPort: {{ $profilerPort }}
106+
name: profiler
107+
protocol: TCP
108+
{{- end }}
97109
- containerPort: 9443
98110
name: webhook-server
99111
protocol: TCP
100-
{{- if $.Values.diagnosticsAddress }}
101-
{{- $diagnosticsPort := $.Values.diagnosticsAddress }}
102-
{{- if contains ":" $diagnosticsPort -}}
103-
{{ $diagnosticsPort = ( split ":" $.Values.diagnosticsAddress)._1 | int }}
104-
{{- end }}
105-
- containerPort: {{ $diagnosticsPort | int }}
106-
name: metrics
112+
{{- if .Values.diagnosticsAddress }}
113+
{{- $diagnosticsPort := .Values.diagnosticsAddress | toString | trimPrefix ":" | int }}
114+
- containerPort: {{ $diagnosticsPort }}
115+
name: diagnostics
107116
protocol: TCP
108-
{{- end }}
117+
{{- end }}
109118
{{- with .Values.resources.manager }}
110119
resources:
111120
{{- toYaml . | nindent 12 }}
@@ -123,15 +132,15 @@ spec:
123132
{{- toYaml . | nindent 12 }}
124133
{{- end }}
125134
terminationMessagePolicy: FallbackToLogsOnError
126-
{{- $healthAddr := $.Values.healthAddr }}
127-
{{- if contains ":" $healthAddr -}}
128-
{{ $healthAddr = ( split ":" $.Values.healthAddr)._1 | int }}
135+
{{- $healthPort := 9440 }}
136+
{{- if .Values.healthAddr }}
137+
{{- $healthPort = .Values.healthAddr | toString | trimPrefix ":" | int }}
129138
{{- end }}
130139
livenessProbe:
131140
failureThreshold: 3
132141
httpGet:
133142
path: /healthz
134-
port: {{ $healthAddr | default 9440 }}
143+
port: {{ $healthPort }}
135144
scheme: HTTP
136145
initialDelaySeconds: 15
137146
periodSeconds: 20
@@ -141,7 +150,7 @@ spec:
141150
failureThreshold: 3
142151
httpGet:
143152
path: /readyz
144-
port: {{ $healthAddr | default 9440 }}
153+
port: {{ $healthPort }}
145154
scheme: HTTP
146155
initialDelaySeconds: 5
147156
periodSeconds: 10

hack/charts/cluster-api-operator/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ env:
4545
manager: []
4646
diagnosticsAddress: ":8443"
4747
healthAddr: ":9440"
48+
profilerAddress: ":6060"
49+
contentionProfiling: false
4850
insecureDiagnostics: false
4951
watchConfigSecret: false
5052
imagePullSecrets: {}

test/e2e/resources/full-chart-install.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22077,17 +22077,21 @@ spec:
2207722077
- --health-addr=:9440
2207822078
- --diagnostics-address=:8443
2207922079
- --leader-elect=true
22080+
- --profiler-address=localhost:6060
2208022081
command:
2208122082
- /manager
2208222083
image: "gcr.io/k8s-staging-capi-operator/cluster-api-operator:dev"
2208322084
imagePullPolicy: IfNotPresent
2208422085
name: manager
2208522086
ports:
22087+
- containerPort: 6060
22088+
name: profiler
22089+
protocol: TCP
2208622090
- containerPort: 9443
2208722091
name: webhook-server
2208822092
protocol: TCP
2208922093
- containerPort: 8443
22090-
name: metrics
22094+
name: diagnostics
2209122095
protocol: TCP
2209222096
resources:
2209322097
limits:

0 commit comments

Comments
 (0)