Skip to content

✨ Add custom path option for webhooks #4845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

damsien
Copy link
Contributor

@damsien damsien commented Jun 1, 2025

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

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 1, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: damsien
Once this PR has been reviewed and has the lgtm label, please assign camilamacedo86 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 1, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @damsien. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

ValidatingWebhookCustomPath string

// DefaultingWebhookCustomPath defines the custom path that will be used by the scaffolded defaulting webhooks
DefaultingWebhookCustomPath string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s great work, thank you! 🎉
The implementation looks solid at a glance.

However, since this introduces API changes, I’ll need a bit more time to review it carefully and fully assess the impact.

A couple of things we should also make sure of before merging:

  • This should be compatible with the alpha generate command — we’ll likely need to include logic to ensure the project is re-scaffolded correctly with the new option.

  • It should also work seamlessly with the Helm charts plugin (helm.kubebuilder.io/v1-alpha) — could you confirm compatibility or add test coverage for that?

Thanks again for the contribution! I’ll follow up with a more detailed review soon.

Copy link
Contributor Author

@damsien damsien Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your quick comment!

we’ll likely need to include logic to ensure the project is re-scaffolded correctly with the new option.

What do you mean by "the project is re-scaffolded"?

could you confirm compatibility or add test coverage for that?

I didn't test it with the helm chart plugin. Also, since I never worked with the helm plugin, I don't really know where to start to confirm the compatibility 😅. Could you please provide me the test file where I should write the new test?

I don't really understand where there should be an incompatibility with the helm plugin since my feature is only related to the webhook file that is created using the CLI (actually it only affects the content of the webhook file, and it does not scaffold any other file). The feature is not related at all with the delivery of the user's operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom path for webhooks
3 participants