Skip to content

Commit 9e04ba9

Browse files
authored
Merge pull request #1500 from estroz/bugfix/inject-wh-server
🐛 add manager.Options.WebhookServer so webhook server can be set
2 parents 485a24a + c51d1ca commit 9e04ba9

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,24 @@ type Options struct {
209209
LivenessEndpointName string
210210

211211
// Port is the port that the webhook server serves at.
212-
// It is used to set webhook.Server.Port.
212+
// It is used to set webhook.Server.Port if WebhookServer is not set.
213213
Port int
214214
// Host is the hostname that the webhook server binds to.
215-
// It is used to set webhook.Server.Host.
215+
// It is used to set webhook.Server.Host if WebhookServer is not set.
216216
Host string
217217

218218
// CertDir is the directory that contains the server key and certificate.
219-
// if not set, webhook server would look up the server key and certificate in
219+
// If not set, webhook server would look up the server key and certificate in
220220
// {TempDir}/k8s-webhook-server/serving-certs. The server key and certificate
221221
// must be named tls.key and tls.crt, respectively.
222+
// It is used to set webhook.Server.CertDir if WebhookServer is not set.
222223
CertDir string
224+
225+
// WebhookServer is an externally configured webhook.Server. By default,
226+
// a Manager will create a default server using Port, Host, and CertDir;
227+
// if this is set, the Manager will use this server instead.
228+
WebhookServer *webhook.Server
229+
223230
// Functions to all for a user to customize the values that will be injected.
224231

225232
// NewCache is the function that will create the cache to be used
@@ -370,6 +377,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
370377
port: options.Port,
371378
host: options.Host,
372379
certDir: options.CertDir,
380+
webhookServer: options.WebhookServer,
373381
leaseDuration: *options.LeaseDuration,
374382
renewDeadline: *options.RenewDeadline,
375383
retryPeriod: *options.RetryPeriod,

pkg/manager/manager_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import (
5353
"sigs.k8s.io/controller-runtime/pkg/reconcile"
5454
"sigs.k8s.io/controller-runtime/pkg/recorder"
5555
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
56+
"sigs.k8s.io/controller-runtime/pkg/webhook"
5657
)
5758

5859
var _ = Describe("manger.Manager", func() {
@@ -273,6 +274,20 @@ var _ = Describe("manger.Manager", func() {
273274
close(done)
274275
})
275276

277+
It("should not initialize a webhook server if Options.WebhookServer is set", func(done Done) {
278+
By("creating a manager with options")
279+
m, err := New(cfg, Options{Port: 9441, WebhookServer: &webhook.Server{Port: 9440}})
280+
Expect(err).NotTo(HaveOccurred())
281+
Expect(m).NotTo(BeNil())
282+
283+
By("checking the server contains the Port set on the webhook server and not passed to Options")
284+
svr := m.GetWebhookServer()
285+
Expect(svr).NotTo(BeNil())
286+
Expect(svr.Port).To(Equal(9440))
287+
288+
close(done)
289+
})
290+
276291
Context("with leader election enabled", func() {
277292
It("should only cancel the leader election after all runnables are done", func() {
278293
m, err := New(cfg, Options{

0 commit comments

Comments
 (0)