@@ -18,6 +18,8 @@ package builder
1818
1919import (
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+
309328func generateMutatePath (gvk schema.GroupVersionKind ) string {
310329 return "/mutate-" + strings .Replace (gvk .Group , "." , "-" , - 1 ) + "-" +
311330 gvk .Version + "-" + strings .ToLower (gvk .Kind )
0 commit comments