fix: preserve integer precision in kube-applier JSON (un)marshalling #7071
Pipeline controller notification
This repo is configured to use the pipeline controller. Second-stage tests will be triggered either automatically or after lgtm label is added, depending on the repository configuration. The pipeline controller will automatically detect which contexts are required and will utilize /test Prow commands to trigger the second stage.
For optional jobs, comment /test ? to see a list of all defined jobs. To trigger manually all jobs from second stage use /pipeline required command.
This repository is configured in: automatic mode
Decoder.Decode accepts a valid JSON prefix and ignores trailing tokens. Because RawExtension.MarshalJSON can return its Raw bytes without validating them, a payload such as {"n":1} garbage can now compare equal and suppress a write-back, whereas the removed json.Unmarshal path rejected it. Keep the strict whole-input validation before doing the number-preserving decode.
The PR is described and titled as a RawExtension precision fix, but this adds a repository-wide Go toolchain policy change; combined with the unrelated swift-recorder and Helm-test changes, it mixes multiple tasks and makes the review scope unclear. Please split these changes into focused PRs or explain why they are required by this fix.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Strict whole-input JSON validation must be restored before approval.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> · 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (2)</strong></summary>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
Updates NeedsUpdate to preserve large-integer precision while remaining numeric-format invariant. The PR also includes unrelated portability and toolchain changes. Review identified a JSON trailing-input validation issue and scope concerns.
Changes:
| File | Description |
|---|---|
| tooling/helmtest/testrunner/system_reserved_test.go | Skips GNU-specific tests outside Linux. |
| swift-recorder/pkg/testutil/testutil.go | Adds a resolved temporary-directory helper. |
| swift-recorder/pkg/recorder/recorder_test.go | Uses portable temporary directories. |
| swift-recorder/cmd/cmd_test.go | Uses the shared temporary-directory helper. |
| Makefile | Pins Make-invoked Go commands to the configured toolchain. |
| internal/controllerutils/needs_update.go | Implements precision-preserving JSON comparison. |
| internal/controllerutils/needs_update_test.go | Adds large-integer and numeric-format test coverage. |
</details>
---
💡 Configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>
<!-- ccr-overview-v2 -->
Copilot review overview
🔵 Needs a closer look
Validate the entire JSON payload before treating comparisons as equal.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (1)</strong></summary>
</details>
<details>
<summary><strong>Resolved since last review (1)</strong></summary>
</details>
done
big.Rat.SetString expands exponent notation into an arbitrary-precision rational. Because RawExtension can carry arbitrary Kubernetes JSON, a short valid value such as 1e1000000000 can make this reconcile-path comparison allocate an enormous integer (and two differently formatted large-exponent values reach this path), risking controller OOM/CPU exhaustion. Bound the exponent/expansion before calling SetString, or compare normalized decimal representations without materializing powers of ten.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Unbounded exponent expansion may cause excessive memory or CPU usage during reconciliation.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (1)</strong></summary>
</details>
<details>
<summary><strong>Resolved since last review (1)</strong></summary>
</details>
Scheduling tests matching the pipeline_run_if_changed or not excluded by pipeline_skip_if_only_changed parameters:
/test e2e-parallel
done
boundedExponent does not actually bound the minimum signed exponent: strconv.Atoi accepts the minimum int value, and negating it overflows so exp remains negative and passes exp <= maxJSONNumberExponent. A valid literal such as 1e-9223372036854775808 can therefore still reach big.Rat.SetString despite the expansion guard; compare both signed bounds before taking an absolute value.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Fix the exponent-bound check overflow at the minimum signed integer value.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (1)</strong></summary>
</details>
<details>
<summary><strong>Resolved since last review (1)</strong></summary>
</details>
Scheduling tests matching the pipeline_run_if_changed or not excluded by pipeline_skip_if_only_changed parameters:
/test e2e-parallel
done
<!-- ccr-overview-v2 -->
Copilot review overview
🔵 Needs a closer look
An unresolved numeric-comparison issue can cause recurring no-op writes.
Review effort: Lite
Findings: None
<details>
<summary><strong>Resolved since last review (1)</strong></summary>
</details>
Scheduling tests matching the pipeline_run_if_changed or not excluded by pipeline_skip_if_only_changed parameters:
/test e2e-parallel
/retest
Slack conversation on failing tests: https://redhat-internal.slack.com/archives/C0B48QR1ZED/p1790235142231019
could you confirm if the problem you are solving here is somehow similar to the one in the link? https://redhat.atlassian.net/browse/ARO-27909
If so, I would suggest your decoding implementation be similar to the one in the linked ticket, so we have an unified decoding mechanism throughout the project.
/hold rethinking solution
Float64() is still lossy for valid JSON numbers outside binary64 precision, so this does not preserve full numeric precision. For example, 9223372036854775807 vs 9223372036854775808 and 9007199254740992.1 vs 9007199254740992.2 round to the same float and suppress a required write-back; underflowed distinct values can also collapse to zero, while the exact-text fallback makes equivalent overflowed forms such as 1e1000 and 10e999 compare unequal. Compare json.Number values using an exact representation with a bounded exponent instead of Float64().
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Float64() can collapse distinct valid JSON numbers and suppress required write-backs.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (1)</strong></summary>
</details>
fine for our use case.
good point. Simplifies the implementation and still gives enough precision (integer precision up to 2^63). Also conformity is a valid point! 👍
/unhold
Scheduling tests matching the pipeline_run_if_changed or not excluded by pipeline_skip_if_only_changed parameters:
/test e2e-parallel
why the first validation? It's unnecessary. Consider d.Decode as an optimistic validator. If it decodes the json is valid, otherwise invalid. This code will be executed in many controllers doing this decoding many times, doing this validation twice just add more latency to the overall system. Add an unit test with an invalid json to catch any regression.
Fixed. The comparison no longer relies on Decode alone: after decoding the first value, decodeJSONPreservingNumbers requires d.Token() to return io.EOF, rejecting trailing tokens like {"n":1} garbage. This uses the same decoder buffer (no separate full-input pass). Covered by the trailing garbage after a valid JSON value must trigger an update test.
Resolved by dropping big.Rat entirely. Numbers are now compared as int64 when both are integral (keeps 2^54 precision) and otherwise as float64. A pathological literal like 1e1000000000 overflows Float64() to a parse error and falls back to an exact text comparison, so no enormous value is ever materialised on this path.
Obsolete after the simplification: the exponent guard (and the strconv.Atoi negation) were removed. float64 parsing now handles out-of-range exponents safely (parse error → exact text compare), so the signed-overflow path no longer exists.
Done. Removed the json.Valid pre-pass. Validation now piggybacks on the single decode: Decode the first value, then require d.Token() == io.EOF to reject trailing data — same decoder buffer, no second full parse. Bare Decode alone was insufficient because it silently ignores trailing tokens (e.g. {"n":1} garbage). Added a malformed-JSON unit test ({"n":}) alongside the trailing-garbage one to catch regressions.
<!-- ccr-overview-v2 -->
Copilot review overview
🔵 Needs a closer look
Decimal or exponent representations of integral values can still lose precision and suppress required updates.
Review effort: Lite
Findings: None
<details>
<summary><strong>Resolved since last review (1)</strong></summary>
</details>
Scheduling tests matching the pipeline_run_if_changed or not excluded by pipeline_skip_if_only_changed parameters:
/test e2e-parallel
/lgtm
<!-- ccr-overview-v2 -->
Copilot review overview
🔵 Needs a closer look
Normalize negative zero values before marshaling to avoid false updates.
Review effort: Lite
Findings: None
This round-trip does not canonicalize all numerically equal JSON values: -0.0 is decoded as float64(-0) and marshaled as -0, while 0 is decoded as int64(0) and marshaled as 0. Thus valid payloads such as {"n":-0.0} and {"n":0} still make NeedsUpdate report a change, contrary to the stated format-invariance guarantee; normalize signed zero (and add a regression case) before comparing.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Normalize signed zero comparisons and qualify the integer-precision comment.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (1)</strong></summary>
</details>
This normalization is still not format-invariant for integral values near the int64 limit. For example, {"n":9223372036854775807} is preserved as an int64, while the numerically equal {"n":9223372036854775807.0} is decoded as float64 and re-marshaled as {"n":9223372036854776000}, so NeedsUpdate reports an update. Either preserve/compare the original numeric representation exactly across integer and decimal spellings, or narrow the documented guarantee and add a regression test for the intended behavior.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Moderate issues remain with near-int64 numeric-format invariance and Raw-vs-Object precision coverage.
Review effort: Lite
Findings: 2 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (2)</strong></summary>
</details>
that's acceptable.
that's acceptable.
Scheduling tests matching the pipeline_run_if_changed or not excluded by pipeline_skip_if_only_changed parameters:
/test e2e-parallel
<!-- ccr-overview-v2 -->
Copilot review overview
🟢 Approval recommended
No unresolved review issues were identified.
Review effort: Lite
Findings: None
<details>
<summary><strong>Resolved since last review (2)</strong></summary>
</details>
Scheduling tests matching the pipeline_run_if_changed or not excluded by pipeline_skip_if_only_changed parameters:
/test e2e-parallel
/lgtm
/retest
@tmstff: 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
--- | --- | --- | --- | ---
ci/prow/e2e-parallel | 6e66d4c793650a413acda3f74dda6a03700e788a | link | true | /test e2e-parallel
Full PR test history. Your PR dashboard.
<details>
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.
</details>
<!-- test report -->
/approve
Do we know if this could impact existing data being serialized/deserialized somehow, specially considering we have pre-existing resources?
/hold for Miguels concern
Do we know if this could impact existing data being serialized/deserialized somehow, specially considering we have pre-existing resources?
NeedsUpdate is used in reconciliation loops to check if a resource already has the desired state. The result of serialization / deserialization in NeedsUpate is not persisted. That means that the only impact can be on the decision whether or not a resource is updated.
Due to thorough code review and unit testing it becomes clear, that the only change in behavior is that for really large numbers (> 2^53 and < 2^63) differences between numbers can now be reliably distinguished.
That was not the case before, because these kind of large numbers were converted to float64, resulting in precision loss.
In short: no negative side effect expected.
As a side question, non blocking for the PR, do we know if json v2 has changed the behavior around it?
> Do we know if this could impact existing data being serialized/deserialized somehow, specially considering we have pre-existing resources?
> NeedsUpdate is used in reconciliation loops to check if a resource already has the desired state. The result of serialization / deserialization in NeedsUpate is not persisted. That means that the only impact can be on the decision whether or not a resource is updated.
> Due to thorough code review and unit testing it becomes clear, that the only change in behavior is that for really large numbers (> 2^53 and < 2^63) differences between numbers can now be reliably distinguished.
> That was not the case before, because these kind of large numbers were converted to float64, resulting in precision loss.
> In short: no negative side effect expected.
I was discussing this with @geoberle and we noticed that the Cosmos serialization/deserialization itself also depends on encoding/json currently. This means that we are also getting potentially truncated results when deserializing. I think this would probably need fixing at that layer too. Otherwise when unmarshaling from Cosmos we would get the truncated value.
@geoberle @miguelsorianod yes, you are right! This solution will not help in this case, and might even trigger a hot loop. Putting it on draft for now and might revisit it later. Thanks for your input! :-)
As a side question, non blocking for the PR, do we know if json v2 has changed the behavior around it?
I don't know - but I will check it out!
New changes are detected. LGTM label has been removed.
[APPROVALNOTIFIER] This PR is APPROVED
This pull-request has been approved by: <a href="https://github.com/Azure/ARO-HCP/pull/7071#issuecomment-5892183844" title="LGTM">adalrsjr1</a>, <a href="https://github.com/Azure/ARO-HCP/pull/7071#issuecomment-5927593630" title="Approved">geoberle</a>, <a href="https://github.com/Azure/ARO-HCP/pull/7071#" title="Author self-approved">tmstff</a>
The full list of commands accepted by this bot can be found here.
The pull request process is described here
<details >
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
</details>
<!-- META={"approvers":[]} -->
This import change also affects softDeleteByCosmosID, which unmarshals GenericDocument[map[string]interface{}] and then reads cosmosMetadata.instanceVersion with unstructured.NestedFloat64 at line 463. util/json preserves that JSON integer as int64, so the lookup errors, current remains 1, and every soft-delete of a document with version >1 writes version 2 instead of incrementing the existing value. Handle both integer types (or keep the generic soft-delete decode on the standard JSON path) and add a regression test with an existing version such as 7.
This import changes jsonBytesEqual below from decoding every JSON number as float64 to decoding integral literals as int64, but that function still compares the decoded values with reflect.DeepEqual. Consequently, equivalent payloads such as {"n":3} and {"n":3.0} now compare unequal and EnsureApplyDesire performs a needless replacement; unlike NeedsUpdate, this path does not re-marshal to normalize numeric spellings. Canonicalize the decoded values before comparison (or otherwise retain the previous numeric-format invariance) and add a regression test.
The repository guidance requires regenerating docs/cosmos-data-flow.md for changes to internal/database CRUD/precondition logic and controller/informer implementations (CLAUDE.md:262-268). This PR changes shared CRUD serialization/deserialization behavior, but the generated data-flow documentation is not included; please regenerate and commit it, or document why this path is intentionally excluded.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Unresolved critical and moderate findings remain, along with the required generated documentation update.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture> · 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> · 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (3)</strong></summary>
</details>
PR needs rebase.
<details>
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.
</details>
tmstff
Jira: ARO-29803
What
Route the kube-applier desired-object JSON (un)marshalling through
k8s.io/apimachinery/pkg/util/json(whoseUnmarshaldecodes whole numbers asint64, exact to 2^63) instead ofencoding/json(which decodes numbers intointerface{}asfloat64, losing precision beyond 2^53).Two parts:
1.
NeedsUpdateRawExtension comparison — compareruntime.RawExtensionpayloads by round-tripping both through
util/json(Unmarshal + Marshal) andcomparing the resulting bytes. Exact integer comparison < 2^63; still
format-invariant (
3 == 3.0,1e3 == 1000) and key-order-insensitive, sonumerically-equal payloads don't trigger spurious no-op write-backs. Added
unit coverage for large-integer distinguishability, numeric-format invariance,
malformed/trailing-garbage input.
2. Whole kube-applier (un)marshal surface — for consistency, every
marshal/unmarshal on the kube-applier data path is switched (not only the
strictly-necessary generic decodes):
- backend desire builders/helpers and read-desire comparison (
jsonBytesEqual)- shared Cosmos serialization used by kube-applier:
cosmosstorageutilsCRUDand iterators,
corecosmosstoragetransaction, informer changefeed/snapshot- kube-applier
read_desirecontroller observed-object marshalWhy
controllerutils.NeedsUpdateand the kube-applier desire lifecycle compare andround-trip
runtime.RawExtension/ Cosmos document payloads via JSON. DecodingJSON numbers into
float64collapses integers beyond float64's exact range(2^53) onto the nearest representable value — two distinct large integers compare
equal, masking real changes and suppressing needed write-backs.
util/json.Marshalis a passthrough toencoding/json(no behaviour change); itsUnmarshalis additionally case-sensitive, which is safe here because everydecoded payload is canonical JSON we produced (lowercase Cosmos tags / k8s
objects).
corecosmosstorage/transaction.gokeepsencoding/jsonforjson.RawMessageand aliasesutil/jsonask8sjson.Scope was deliberately limited to the kube-applier data path; the frontend ARM
request/response parsers (
coreapi/coreapihelpers) and cluster-service dispatch(
ocm) were left onencoding/jsonto avoid changing ARM case-insensitivitysemantics.
Testing
All affected modules build, vet and lint clean; unit tests pass under the
repo-pinned toolchain (
GOTOOLCHAIN=go1.25.7).🤖 Generated with Claude Code