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 custom defaulter: avoid deleting unknown fields (zero change patch) #2982

Merged

Conversation

trasc
Copy link
Contributor

@trasc trasc commented Oct 14, 2024

When creating a patch for the custom defaulter, don't return the removals generated by object decoding with the local scheme.

Otherwise, the patch will include a remove instruction for fields that the go type doesn't contain.

This could happen when building a webhook for a resource that belongs to another project (including k8s types).

This PR stops the defaulter from pruning the fields that are not recognized in the local scheme.
When doing this we are creating two patches one before and one after the custom defaulter call and only send back the remove operations that are not present in both patches.

The user can opt-out from this new behavior by specifying the DefaulterRemoveUnknownFields option when calling WithCustomDefaulter.

(This is a rework of #2931)

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 14, 2024
@trasc
Copy link
Contributor Author

trasc commented Oct 14, 2024

/cc @alculquicondor
/cc @sbueringer
/cc @alvaroaleman

@alculquicondor
Copy link
Contributor

For context to c-r maintainers, we have been discussing what alternatives we have for a generic defaulter that doesn't drop unknown fields. Here is the thread for additional context kubernetes-sigs/kueue#3194 (comment)

Taking @mimowo's words, we would be happy to first merge a solution in kueue and let it mature there before moving it here. But we would appreciate your input to the approaches.

@alvaroaleman
Copy link
Member

@alculquicondor @trasc @mimowo We (Stefan, Vince and I) had some discussion around #2932 vs this and we landed at this here being better but we aren't 100% sure if its compatible in all cases and currently don't have time to spend on that question, which is why we would like an opt-out for what is suggested here (but on by default).

Does that sound reasonable to you all?

@trasc
Copy link
Contributor Author

trasc commented Oct 23, 2024

@alculquicondor @trasc @mimowo We (Stefan, Vince and I) had some discussion around #2932 vs this and we landed at this here being better but we aren't 100% sure if its compatible in all cases and currently don't have time to spend on that question, which is why we would like an opt-out for what is suggested here (but on by default).

Does that sound reasonable to you all?

Sure, I'll ping you when I have something in that direction.

@alculquicondor
Copy link
Contributor

Thanks for considering the options!

Do you have any particular guideline on how the opt-out mechanism could look like? Should we have a different method in the factory? Or should the method WithDefaulter accept some options? Any other suggestion?

Other than that, I hope the overhead of the approach is not significant. Fortunately, a new encoding is coming to k/k CRDs https://github.com/kubernetes/enhancements/tree/master/keps/sig-api-machinery/4222-cbor-serializer#motivation

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 24, 2024
@trasc
Copy link
Contributor Author

trasc commented Oct 24, 2024

I think

... should the method WithDefaulter accept some options ...

it leave room to maybe add an additional option that removes the add zero values.

@trasc
Copy link
Contributor Author

trasc commented Oct 24, 2024

@alvaroaleman please have a look at my DefaulterPreserveUnknownFields proposal when you have some time.

Also please let me know if we can do something about the pull-controller-runtime-apidiff check,

changed from 
func(*k8s.io/apimachinery/pkg/runtime.Scheme, k8s.io/apimachinery/pkg/runtime.Object, CustomDefaulter) *Webhook to 
func(*k8s.io/apimachinery/pkg/runtime.Scheme, k8s.io/apimachinery/pkg/runtime.Object, CustomDefaulter, ...defaulterOption) *Webhook

is a flase positive in my opinion, as you don't need to actually do something to old code in order to use the new version.

@trasc trasc force-pushed the dont-drop-missing-in-scheme branch from 72ac9bd to e652815 Compare October 25, 2024 06:51
@trasc trasc force-pushed the dont-drop-missing-in-scheme branch from 4a124c8 to ab4e8c9 Compare October 25, 2024 18:34
@alculquicondor
Copy link
Contributor

/lgtm

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

LGTM label has been added.

Git tree hash: dc8f633f86d5467de1824324a9004cf5c10d3d61

@alvaroaleman alvaroaleman added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Oct 26, 2024
@sbueringer
Copy link
Member

sbueringer commented Nov 1, 2024

In general okay. One comment about the option name and documentation

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 4, 2024
@sbueringer
Copy link
Member

sbueringer commented Nov 4, 2024

Thank you!!

/lgtm

@alvaroaleman Keeping the hold in case you want to take a final look after the last change

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

LGTM label has been added.

Git tree hash: f8f793a6960067cffea8b08e87822f9d14246b85

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 5, 2024
@alvaroaleman
Copy link
Member

PR needs rebase.

LGTM modulo the rebase

@trasc trasc force-pushed the dont-drop-missing-in-scheme branch from b5419bf to fb7b6cd Compare November 5, 2024 19:58
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 5, 2024
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 5, 2024
@trasc
Copy link
Contributor Author

trasc commented Nov 5, 2024

Rebased

@k8s-ci-robot
Copy link
Contributor

@trasc: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-controller-runtime-apidiff fb7b6cd link false /test pull-controller-runtime-apidiff

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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

@sbueringer
Copy link
Member

Thx!

/lgtm
/approve
/hold cancel

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Nov 5, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 7302a762ff601db57984c8fa33072a6e68119c8f

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, sbueringer, trasc

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:
  • OWNERS [alvaroaleman,sbueringer]

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 merged commit 48ec3b7 into kubernetes-sigs:main Nov 5, 2024
8 of 9 checks passed
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. size/L Denotes a PR that changes 100-499 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.

5 participants