@@ -145,6 +145,9 @@ type controllerManager struct {
145
145
certDir string
146
146
147
147
webhookServer * webhook.Server
148
+ // webhookServerOnce will be called in GetWebhookServer() to optionally initialize
149
+ // webhookServer if unset, and Add() it to controllerManager.
150
+ webhookServerOnce sync.Once
148
151
149
152
// leaseDuration is the duration that non-leader candidates will
150
153
// wait to force acquire leadership.
@@ -332,32 +335,19 @@ func (cm *controllerManager) GetAPIReader() client.Reader {
332
335
}
333
336
334
337
func (cm * controllerManager ) GetWebhookServer () * webhook.Server {
335
- server , wasNew := func () (* webhook.Server , bool ) {
336
- cm .mu .Lock ()
337
- defer cm .mu .Unlock ()
338
-
339
- if cm .webhookServer != nil {
340
- return cm .webhookServer , false
341
- }
342
-
343
- cm .webhookServer = & webhook.Server {
344
- Port : cm .port ,
345
- Host : cm .host ,
346
- CertDir : cm .certDir ,
338
+ cm .webhookServerOnce .Do (func () {
339
+ if cm .webhookServer == nil {
340
+ cm .webhookServer = & webhook.Server {
341
+ Port : cm .port ,
342
+ Host : cm .host ,
343
+ CertDir : cm .certDir ,
344
+ }
347
345
}
348
- return cm .webhookServer , true
349
- }()
350
-
351
- // only add the server if *we ourselves* just registered it.
352
- // Add has its own lock, so just do this separately -- there shouldn't
353
- // be a "race" in this lock gap because the condition is the population
354
- // of cm.webhookServer, not anything to do with Add.
355
- if wasNew {
356
- if err := cm .Add (server ); err != nil {
346
+ if err := cm .Add (cm .webhookServer ); err != nil {
357
347
panic ("unable to add webhook server to the controller manager" )
358
348
}
359
- }
360
- return server
349
+ })
350
+ return cm . webhookServer
361
351
}
362
352
363
353
func (cm * controllerManager ) GetLogger () logr.Logger {
0 commit comments