Skip to content

Commit 45bbfa1

Browse files
estrozAyush Rangwala
authored andcommitted
use sync.Once to initialize webhook server
Signed-off-by: Eric Stroczynski <[email protected]>
1 parent 4b22c1a commit 45bbfa1

File tree

3 files changed

+15
-41
lines changed

3 files changed

+15
-41
lines changed

pkg/manager/internal.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ type controllerManager struct {
145145
certDir string
146146

147147
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
148151

149152
// leaseDuration is the duration that non-leader candidates will
150153
// wait to force acquire leadership.
@@ -332,32 +335,19 @@ func (cm *controllerManager) GetAPIReader() client.Reader {
332335
}
333336

334337
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+
}
347345
}
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 {
357347
panic("unable to add webhook server to the controller manager")
358348
}
359-
}
360-
return server
349+
})
350+
return cm.webhookServer
361351
}
362352

363353
func (cm *controllerManager) GetLogger() logr.Logger {

pkg/manager/manager.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
365365
return nil, err
366366
}
367367

368-
cm := &controllerManager{
368+
return &controllerManager{
369369
cluster: cluster,
370370
recorderProvider: recorderProvider,
371371
resourceLock: resourceLock,
@@ -387,17 +387,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
387387
gracefulShutdownTimeout: *options.GracefulShutdownTimeout,
388388
internalProceduresStop: make(chan struct{}),
389389
leaderElectionStopped: make(chan struct{}),
390-
}
391-
392-
// A webhook server set by New's caller should be added now
393-
// so GetWebhookServer can construct a new one if unset and add it only once.
394-
if cm.webhookServer != nil {
395-
if err := cm.Add(cm.webhookServer); err != nil {
396-
return nil, err
397-
}
398-
}
399-
400-
return cm, nil
390+
}, nil
401391
}
402392

403393
// AndFrom will use a supplied type and convert to Options

pkg/manager/manager_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,6 @@ var _ = Describe("manger.Manager", func() {
280280
Expect(err).NotTo(HaveOccurred())
281281
Expect(m).NotTo(BeNil())
282282

283-
By("checking the webhook server was added to non-leader-election runnables")
284-
Expect(m).To(BeAssignableToTypeOf(&controllerManager{}))
285-
nonLERunnables := m.(*controllerManager).nonLeaderElectionRunnables
286-
Expect(nonLERunnables).To(HaveLen(1))
287-
Expect(nonLERunnables[0]).To(BeAssignableToTypeOf(&webhook.Server{}))
288-
289283
By("checking the server contains the Port set on the webhook server and not passed to Options")
290284
svr := m.GetWebhookServer()
291285
Expect(svr).NotTo(BeNil())

0 commit comments

Comments
 (0)