kubebuilder
kubebuilder copied to clipboard
:sparkles: Add custom path option for webhooks
trafficstars
Closes #4295 ! This PR brings a way to configure custom webhook path for the defaulting and validating webhooks in the kubebuilder cli.
Two new flags are added for the kubebuilder create webhook command: --defaulting-custom-path & --validating-custom-path. Both can be used independently.
Usage:
kubebuilder create webhook \
--group mygroup.io --kind MyResource --version v1alpha1 \
--defaulting --programmatic-validation \
--defaulting-custom-path=/my-custom-defaulting-webhook-path \
--validating-custom-path=/my-custom-validating-webhook-path
When used, it will:
- Change the
// +kubebuilder:webhook:pathgo marker. - Add
WithValidatorCustomPath(...).or/andWithDefaulterCustomPath(...).to the webhook setuper function.
Result from the example above:
func SetupMyResourceWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).For(&mygroupiov1alpha1.MyResource{}).
WithValidator(&MyResourceCustomValidator{}).
WithDefaulter(&MyResourceCustomDefaulter{}).
WithValidatorCustomPath("/my-custom-validating-webhook-path").
WithDefaulterCustomPath("/my-custom-defaulting-webhook-path").
Complete()
}
// +kubebuilder:webhook:path=/my-custom-defaulting-webhook-path,mutating=true,failurePolicy=fail,sideEffects=None,groups=mygroup.io,resources=myresources,verbs=create;update,versions=v1alpha1,name=mmyresource-v1alpha1.kb.io,admissionReviewVersions=v1
...
// +kubebuilder:webhook:path=/my-custom-validating-webhook-path,mutating=false,failurePolicy=fail,sideEffects=None,groups=mygroup.io,resources=myresources,verbs=create;update,versions=v1alpha1,name=vmyresource-v1alpha1.kb.io,admissionReviewVersions=v1