Handle decimals in k8s resource usage calculation (#20514)

When one of the numerator/denominator is a number and other is bigint this was failing.
decimals cannot be converted to bigints

Signed-off-by: Tomasz Szuba <tszuba@box.com>
This commit is contained in:
Tomasz Szuba
2023-10-11 16:11:33 +02:00
committed by GitHub
parent f4dbbfb322
commit 4262e12921
4 changed files with 25 additions and 15 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-kubernetes-react': patch
---
Handle mixed decimals and bigint when calculating k8s resource usage
+2 -1
View File
@@ -25,6 +25,7 @@ backend's
backported
backporting
BEPs
bigint
Bigtable
Billett
bitbucket
@@ -453,4 +454,4 @@ zsh
Pulumi
Lightsail
PR
rebasing
rebasing
+12 -12
View File
@@ -24,18 +24,18 @@ import { SubvalueCell } from '@backstage/core-components';
describe('pod', () => {
describe('currentToDeclaredResourceToPerc', () => {
it('10%', () => {
const tests: (number | string)[][] = [
[10, 100],
[10, '100'],
['10', 100],
['10', '100'],
];
tests.forEach(([a, b]) => {
const result = currentToDeclaredResourceToPerc(a, b);
expect(result).toBe('10%');
});
});
it.each([
[10, 100],
[10, '100'],
['10', 100],
['10', '100'],
['10', 100.0],
[10.0, '100'],
[10.1, '100'],
['10', 100.1],
])('%p out of %p gives 10%%', (current, resource) =>
expect(currentToDeclaredResourceToPerc(current, resource)).toBe('10%'),
);
});
describe('podStatusToCpuUtil', () => {
it('does use correct units', () => {
+6 -2
View File
@@ -126,8 +126,12 @@ export const currentToDeclaredResourceToPerc = (
return `${Math.round((current / resource) * 100)}%`;
}
const numerator: bigint = BigInt(current);
const denominator: bigint = BigInt(resource);
const numerator: bigint = BigInt(
typeof current === 'number' ? Math.round(current) : current,
);
const denominator: bigint = BigInt(
typeof resource === 'number' ? Math.round(resource) : resource,
);
return `${(numerator * BigInt(100)) / denominator}%`;
};