Skip to content

🐛Use k8s.io/apimachinery/pkg/util/json to unmarshal in fakeclient #3208

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 2 commits into
base: main
Choose a base branch
from

Conversation

troy0820
Copy link
Member

@troy0820 troy0820 commented May 7, 2025

This will use k8s.io/apimachinery/pkg/util/json which calls sigs.k8s.io/json to UnmarshalWithCaseSensitivePreserveInts in the fakeclient.

Resolves #3207

NOTE: https://pkg.go.dev/sigs.k8s.io/json#UnmarshalCaseSensitivePreserveInts

UnmarshalCaseSensitivePreserveInts parses the JSON-encoded data and stores the result in the value pointed to by v.

UnmarshalCaseSensitivePreserveInts matches the behavior of encoding/json#Unmarshal, with the following changes:

When unmarshaling into a struct, JSON keys must case-sensitively match json tag names (for tagged struct fields) or struct field names (for untagged struct fields), or they are treated as unknown fields and discarded.

When unmarshaling a number into an interface value, it is unmarshaled as an int64 if the JSON data does not contain a "." character and parses as an integer successfully and does not overflow int64. Otherwise, the number is unmarshaled as a float64.

If a syntax error is returned, it will not be of type encoding/json#SyntaxError, but will be recognizeable by this package's IsSyntaxError() function

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 7, 2025
@k8s-ci-robot k8s-ci-robot requested review from FillZpp and inteon May 7, 2025 15:30
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: troy0820
Once this PR has been reviewed and has the lgtm label, please assign vincepri 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 the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label May 7, 2025
@troy0820 troy0820 force-pushed the troy0820/api-machinery-marshal branch from 43929d1 to dde4486 Compare May 7, 2025 15:32
go.mod Outdated
@@ -32,6 +32,8 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3
Copy link
Member

Choose a reason for hiding this comment

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

Could it be in with the others ones?

Copy link
Member Author

@troy0820 troy0820 May 8, 2025

Choose a reason for hiding this comment

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

Yes, this is actually in k8s.io/apimachinery/pkg/util/json which has an Unmarshal method. That method calls the method UnmarshalCaseSensitivePreserveInts

https://github.com/kubernetes/apimachinery/blob/d56afd172ac5d1ce5afe66caedf3e663a56433f5/pkg/util/json/json.go#L45C1-L47C2

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 8, 2025
@troy0820 troy0820 force-pushed the troy0820/api-machinery-marshal branch from 73e4dac to a4ac626 Compare May 8, 2025 15:23
@troy0820 troy0820 changed the title 🐛Use sigs.k8s.io/json to unmarshal in fakeclient 🐛Use k8s.io/apimachinery/pkg/util/json to unmarshal in fakeclient May 8, 2025
@@ -58,6 +57,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
Copy link
Member

Choose a reason for hiding this comment

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

Lets please add a test for the issue, otherwise this will likely break again in the future

Copy link
Member

@sbueringer sbueringer May 9, 2025

Choose a reason for hiding this comment

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

[I might be mixing up stuff]

IIRC this is heavily aligned to the upstream client-go fake client and we want to avoid differences in behavior.

What is the upstream fake client doing here? (or is it not using the json package at all and this is just from additional code that we have here)

Copy link
Member Author

Choose a reason for hiding this comment

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

IIRC this is heavily aligned to the upstream client-go fake client and we want to avoid differences in behavior.

What is the upstream fake client doing here? (or is it not using the json package at all and this is just from additional code that we have here)

I am not seeing anything in client-go fakeclient where this would be present. I may be overlooking it but it seems they use k8s.io/apimachinery/pkg/util/json in client-go but not for the fakeclient. They are using instances of encoding/json to Marshal for the ApplyConfigurations.

Copy link
Member

Choose a reason for hiding this comment

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

@sbueringer I think you are confusing this with json patch, I don't think there is any reason for upstream to do json encoding in the fake client. We need it because of edge cases like "Object gets stored and retrieved in different representations like unstructured/structure", that issue does not exist in upstream fake clients as they don't support that

Copy link
Member

@sbueringer sbueringer May 9, 2025

Choose a reason for hiding this comment

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

Sounds good, thx for the additional info!

I know that it's not the json patch case, was just wondering if it's a similar case

@troy0820
Copy link
Member Author

troy0820 commented May 9, 2025

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 9, 2025
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/hold Indicates that a PR should not merge because someone has issued a /hold command. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FakeClient: use apiMachinery json when serializing/deserializing
5 participants