Skip to content

Commit e826e01

Browse files
authored
Merge pull request #486 from mengqiy/multicontroller
🐛 stop registering webhooks with same path when adding multiple controllers
2 parents f99287c + 3547c6c commit e826e01

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

pkg/builder/build.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package builder
1818

1919
import (
2020
"fmt"
21+
"net/http"
22+
"net/url"
2123
"strings"
2224

2325
"k8s.io/apimachinery/pkg/runtime"
@@ -280,22 +282,31 @@ func (blder *Builder) doWebhook() error {
280282
mwh := admission.DefaultingWebhookFor(defaulter)
281283
if mwh != nil {
282284
path := generateMutatePath(gvk)
283-
log.Info("Registering a mutating webhook",
284-
"GVK", gvk,
285-
"path", path)
286285

287-
blder.mgr.GetWebhookServer().Register(path, mwh)
286+
// Checking if the path is already registered.
287+
// If so, just skip it.
288+
if !blder.isAlreadyHandled(path) {
289+
log.Info("Registering a mutating webhook",
290+
"GVK", gvk,
291+
"path", path)
292+
blder.mgr.GetWebhookServer().Register(path, mwh)
293+
}
288294
}
289295
}
290296

291297
if validator, isValidator := blder.apiType.(admission.Validator); isValidator {
292298
vwh := admission.ValidatingWebhookFor(validator)
293299
if vwh != nil {
294300
path := generateValidatePath(gvk)
295-
log.Info("Registering a validating webhook",
296-
"GVK", gvk,
297-
"path", path)
298-
blder.mgr.GetWebhookServer().Register(path, vwh)
301+
302+
// Checking if the path is already registered.
303+
// If so, just skip it.
304+
if !blder.isAlreadyHandled(path) {
305+
log.Info("Registering a validating webhook",
306+
"GVK", gvk,
307+
"path", path)
308+
blder.mgr.GetWebhookServer().Register(path, vwh)
309+
}
299310
}
300311
}
301312

@@ -306,6 +317,14 @@ func (blder *Builder) doWebhook() error {
306317
return nil
307318
}
308319

320+
func (blder *Builder) isAlreadyHandled(path string) bool {
321+
h, p := blder.mgr.GetWebhookServer().WebhookMux.Handler(&http.Request{URL: &url.URL{Path: path}})
322+
if p == path && h != nil {
323+
return true
324+
}
325+
return false
326+
}
327+
309328
func generateMutatePath(gvk schema.GroupVersionKind) string {
310329
return "/mutate-" + strings.Replace(gvk.Group, ".", "-", -1) + "-" +
311330
gvk.Version + "-" + strings.ToLower(gvk.Kind)

pkg/builder/build_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,34 @@ var _ = Describe("application", func() {
350350
Expect(w.Body).To(ContainSubstring(`"allowed":true`))
351351
Expect(w.Body).To(ContainSubstring(`"code":200`))
352352
})
353+
354+
It("should allow multiple controllers for the same kind", func() {
355+
By("creating a controller manager")
356+
m, err := manager.New(cfg, manager.Options{})
357+
Expect(err).NotTo(HaveOccurred())
358+
359+
By("registering the type in the Scheme")
360+
builder := scheme.Builder{GroupVersion: testDefaultValidatorGVK.GroupVersion()}
361+
builder.Register(&TestDefaultValidator{}, &TestDefaultValidatorList{})
362+
err = builder.AddToScheme(m.GetScheme())
363+
Expect(err).NotTo(HaveOccurred())
364+
365+
By("creating the 1st controller")
366+
ctrl1, err := ControllerManagedBy(m).
367+
For(&TestDefaultValidator{}).
368+
Owns(&appsv1.ReplicaSet{}).
369+
Build(noop)
370+
Expect(err).NotTo(HaveOccurred())
371+
Expect(ctrl1).NotTo(BeNil())
372+
373+
By("creating the 2nd controller")
374+
ctrl2, err := ControllerManagedBy(m).
375+
For(&TestDefaultValidator{}).
376+
Owns(&appsv1.ReplicaSet{}).
377+
Build(noop)
378+
Expect(err).NotTo(HaveOccurred())
379+
Expect(ctrl2).NotTo(BeNil())
380+
})
353381
})
354382

355383
Describe("Start with SimpleController", func() {

0 commit comments

Comments
 (0)