Skip to content
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

🐛 fix nil Decoder in multiValidating and multiMutating handlers by implementing DecoderInjector #1502

Conversation

d-honeybadger
Copy link
Contributor

TL;DR
Webhooks with multiValidating and multiMutating handlers currently fail to inject decoders into their sub-handlers, resulting in nil pointer panics. This PR is fixing it.

Full description
Before the change in #1307, webhook method webhook.InjectScheme was called before webhook.InjectFunc, meaning that decoder intstantiation was happening before injecting it by webhook.InjectFunc.
After that PR (specifically, the changes to controllerManager.SetFields in pkg/manager/internal.go) , the order has changed, and now webhook.InjectFunc gets called before webhook.InjectScheme, meaning that we webhook.InjectFunc injects a nil decoder.
It appears that such re-ordering was anticipated, because in the webhook.InjectScheme it tries to inject decoder again, just in case, with this comment:

// inject the decoder here too, just in case the order of calling this is not
// scheme first, then inject func
if w.Handler != nil {
	if _, err := InjectDecoderInto(w.GetDecoder(), w.Handler); err != nil {
		return err
	}
}

This second attempt at decoder injection works just fine for regular webhooks, but fails if the weebhook's handler is a multiValidating or multiMutating handler, because those handlers do not implement DecoderInjector interface.
Note that for regular handlers, it is the responsibility of the user to implement DecoderInjector interface, but because both multiValidating and multiMutating are unexported types, a user cannot do so for them.

The fix proposed in this PR is not the only possible solution. Another one could be to switch back the order of inject.InjectorInto and cm.cluster.SetFields in

func (cm *controllerManager) SetFields(i interface{}) error {
. And yet another one is proposed in #1437

Link to the issue that was filed for this bug: #1487

Tagging @porridge since you were looking at that issue and @alvaroaleman as the author of the PR that re-ordered webhook.InjectScheme and webhook.InjectFunc.

Happy to make any changes or implement a different fix for this bug.

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label May 1, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @d-honeybadger!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label May 1, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @d-honeybadger. 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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 1, 2021
Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

The fix proposed in this PR is not the only possible solution. Another one could be to switch back the order of inject.InjectorInto and cm.cluster.SetFields in

@d-honeybadger would you mind to also do that, just on the off chance that something else also relied on the order there?

/ok-to-test


ctx, cancel := context.WithCancel(context.Background())
go func() {
_ = server.Start(ctx)
Copy link
Member

Choose a reason for hiding this comment

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

Please check the return

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels May 1, 2021
@d-honeybadger d-honeybadger force-pushed the fix/multi-webhook-decoder-injection branch from 0fa8eac to e617f06 Compare May 1, 2021 14:28
@d-honeybadger
Copy link
Contributor Author

@d-honeybadger would you mind to also do that, just on the off chance that something else also relied on the order there?

👍 done!
And fixed a linting problem.

@d-honeybadger d-honeybadger force-pushed the fix/multi-webhook-decoder-injection branch from e617f06 to 21ac80f Compare May 1, 2021 14:36
@alvaroaleman alvaroaleman added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label May 1, 2021
Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

thanks!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 1, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, d-honeybadger

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

The pull request process is described 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 the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 1, 2021
@k8s-ci-robot k8s-ci-robot merged commit 3b25aa6 into kubernetes-sigs:master May 1, 2021
@k8s-ci-robot k8s-ci-robot added this to the v0.9.x milestone May 1, 2021
aayushrangwala pushed a commit to aayushrangwala/controller-runtime that referenced this pull request May 5, 2021
…plementing DecoderInjector (kubernetes-sigs#1502)

* multiValidating and multiMutating handlers implement DecoderInjector

* set fields before injecting in controllerManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants