@@ -317,9 +317,9 @@ func (cm *controllerManager) addMetricsServer() error {
317
317
})
318
318
}
319
319
320
- func (cm * controllerManager ) serveHealthProbes () {
320
+ func (cm * controllerManager ) addHealthProbeServer () error {
321
321
mux := http .NewServeMux ()
322
- server := httpserver .New (mux )
322
+ srv := httpserver .New (mux )
323
323
324
324
if cm .readyzHandler != nil {
325
325
mux .Handle (cm .readinessEndpointName , http .StripPrefix (cm .readinessEndpointName , cm .readyzHandler ))
@@ -332,7 +332,12 @@ func (cm *controllerManager) serveHealthProbes() {
332
332
mux .Handle (cm .livenessEndpointName + "/" , http .StripPrefix (cm .livenessEndpointName , cm .healthzHandler ))
333
333
}
334
334
335
- go cm .httpServe ("health probe" , cm .logger , server , cm .healthProbeListener )
335
+ return cm .add (& server {
336
+ Kind : "health probe" ,
337
+ Log : cm .logger ,
338
+ Server : srv ,
339
+ Listener : cm .healthProbeListener ,
340
+ })
336
341
}
337
342
338
343
func (cm * controllerManager ) addPprofServer () error {
@@ -353,42 +358,6 @@ func (cm *controllerManager) addPprofServer() error {
353
358
})
354
359
}
355
360
356
- func (cm * controllerManager ) httpServe (kind string , log logr.Logger , server * http.Server , ln net.Listener ) {
357
- log = log .WithValues ("kind" , kind , "addr" , ln .Addr ())
358
-
359
- go func () {
360
- log .Info ("Starting server" )
361
- if err := server .Serve (ln ); err != nil {
362
- if errors .Is (err , http .ErrServerClosed ) {
363
- return
364
- }
365
- if atomic .LoadInt64 (cm .stopProcedureEngaged ) > 0 {
366
- // There might be cases where connections are still open and we try to shutdown
367
- // but not having enough time to close the connection causes an error in Serve
368
- //
369
- // In that case we want to avoid returning an error to the main error channel.
370
- log .Error (err , "error on Serve after stop has been engaged" )
371
- return
372
- }
373
- cm .errChan <- err
374
- }
375
- }()
376
-
377
- // Shutdown the server when stop is closed.
378
- <- cm .internalProceduresStop
379
- if err := server .Shutdown (cm .shutdownCtx ); err != nil {
380
- if errors .Is (err , context .Canceled ) || errors .Is (err , context .DeadlineExceeded ) {
381
- // Avoid logging context related errors.
382
- return
383
- }
384
- if atomic .LoadInt64 (cm .stopProcedureEngaged ) > 0 {
385
- cm .logger .Error (err , "error on Shutdown after stop has been engaged" )
386
- return
387
- }
388
- cm .errChan <- err
389
- }
390
- }
391
-
392
361
// Start starts the manager and waits indefinitely.
393
362
// There is only two ways to have start return:
394
363
// An error has occurred during in one of the internal operations,
@@ -449,7 +418,9 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
449
418
450
419
// Serve health probes.
451
420
if cm .healthProbeListener != nil {
452
- cm .serveHealthProbes ()
421
+ if err := cm .addHealthProbeServer (); err != nil {
422
+ return fmt .Errorf ("failed to add health probe server: %w" , err )
423
+ }
453
424
}
454
425
455
426
// Add pprof server
@@ -459,7 +430,17 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
459
430
}
460
431
}
461
432
462
- // First start any webhook servers, which includes conversion, validation, and defaulting
433
+ // First start any internal HTTP servers, which includes health probes, metrics and profiling if enabled.
434
+ //
435
+ // WARNING: Internal HTTP servers MUST start before any cache is populated, otherwise it would block
436
+ // conversion webhooks to be ready for serving which make the cache never get ready.
437
+ if err := cm .runnables .HTTPServers .Start (cm .internalCtx ); err != nil {
438
+ if err != nil {
439
+ return fmt .Errorf ("failed to start HTTP servers: %w" , err )
440
+ }
441
+ }
442
+
443
+ // Start any webhook servers, which includes conversion, validation, and defaulting
463
444
// webhooks that are registered.
464
445
//
465
446
// WARNING: Webhooks MUST start before any cache is populated, otherwise there is a race condition
@@ -591,10 +572,13 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e
591
572
cm .logger .Info ("Stopping and waiting for caches" )
592
573
cm .runnables .Caches .StopAndWait (cm .shutdownCtx )
593
574
594
- // Webhooks should come last, as they might be still serving some requests.
575
+ // Webhooks and internal HTTP servers should come last, as they might be still serving some requests.
595
576
cm .logger .Info ("Stopping and waiting for webhooks" )
596
577
cm .runnables .Webhooks .StopAndWait (cm .shutdownCtx )
597
578
579
+ cm .logger .Info ("Stopping and waiting for HTTP servers" )
580
+ cm .runnables .HTTPServers .StopAndWait (cm .shutdownCtx )
581
+
598
582
// Proceed to close the manager and overall shutdown context.
599
583
cm .logger .Info ("Wait completed, proceeding to shutdown the manager" )
600
584
shutdownCancel ()
0 commit comments