Skip to content

Commit f8c6ee4

Browse files
committed
pkg/manager: add Options.WebhookServer to allow external configuration of a webhook server
Signed-off-by: Eric Stroczynski <[email protected]>
1 parent e10bf72 commit f8c6ee4

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

pkg/manager/manager.go

Lines changed: 23 additions & 5 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
@@ -358,7 +365,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
358365
return nil, err
359366
}
360367

361-
return &controllerManager{
368+
cm := &controllerManager{
362369
cluster: cluster,
363370
recorderProvider: recorderProvider,
364371
resourceLock: resourceLock,
@@ -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,
@@ -379,7 +387,17 @@ func New(config *rest.Config, options Options) (Manager, error) {
379387
gracefulShutdownTimeout: *options.GracefulShutdownTimeout,
380388
internalProceduresStop: make(chan struct{}),
381389
leaderElectionStopped: make(chan struct{}),
382-
}, nil
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
383401
}
384402

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

pkg/manager/manager_test.go

Lines changed: 21 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,26 @@ 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 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+
289+
By("checking the server contains the Port set on the webhook server and not passed to Options")
290+
svr := m.GetWebhookServer()
291+
Expect(svr).NotTo(BeNil())
292+
Expect(svr.Port).To(Equal(9440))
293+
294+
close(done)
295+
})
296+
276297
Context("with leader election enabled", func() {
277298
It("should only cancel the leader election after all runnables are done", func() {
278299
m, err := New(cfg, Options{

0 commit comments

Comments
 (0)