Skip to content

fix(util): parse fractional SI quantities in quantityToScalar - #3153

Open
eisen0419 wants to merge 1 commit into
kubernetes-client:mainfrom
eisen0419:fix/quantity-to-scalar-fractional-suffix
Open

eisen0419 wants to merge 1 commit into
kubernetes-client:mainfrom
eisen0419:fix/quantity-to-scalar-fractional-suffix

Conversation

@eisen0419

Copy link
Copy Markdown

Fixes #3152

Summary

quantityToScalar passed mantissas for k..E and Ki..Ei directly to BigInt(...), throwing SyntaxError: Cannot convert <x> to a BigInt on fractional quantities such as 1.5Gi, 0.5Mi, 0.1k, or 2.5G. Parse decimal mantissas with exact BigInt scaling and round non-zero remainders away from zero to match Kubernetes resource.Quantity.Value().

  quantityToScalar(quantity)
    suffix in ['', 'n', 'u', 'm'] -> Number scaling (unchanged, rejects NaN)
    suffix in ['k'..'E', 'Ki'..'Ei']
-     BigInt(mantissa) * multiplier
+     parseDecimal(mantissa) -> significand * multiplier * 10^(exp - fracDigits)
+     round non-zero remainder away from zero (matches Quantity.Value() / inf.RoundUp)

Evidence

  • Before:
    quantityToScalar('1.5Gi') -> SyntaxError: Cannot convert 1.5 to a BigInt
    totalMemory(podWith1_5Gi) -> SyntaxError: Cannot convert 1.5 to a BigInt
    
  • After:
    quantityToScalar('1.5Gi') -> 1610612736n
    quantityToScalar('0.5Mi') -> 524288n
    quantityToScalar('1.5Ki') -> 1536n
    quantityToScalar('0.1k')  -> 100n
    quantityToScalar('2.5G')  -> 2500000000n
    npm test && npm run lint && npm run build -> 429 passed, 0 failed
    

Merge Danger

Door: two-way

Self-contained parser update in src/util.ts (src/gen/ untouched); integer SI quantities and n/u/m/bare number return types are unchanged.

Blast Radius: narrow

Scoped to quantityToScalar and its resource helper callers (totalMemory, totalCPU, topPods, topNodes).

quantityToScalar passed mantissas such as 1.5Gi straight to BigInt, which rejects fractions. Decimal scaling now matches Quantity.Value() and leaves integer results unchanged.
@kubernetes-prow
kubernetes-prow Bot requested a review from cjihrig November 14, 2026 12:40
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: eisen0419
Once this PR has been reviewed and has the lgtm label, please assign davidgamero for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

@kubernetes-prow kubernetes-prow Bot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Nov 14, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @eisen0419!

It looks like this is your first PR to kubernetes-client/javascript 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/javascript has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 14, 2026

This branch has not been deployed

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

quantityToScalar throws "Cannot convert 1.5 to a BigInt" for fractional quantities like 1.5Gi, 0.5Mi, 2.5G

1 participant