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:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-kubernetes-react': patch
|
||||
---
|
||||
|
||||
Handle mixed decimals and bigint when calculating k8s resource usage
|
||||
@@ -25,6 +25,7 @@ backend's
|
||||
backported
|
||||
backporting
|
||||
BEPs
|
||||
bigint
|
||||
Bigtable
|
||||
Billett
|
||||
bitbucket
|
||||
@@ -453,4 +454,4 @@ zsh
|
||||
Pulumi
|
||||
Lightsail
|
||||
PR
|
||||
rebasing
|
||||
rebasing
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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}%`;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user