File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ package builder
18
18
19
19
import (
20
20
"errors"
21
+ "fmt"
21
22
"net/http"
22
23
"net/url"
23
24
"strings"
@@ -44,6 +45,7 @@ type WebhookBuilder struct {
44
45
config * rest.Config
45
46
recoverPanic bool
46
47
logConstructor func (base logr.Logger , req * admission.Request ) logr.Logger
48
+ err error
47
49
}
48
50
49
51
// WebhookManagedBy returns a new webhook builder.
@@ -57,6 +59,9 @@ func WebhookManagedBy(m manager.Manager) *WebhookBuilder {
57
59
// If the given object implements the admission.Defaulter interface, a MutatingWebhook will be wired for this type.
58
60
// If the given object implements the admission.Validator interface, a ValidatingWebhook will be wired for this type.
59
61
func (blder * WebhookBuilder ) For (apiType runtime.Object ) * WebhookBuilder {
62
+ if blder .apiType != nil {
63
+ blder .err = fmt .Errorf ("For(...) should only be called once, could not assign multiple objects for webhook registration" )
64
+ }
60
65
blder .apiType = apiType
61
66
return blder
62
67
}
@@ -142,7 +147,7 @@ func (blder *WebhookBuilder) registerWebhooks() error {
142
147
if err != nil {
143
148
return err
144
149
}
145
- return nil
150
+ return blder . err
146
151
}
147
152
148
153
// registerDefaultingWebhook registers a defaulting webhook if necessary.
Original file line number Diff line number Diff line change @@ -662,6 +662,24 @@ func runTests(admissionReviewVersion string) {
662
662
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
663
663
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
664
664
})
665
+
666
+ It ("should send an error when trying to register a webhook with more than one For" , func () {
667
+ By ("creating a controller manager" )
668
+ m , err := manager .New (cfg , manager.Options {})
669
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
670
+
671
+ By ("registering the type in the Scheme" )
672
+ builder := scheme.Builder {GroupVersion : testDefaulterGVK .GroupVersion ()}
673
+ builder .Register (& TestDefaulter {}, & TestDefaulterList {})
674
+ err = builder .AddToScheme (m .GetScheme ())
675
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
676
+
677
+ err = WebhookManagedBy (m ).
678
+ For (& TestDefaulter {}).
679
+ For (& TestDefaulter {}).
680
+ Complete ()
681
+ Expect (err ).To (HaveOccurred ())
682
+ })
665
683
}
666
684
667
685
// TestDefaulter.
You can’t perform that action at this time.
0 commit comments