Skip to content

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

Merged
kubernetes-prow[bot] merged 1 commit into
kubernetes-client:mainfrom
eisen0419:fix/quantity-to-scalar-fractional-suffix
Nov 17, 2026
Merged

kubernetes-prow[bot] merged 1 commit into
kubernetes-client:mainfrom
eisen0419:fix/quantity-to-scalar-fractional-suffix

Conversation

@eisen0419

Copy link
Copy Markdown
Contributor

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 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
@brendandburns

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 17, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, eisen0419

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

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 17, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 20ac4c0 into kubernetes-client:main Nov 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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

2 participants