Skip to content

Commit fc15d4a

Browse files
committed
Introduce WebhookFor
1 parent 8196239 commit fc15d4a

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

alias.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ var (
105105
// NewControllerManagedBy returns a new controller builder that will be started by the provided Manager.
106106
NewControllerManagedBy = builder.ControllerManagedBy
107107

108+
// NewWebhookManagedBy returns a new webhook builder that will be started by the provided Manager.
109+
// Deprecated: Use NewWebhookFor instead.
110+
NewWebhookManagedBy = builder.WebhookManagedBy
111+
108112
// NewManager returns a new Manager for creating Controllers.
109113
// Note that if ContentType in the given config is not set, "application/vnd.kubernetes.protobuf"
110114
// will be used for all built-in resources of Kubernetes, and "application/json" is for other types
@@ -154,7 +158,7 @@ var (
154158
SetLogger = log.SetLogger
155159
)
156160

157-
// NewWebhookManagedBy returns a new webhook builder that will be started by the provided Manager.
158-
func NewWebhookManagedBy[T runtime.Object](m manager.Manager) *builder.WebhookBuilder[T] {
159-
return builder.WebhookManagedBy[T](m)
161+
// NewWebhookFor returns a new webhook builder for the provided type T.
162+
func NewWebhookFor[T runtime.Object](mgr manager.Manager, obj T) *builder.WebhookBuilder[T] {
163+
return builder.WebhookFor(mgr, obj)
160164
}

examples/builtins/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func main() {
6060
os.Exit(1)
6161
}
6262

63-
if err := ctrl.NewWebhookManagedBy[*corev1.Pod](mgr).
63+
if err := ctrl.NewWebhookFor(mgr, &corev1.Pod{}).
6464
WithDefaulter(&podAnnotator{}).
6565
WithValidator(&podValidator{}).
6666
Complete(); err != nil {

examples/crd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func main() {
129129
os.Exit(1)
130130
}
131131

132-
err = ctrl.NewWebhookManagedBy[*api.ChaosPod](mgr).
132+
err = ctrl.NewWebhookFor(mgr, &api.ChaosPod{}).
133133
Complete()
134134
if err != nil {
135135
setupLog.Error(err, "unable to create webhook")

pkg/builder/example_webhook_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ func ExampleWebhookBuilder() {
4141
}
4242

4343
err = builder.
44-
WebhookManagedBy[runtime.Object](mgr). // Create the WebhookManagedBy
45-
For(&examplegroup.ChaosPod{}). // ChaosPod is a CRD.
44+
WebhookFor[runtime.Object](mgr, &examplegroup.ChaosPod{}). // Create the WebhookManagedBy
4645
Complete()
4746
if err != nil {
4847
log.Error(err, "could not create webhook")

pkg/builder/webhook.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ type WebhookBuilder[T runtime.Object] struct {
5656
}
5757

5858
// WebhookManagedBy returns a new webhook builder.
59-
func WebhookManagedBy[T runtime.Object](m manager.Manager) *WebhookBuilder[T] {
59+
// Deprecated: Use WebhookFor instead.
60+
func WebhookManagedBy(m manager.Manager) *WebhookBuilder[runtime.Object] {
61+
return &WebhookBuilder[runtime.Object]{mgr: m}
62+
}
63+
64+
// WebhookFor return a new webhook builder for the provided type T.
65+
func WebhookFor[T runtime.Object](m manager.Manager, object T) *WebhookBuilder[T] {
6066
return &WebhookBuilder[T]{mgr: m}
6167
}
6268

pkg/builder/webhook_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func runTests(admissionReviewVersion string) {
9696
err = builder.AddToScheme(m.GetScheme())
9797
ExpectWithOffset(1, err).NotTo(HaveOccurred())
9898

99-
err = WebhookManagedBy[runtime.Object](m).
99+
err = WebhookManagedBy(m).
100100
For(&TestDefaulter{}).
101101
WithDefaulter(&TestCustomDefaulter{}).
102102
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
@@ -173,7 +173,7 @@ func runTests(admissionReviewVersion string) {
173173
ExpectWithOffset(1, err).NotTo(HaveOccurred())
174174

175175
customPath := "/custom-defaulting-path"
176-
err = WebhookManagedBy[runtime.Object](m).
176+
err = WebhookManagedBy(m).
177177
For(&TestDefaulter{}).
178178
WithDefaulter(&TestCustomDefaulter{}).
179179
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
@@ -251,7 +251,7 @@ func runTests(admissionReviewVersion string) {
251251
err = builder.AddToScheme(m.GetScheme())
252252
ExpectWithOffset(1, err).NotTo(HaveOccurred())
253253

254-
err = WebhookManagedBy[runtime.Object](m).
254+
err = WebhookManagedBy(m).
255255
For(&TestDefaulter{}).
256256
WithDefaulter(&TestCustomDefaulter{}).
257257
RecoverPanic(true).
@@ -315,7 +315,7 @@ func runTests(admissionReviewVersion string) {
315315
err = builder.AddToScheme(m.GetScheme())
316316
ExpectWithOffset(1, err).NotTo(HaveOccurred())
317317

318-
err = WebhookManagedBy[runtime.Object](m).
318+
err = WebhookManagedBy(m).
319319
For(&TestValidator{}).
320320
WithValidator(&TestCustomValidator{}).
321321
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
@@ -434,7 +434,7 @@ func runTests(admissionReviewVersion string) {
434434
ExpectWithOffset(1, err).NotTo(HaveOccurred())
435435

436436
customPath := "/custom-validating-path"
437-
err = WebhookManagedBy[runtime.Object](m).
437+
err = WebhookManagedBy(m).
438438
For(&TestValidator{}).
439439
WithValidator(&TestCustomValidator{}).
440440
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
@@ -513,7 +513,7 @@ func runTests(admissionReviewVersion string) {
513513
err = builder.AddToScheme(m.GetScheme())
514514
ExpectWithOffset(1, err).NotTo(HaveOccurred())
515515

516-
err = WebhookManagedBy[runtime.Object](m).
516+
err = WebhookManagedBy(m).
517517
For(&TestValidator{}).
518518
WithValidator(&TestCustomValidator{}).
519519
RecoverPanic(true).
@@ -579,7 +579,7 @@ func runTests(admissionReviewVersion string) {
579579
err = builder.AddToScheme(m.GetScheme())
580580
ExpectWithOffset(1, err).NotTo(HaveOccurred())
581581

582-
err = WebhookManagedBy[runtime.Object](m).
582+
err = WebhookManagedBy(m).
583583
For(&TestValidator{}).
584584
WithValidator(&TestCustomValidator{}).
585585
Complete()
@@ -670,7 +670,7 @@ func runTests(admissionReviewVersion string) {
670670
err = builder.AddToScheme(m.GetScheme())
671671
ExpectWithOffset(1, err).NotTo(HaveOccurred())
672672

673-
err = WebhookManagedBy[runtime.Object](m).
673+
err = WebhookManagedBy(m).
674674
For(&TestDefaulter{}).
675675
For(&TestDefaulter{}).
676676
Complete()
@@ -688,7 +688,7 @@ func runTests(admissionReviewVersion string) {
688688
err = builder.AddToScheme(m.GetScheme())
689689
ExpectWithOffset(1, err).NotTo(HaveOccurred())
690690

691-
err = WebhookManagedBy[runtime.Object](m).
691+
err = WebhookManagedBy(m).
692692
For(&TestDefaultValidator{}).
693693
WithDefaulter(&TestCustomDefaultValidator{}).
694694
WithValidator(&TestCustomDefaultValidator{}).
@@ -773,7 +773,7 @@ func runTests(admissionReviewVersion string) {
773773

774774
validatingCustomPath := "/custom-validating-path"
775775
defaultingCustomPath := "/custom-defaulting-path"
776-
err = WebhookManagedBy[runtime.Object](m).
776+
err = WebhookManagedBy(m).
777777
For(&TestDefaultValidator{}).
778778
WithDefaulter(&TestCustomDefaultValidator{}).
779779
WithValidator(&TestCustomDefaultValidator{}).
@@ -878,7 +878,7 @@ func runTests(admissionReviewVersion string) {
878878
err = builder.AddToScheme(m.GetScheme())
879879
ExpectWithOffset(1, err).NotTo(HaveOccurred())
880880

881-
err = WebhookManagedBy[runtime.Object](m).
881+
err = WebhookManagedBy(m).
882882
For(&TestDefaultValidator{}).
883883
WithDefaulter(&TestCustomDefaultValidator{}).
884884
WithValidator(&TestCustomDefaultValidator{}).
@@ -901,7 +901,7 @@ func runTests(admissionReviewVersion string) {
901901
err = builder.AddToScheme(m.GetScheme())
902902
ExpectWithOffset(1, err).NotTo(HaveOccurred())
903903

904-
err = WebhookManagedBy[runtime.Object](m).
904+
err = WebhookManagedBy(m).
905905
For(&TestDefaulter{}).
906906
WithDefaulter(&TestCustomDefaulter{}).
907907
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
@@ -924,7 +924,7 @@ func runTests(admissionReviewVersion string) {
924924
err = builder.AddToScheme(m.GetScheme())
925925
ExpectWithOffset(1, err).NotTo(HaveOccurred())
926926

927-
err = WebhookManagedBy[runtime.Object](m).
927+
err = WebhookManagedBy(m).
928928
For(&TestValidator{}).
929929
WithValidator(&TestCustomValidator{}).
930930
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {

0 commit comments

Comments
 (0)