@@ -147,6 +147,9 @@ var _ = Describe("manger.Manager", func() {
147147ReadinessEndpointName : "/readyz" ,
148148LivenessEndpointName : "/livez" ,
149149},
150+ Pprof : v1alpha1.ControllerPprof {
151+ BindAddress : ":6080" ,
152+ },
150153Webhook : v1alpha1.ControllerWebhook {
151154Port : & port ,
152155Host : "localhost" ,
@@ -171,6 +174,7 @@ var _ = Describe("manger.Manager", func() {
171174Expect (m .HealthProbeBindAddress ).To (Equal ("6060" ))
172175Expect (m .ReadinessEndpointName ).To (Equal ("/readyz" ))
173176Expect (m .LivenessEndpointName ).To (Equal ("/livez" ))
177+ Expect (m .PprofBindAddress ).To (Equal (":6080" ))
174178Expect (m .Port ).To (Equal (port ))
175179Expect (m .Host ).To (Equal ("localhost" ))
176180Expect (m .CertDir ).To (Equal ("/certs" ))
@@ -203,6 +207,9 @@ var _ = Describe("manger.Manager", func() {
203207ReadinessEndpointName : "/readyz" ,
204208LivenessEndpointName : "/livez" ,
205209},
210+ Pprof : v1alpha1.ControllerPprof {
211+ BindAddress : ":6080" ,
212+ },
206213Webhook : v1alpha1.ControllerWebhook {
207214Port : & port ,
208215Host : "localhost" ,
@@ -225,6 +232,7 @@ var _ = Describe("manger.Manager", func() {
225232HealthProbeBindAddress : "5000" ,
226233ReadinessEndpointName : "/readiness" ,
227234LivenessEndpointName : "/liveness" ,
235+ PprofBindAddress : ":6000" ,
228236Port : 8080 ,
229237Host : "example.com" ,
230238CertDir : "/pki" ,
@@ -244,6 +252,7 @@ var _ = Describe("manger.Manager", func() {
244252Expect (m .HealthProbeBindAddress ).To (Equal ("5000" ))
245253Expect (m .ReadinessEndpointName ).To (Equal ("/readiness" ))
246254Expect (m .LivenessEndpointName ).To (Equal ("/liveness" ))
255+ Expect (m .PprofBindAddress ).To (Equal (":6000" ))
247256Expect (m .Port ).To (Equal (8080 ))
248257Expect (m .Host ).To (Equal ("example.com" ))
249258Expect (m .CertDir ).To (Equal ("/pki" ))
@@ -282,6 +291,7 @@ var _ = Describe("manger.Manager", func() {
282291LeaderElectionID : "test-leader-election-id-2" ,
283292HealthProbeBindAddress : "0" ,
284293MetricsBindAddress : "0" ,
294+ PprofBindAddress : "0" ,
285295})
286296Expect (err ).To (BeNil ())
287297
@@ -327,6 +337,7 @@ var _ = Describe("manger.Manager", func() {
327337LeaderElectionID : "test-leader-election-id-3" ,
328338HealthProbeBindAddress : "0" ,
329339MetricsBindAddress : "0" ,
340+ PprofBindAddress : "0" ,
330341})
331342Expect (err ).To (BeNil ())
332343
@@ -361,6 +372,7 @@ var _ = Describe("manger.Manager", func() {
361372},
362373HealthProbeBindAddress : "0" ,
363374MetricsBindAddress : "0" ,
375+ PprofBindAddress : "0" ,
364376})
365377Expect (err ).ToNot (HaveOccurred ())
366378Expect (m1 ).ToNot (BeNil ())
@@ -381,6 +393,7 @@ var _ = Describe("manger.Manager", func() {
381393},
382394HealthProbeBindAddress : "0" ,
383395MetricsBindAddress : "0" ,
396+ PprofBindAddress : "0" ,
384397})
385398Expect (err ).ToNot (HaveOccurred ())
386399Expect (m2 ).ToNot (BeNil ())
@@ -1395,6 +1408,94 @@ var _ = Describe("manger.Manager", func() {
13951408})
13961409})
13971410
1411+ Context ("should start serving pprof" , func () {
1412+ var listener net.Listener
1413+ var opts Options
1414+
1415+ BeforeEach (func () {
1416+ listener = nil
1417+ opts = Options {
1418+ newPprofListener : func (addr string ) (net.Listener , error ) {
1419+ var err error
1420+ listener , err = defaultPprofListener (addr )
1421+ return listener , err
1422+ },
1423+ }
1424+ })
1425+
1426+ AfterEach (func () {
1427+ if listener != nil {
1428+ listener .Close ()
1429+ }
1430+ })
1431+
1432+ It ("should stop serving pprof when stop is called" , func () {
1433+ opts .PprofBindAddress = ":0"
1434+ m , err := New (cfg , opts )
1435+ Expect (err ).NotTo (HaveOccurred ())
1436+
1437+ ctx , cancel := context .WithCancel (context .Background ())
1438+ go func () {
1439+ defer GinkgoRecover ()
1440+ Expect (m .Start (ctx )).NotTo (HaveOccurred ())
1441+ }()
1442+ <- m .Elected ()
1443+
1444+ // Check the pprof started
1445+ endpoint := fmt .Sprintf ("http://%s" , listener .Addr ().String ())
1446+ _ , err = http .Get (endpoint )
1447+ Expect (err ).NotTo (HaveOccurred ())
1448+
1449+ // Shutdown the server
1450+ cancel ()
1451+
1452+ // Expect the pprof server to shutdown
1453+ Eventually (func () error {
1454+ _ , err = http .Get (endpoint )
1455+ return err
1456+ }).ShouldNot (Succeed ())
1457+ })
1458+
1459+ It ("should serve pprof endpoints" , func () {
1460+ opts .PprofBindAddress = ":0"
1461+ m , err := New (cfg , opts )
1462+ Expect (err ).NotTo (HaveOccurred ())
1463+
1464+ ctx , cancel := context .WithCancel (context .Background ())
1465+ defer cancel ()
1466+ go func () {
1467+ defer GinkgoRecover ()
1468+ Expect (m .Start (ctx )).NotTo (HaveOccurred ())
1469+ }()
1470+ <- m .Elected ()
1471+
1472+ pprofIndexEndpoint := fmt .Sprintf ("http://%s/debug/pprof/" , listener .Addr ().String ())
1473+ resp , err := http .Get (pprofIndexEndpoint )
1474+ Expect (err ).NotTo (HaveOccurred ())
1475+ Expect (resp .StatusCode ).To (Equal (200 ))
1476+
1477+ pprofCmdlineEndpoint := fmt .Sprintf ("http://%s/debug/pprof/cmdline" , listener .Addr ().String ())
1478+ resp , err = http .Get (pprofCmdlineEndpoint )
1479+ Expect (err ).NotTo (HaveOccurred ())
1480+ Expect (resp .StatusCode ).To (Equal (200 ))
1481+
1482+ pprofProfileEndpoint := fmt .Sprintf ("http://%s/debug/pprof/profile" , listener .Addr ().String ())
1483+ resp , err = http .Get (pprofProfileEndpoint )
1484+ Expect (err ).NotTo (HaveOccurred ())
1485+ Expect (resp .StatusCode ).To (Equal (200 ))
1486+
1487+ pprofSymbolEndpoint := fmt .Sprintf ("http://%s/debug/pprof/symbol" , listener .Addr ().String ())
1488+ resp , err = http .Get (pprofSymbolEndpoint )
1489+ Expect (err ).NotTo (HaveOccurred ())
1490+ Expect (resp .StatusCode ).To (Equal (200 ))
1491+
1492+ pprofTraceEndpoint := fmt .Sprintf ("http://%s/debug/pprof/trace" , listener .Addr ().String ())
1493+ resp , err = http .Get (pprofTraceEndpoint )
1494+ Expect (err ).NotTo (HaveOccurred ())
1495+ Expect (resp .StatusCode ).To (Equal (200 ))
1496+ })
1497+ })
1498+
13981499Describe ("Add" , func () {
13991500It ("should immediately start the Component if the Manager has already Started another Component" ,
14001501func () {
0 commit comments