-
Notifications
You must be signed in to change notification settings - Fork 1.2k
MergeFrom can return invalid patch data when using int64 properties #2603
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
Comments
/kind bug That's not good. The actual patch generation is here: controller-runtime/pkg/client/patch.go Line 136 in 2154ffb
I would assume the root of the problem is that the int64 ends up getting converted to a float64 and the float64 can not represent the difference. We can try to open an issue in the json-patch repo to see if this can be fixed. |
I filled the bug here because I’m not sure this can be really considered a bug in |
@jfremy actually on second thought I am unsure if this is solveable at all. Because at the end of the day, patch or not, this will get serialized to json and sent to the apiserver. At that point, the golang json (de-)serializer will end up truncating portions of large int64s. So the only ways on how this could be fixed that I can think of are:
All of these do not seem very practical unfortunately. Do you have other ideas? And out of curiosity, in what context did you run into this? It might be worth reporting this in kubernetes/kubernetes btw, as this issue doesn't seem specific to controller-runtime. |
I have a CRD where I store a "seed" in an int64 (so I can end up with values in the whole int64 range). Regarding the scope of the issue, I think it is limited to
I've seen the API server handle updates with large int64 correctly so that part seems to work. Using the |
Yeah you are right, it actually works correctly in the golang json marshaller, seems I didn't look correctly yesterday. But that just brings us back to square zero, we need a fix in |
Unfortunately, I agree. I don't see a way to fix it that does not imply modifying json-patch or switching to a different library that does what we want |
Created evanphx/json-patch#189 to ask about the possibilities and stance there |
I don’t believe this is a known issue (at least, did not find mention of it) so I wanted to raise it.
This bug impacts uses of
client.MergeFrom
but also helper function likeCreateOrPatch
(which usesMergeFrom
).Summary: If you have an object (K8s or CRD) that has int64 properties, the
MergeFrom
call will very likely generate invalid patch data if the values considered cannot be exactly encoded in a float64.The reason is that the
jsonpatch
library used to generate the patch is unaware of the underlying type and therefore uses “standard” json marshaling/unmarshaling. So when unmarshaling numbers (int/float), they are stored as float64.If the number cannot be exactly encoded into a float64, we start seeing incorrect behaviors
The current flow to generate a patch is
There are two kinds of impact:
I don’t consider it a bug in
jsonpatch
. The library is following the JSON standardLooking at the issue, I think that fixing it would require to switch to another json patch generation implementation that is aware of the underlying data types but that’s probably a big change.
I’m including a simple test program to illustrate the issue - MergeFrom generates an empty patch despite the change (I’ve used a Job because it was an existing object with an int64 property).
The text was updated successfully, but these errors were encountered: