File tree 2 files changed +23
-1
lines changed
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ type WebhookBuilder struct {
44
44
config * rest.Config
45
45
recoverPanic bool
46
46
logConstructor func (base logr.Logger , req * admission.Request ) logr.Logger
47
+ err error
47
48
}
48
49
49
50
// WebhookManagedBy returns a new webhook builder.
@@ -57,6 +58,9 @@ func WebhookManagedBy(m manager.Manager) *WebhookBuilder {
57
58
// If the given object implements the admission.Defaulter interface, a MutatingWebhook will be wired for this type.
58
59
// If the given object implements the admission.Validator interface, a ValidatingWebhook will be wired for this type.
59
60
func (blder * WebhookBuilder ) For (apiType runtime.Object ) * WebhookBuilder {
61
+ if blder .apiType != nil {
62
+ blder .err = errors .New ("For(...) should only be called once, could not assign multiple objects for webhook registration" )
63
+ }
60
64
blder .apiType = apiType
61
65
return blder
62
66
}
@@ -142,7 +146,7 @@ func (blder *WebhookBuilder) registerWebhooks() error {
142
146
if err != nil {
143
147
return err
144
148
}
145
- return nil
149
+ return blder . err
146
150
}
147
151
148
152
// 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