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

Specifying {} to remove nested fields should yield {}, not nil #259

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

Conversation

alexzielenski
Copy link
Contributor

Consider old object:

{
  requiredField: {
      optionalField1: 1
      optionalFIeld2: 2
   }
}

And patch, all with a single owner

{
    requiredField: {}
}

I expect this to yield:

{
    requiredField: {}
}

But today it yields:

{
    requiredField: nil
}

Which results in an error on k8s since nil is not an acceptable value for this requiredField

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: alexzielenski
Once this PR has been reviewed and has the lgtm label, please assign apelisse for approval. For more information see the Kubernetes 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 the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 23, 2024
@alexzielenski
Copy link
Contributor Author

/cc @apelisse

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 23, 2024
@k8s-ci-robot
Copy link
Contributor

@alexzielenski: 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-structured-merge-diff-test d7c26a3 link true /test pull-structured-merge-diff-test

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.

@apelisse
Copy link
Contributor

apelisse commented May 23, 2024

Can we take the opportunity to summarize the various use-cases here that we've found so far? I'm getting a bit lost by the different possibilities and the differences they have. I suspect we should create a table for what happens when nil/empty is sent in various cases because there's a lot of confusion here.

@@ -58,6 +60,9 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
defer w.allocator.Free(l)
// If list is null or empty just return
if l == nil || l.Length() == 0 {
if REMOVEKEEPEMPTYCOLLECTIONS {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we treat separately empty and nil here? Is that part of the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change wasn't directly part of the problem I was trying to solve but I think something like this will be needed to have a consistent solution. The behavior of this right now always results in a nil list even if you specify []

import (
"testing"

"sigs.k8s.io/structured-merge-diff/v4/internal/fixture"
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't the other tests usually importing everything from fixture?

},
APIVersion: `v1`,
Object: `{nestedMap: {}}`,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, so I guess now I'm confused about:

  1. What happens if we apply null instead.
  2. What happens if you have further level of nesting (i.e. you're apply {} when you had {'nested': {'numeric':1}} (I'm guessing the same but would love to see)
  3. How do you entirely remove the field? If null keeps null and {} keep it to empty, then how do you remove it altogether?

Copy link
Contributor Author

@alexzielenski alexzielenski May 23, 2024

Choose a reason for hiding this comment

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

Will add cases for 1 & 2

Re: 3. If you wanted to make {nestedMap: { myField: value }} into {} you would just apply {}. If you applied {} then nestedMap field is included in the toRemove set. If you had applied {nestedMap: {}} then only nestedMap.myField is in the toRemove set, so nestedMap would be retained yielding {nestedMap: {}}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the null case naively I would expect it to keep null if you specified null.

@apelisse
Copy link
Contributor

One last thing, I suspect lists and maps should behave the same. Struct, I'm not entirely sure. maybe they should?

@alexzielenski
Copy link
Contributor Author

One last thing, I suspect lists and maps should behave the same. Struct, I'm not entirely sure. maybe they should?

This bug also affects structs. There are valid CRDs that can be constructed with defined object fields where {} is valid value users should be able to set. Today they cannot

@ybettan
Copy link

ybettan commented Jul 2, 2024

Hello.

Is this fix planned to be merged soon? We currently have to bump our API cause we need to add the +nullable kubebuilder annotation to a bunch of API object and adding a new mutating webhook is just breaking any pointers/slices in the API objects.

Ref: kubernetes-sigs/kernel-module-management#826

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. 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.

None yet

4 participants