kubebuilder icon indicating copy to clipboard operation
kubebuilder copied to clipboard

:sparkles: Add custom path option for webhooks

Open damsien opened this issue 5 months ago • 2 comments
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:

  1. Change the // +kubebuilder:webhook:path go marker.
  2. Add WithValidatorCustomPath(...). or/and WithDefaulterCustomPath(...). 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

damsien avatar Jun 01 '25 07:06 damsien