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

KEP-4222: Decode CBOR to UnstructuredList as UnstructuredJSONScheme does. #124775

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

Conversation

benluddy
Copy link
Contributor

@benluddy benluddy commented May 9, 2024

Decoding to map[string]interface{} and passing the result to UnstructuredList's SetUnstructuredContent method does not produce objects that are identical to those produced by UnstructuredJSONScheme's decode method. UnstructuredJSONScheme's decode:

  1. removes the "items" key from the map in its Object field

  2. sets "apiVersion" and "kind" (determined heuristically from the list's GVK) on elements of its Items slice that were not serialized with a nonempty string "apiVersion" and "kind"

  3. returns a missing kind error if any element is missing "kind"

What type of PR is this?

/kind cleanup
/sig api-machinery

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?


Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

- [KEP]: https://kep.k8s.io/4222

@k8s-ci-robot
Copy link
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@k8s-ci-robot
Copy link
Contributor

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

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.

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. labels May 9, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: benluddy
Once this PR has been reviewed and has the lgtm label, please assign smarterclayton 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 cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels May 9, 2024
@benluddy
Copy link
Contributor Author

benluddy commented May 9, 2024

xref #123783

@benluddy benluddy marked this pull request as ready for review May 9, 2024 19:35
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 9, 2024
@k8s-ci-robot k8s-ci-robot requested a review from wojtek-t May 9, 2024 19:35
Copy link
Member

@stlaz stlaz left a comment

Choose a reason for hiding this comment

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

The PR appears to be doing what it says is doing 🙂

Would this allow us to do some kind of a roundtrip test comparison along with JSON for the unstructured lists?

@@ -156,6 +156,33 @@ func TestEncode(t *testing.T) {
}
},
},
{
Copy link
Member

Choose a reason for hiding this comment

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

This was possible even before this PR, correct? So it's just additional data related to the new code IIUC

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, although the behavior of the "list element missing kind", "list element missing version", and item GVK heuristic cases would have had different expectations since the original implementation had been calling SetUnstructuredContent.

@benluddy
Copy link
Contributor Author

Would this allow us to do some kind of a roundtrip test comparison along with JSON for the unstructured lists?

Yes! I'm wrapping up a PR that does roundtrip comparisons vs. JSON for all of the built-in GVKs to Unstructured and back, and I have a checklist entry to add a similar test that will operate on fuzzed Unstructured/UnstructuredList.

The object you get with JSON actually varies between strict and non-strict decodes (#123783). I don't think that was intentional, so I'm not carrying the behavior over to this decoder.

var cast bool
items, cast = uncast.([]interface{})
if !cast {
strict, lax = nil, fmt.Errorf("items field of UnstructuredList must be encoded as an array or null if present")
Copy link
Contributor

Choose a reason for hiding this comment

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

why is strict nil and lax an error? should it be the other way around?

items, cast = uncast.([]interface{})
if !cast {
strict, lax = nil, fmt.Errorf("items field of UnstructuredList must be encoded as an array or null if present")
return
Copy link
Contributor

Choose a reason for hiding this comment

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

if someone is using lax, they'll get no error and also no content. What would someone do with that return? is that a useful return?

Given this is a new serializer down an error case, do we have any viable use-case for not failing and also not returning a useable list? Is it better to always fail?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like it's better to fail both lax and strict. We can't make sense of how to decode in these cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add an explanation about these errors in the comments. It does fail in both cases. The last return value is meant to be interpreted according to the usual Go convention, and indicates that the unmarshal operation failed entirely.

The signature is roughly based on UnmarshalStrict from sigs.k8s.io/json.

object, cast := items[i].(map[string]interface{})
if !cast {
strict, lax = nil, fmt.Errorf("elements of the items field of UnstructuredList must be encoded as a map")
return
Copy link
Contributor

Choose a reason for hiding this comment

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

similar to the above question.

Decoding to map[string]interface{} and passing the result to UnstructuredList's
SetUnstructuredContent method does not produce objects that are identical to those produced by
UnstructuredJSONScheme's decode method. UnstructuredJSONScheme's decode:

1. removes the "items" key from the map in its Object field

2. sets "apiVersion" and "kind" (determined heuristically from the list's GVK) on elements of its
Items slice that were not serialized with a nonempty string "apiVersion" and "kind"

3. returns a missing kind error if any element is missing "kind"
@fedebongio
Copy link
Contributor

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels May 14, 2024
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. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants