Skip to content

frontend : lib : fix decimal CPU quantity formatting in normalizeUnit#6444

Open
AarishMansur wants to merge 2 commits into
kubernetes-sigs:mainfrom
AarishMansur:fix/decimal-formatting
Open

frontend : lib : fix decimal CPU quantity formatting in normalizeUnit#6444
AarishMansur wants to merge 2 commits into
kubernetes-sigs:mainfrom
AarishMansur:fix/decimal-formatting

Conversation

@AarishMansur

@AarishMansur AarishMansur commented Jul 9, 2026

Copy link
Copy Markdown

Summary

This PR fixes a formatting bug in normalizeUnit where CPU quantities passed as decimal strings (e.g. "2.0") were displayed as "2.0 cores" instead of "2 cores", the current change trims trailing decimal zeros via regex replacement

Related Issue

Fixes #6443

Changes

  • Fixed fallback branch in normalizeUnit (frontend/src/lib/util.ts) to wrap quantity in Number(), normalizing decimal strings like "2.0" to whole numbers before appending the unit label.

Steps to Test

  1. Call normalizeUnit('cpu', '2.0') in a local test script or unit test.
  2. Observe the return value is now "2 cores" instead of "2.0 cores".
  3. Verify existing cases are unaffected: "1000m""1 core", "1""1 core", "2000m""2 cores".

Screenshots (if applicable)

N/A this is a pure logic fix with no UI changes.

Notes for the Reviewer

  • The change is a single line fix in the CPU case of normalizeUnit.
  • "2000" (no suffix) is not a valid Kubernetes CPU input, so no handling was added for that case.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 9, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: AarishMansur / name: aarish mansur (bb2d572)

@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @AarishMansur!

It looks like this is your first PR to kubernetes-sigs/headlamp 🎉. 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-sigs/headlamp 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

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: AarishMansur
Once this PR has been reviewed and has the lgtm label, please assign sniok 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 size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Jul 9, 2026
@kubernetes-prow kubernetes-prow Bot requested review from illume and sniok July 9, 2026 06:35
@kubernetes-prow kubernetes-prow Bot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jul 9, 2026
@illume illume requested a review from Copilot July 10, 2026 05:14

@illume illume left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for working on this.

The commit messages could use some tidying up to match our contribution guidelines. We use Linux kernel style — the contributing guide has the details, and git log shows good examples.

Commits that need attention
  • frontend : lib : fix decimal CPU quantity formatting in normalizeUnit — Missing area: description prefix — e.g. frontend: HomeButton: Fix so it navigates to home or backend: config: Add enable-dynamic-clusters flag.
Commit guidelines
  • Use atomic commits focused on a single change.
  • Use the title format <area>: <Description of changes> — description must start with a capital letter.
  • Keep the title under 72 characters (soft requirement).
  • Explain the intention and why the change is needed.
  • Make commit titles meaningful and describe what changed.
  • Do not add code that a later commit rewrites; squash or reorder commits instead.
  • Do not include Fixes #NN in commit messages.

Good examples:

  • frontend: HomeButton: Fix so it navigates to home
  • backend: config: Add enable-dynamic-clusters flag

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to fix CPU quantity display in normalizeUnit so decimal core strings like "2.0" render as "2 cores" rather than "2.0 cores".

Changes:

  • Updates the CPU fallback formatting path in normalizeUnit to coerce decimal strings via Number(...) before stringification.

Note: CI/check status and PR commit history are not available in the provided review context—please confirm CI is green and the PR history is linear (no merge commits in the PR range).

Comment thread frontend/src/lib/util.ts Outdated
Comment thread frontend/src/lib/util.ts Outdated
@kubernetes-prow kubernetes-prow Bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jul 10, 2026
@AarishMansur AarishMansur force-pushed the fix/decimal-formatting branch from 1072701 to d923b23 Compare July 10, 2026 12:59
@AarishMansur

Copy link
Copy Markdown
Author

@illume I've updated the commit messages to follow the Linux kernel style and addressed the Copilot suggestions as well. Could you please take another look when you have some time? Thanks!

@illume illume requested a review from Copilot July 10, 2026 14:21

@illume illume left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the contribution.

it looks like there's a merge-main commit in this PR — could you rebase onto main instead?

Why this matters

Merge commits from main make the PR history harder to review. Please rebase your branch on top of the latest main instead, then update the PR with the rebased commits.

Could you take a look at the commit messages in this PR? We follow a Linux kernel style for git commits — see the contributing guide and git log for examples.

Commits that need attention
  • frontend : lib : fix decimal CPU quantity formatting in normalizeUnit — Missing area: description prefix — e.g. frontend: HomeButton: Fix so it navigates to home or backend: config: Add enable-dynamic-clusters flag.
  • frontend : lib : Added test to cover Normalize unit — Missing area: description prefix — e.g. frontend: HomeButton: Fix so it navigates to home or backend: config: Add enable-dynamic-clusters flag.
Commit guidelines
  • Use atomic commits focused on a single change.
  • Use the title format <area>: <Description of changes> — description must start with a capital letter.
  • Keep the title under 72 characters (soft requirement).
  • Explain the intention and why the change is needed.
  • Make commit titles meaningful and describe what changed.
  • Do not add code that a later commit rewrites; squash or reorder commits instead.
  • Do not include Fixes #NN in commit messages.

Good examples:

  • frontend: HomeButton: Fix so it navigates to home
  • backend: config: Add enable-dynamic-clusters flag

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread frontend/src/lib/util.ts
@AarishMansur AarishMansur force-pushed the fix/decimal-formatting branch from 271ec43 to c33832d Compare July 10, 2026 14:58
@AarishMansur

Copy link
Copy Markdown
Author

@illume Done I've rebased the branch and updated the commit messages to follow the project's commit message style.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread frontend/src/lib/util.ts
Comment on lines 527 to 531
case 'cpu':
normalizedQuantity = quantity?.endsWith('m')
? `${Number(quantity.substring(0, quantity.length - 1)) / 1000}`
: `${quantity}`;
: `${(quantity ?? '').replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '')}`;
if (normalizedQuantity === '1') {
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/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

util: normalizeUnit formats decimal CPU quantities incorrectly ("2.0 cores" instead of "2 cores")

3 participants