fix(kubernetes): Namespace resource limits and requests display consistent value (#1055)

pull/12845/head
Cara Ryan 2025-09-01 10:25:53 +12:00 committed by GitHub
parent 965ef5246b
commit 541f281b29
3 changed files with 57 additions and 3 deletions

View File

@ -4,7 +4,11 @@ import { StorageClass } from '@/react/portainer/environments/types';
import { Registry } from '@/react/portainer/registries/types/registry';
import { NamespaceFormValues, PortainerNamespace } from '../types';
import { megaBytesValue, parseCPU } from '../resourceQuotaUtils';
import {
megaBytesValue,
parseCPU,
convertBase2ToMiB,
} from '../resourceQuotaUtils';
import { IngressControllerClassMap } from '../../cluster/ingressClass/types';
interface ComputeInitialValuesParams {
@ -41,7 +45,7 @@ export function computeInitialValues({
ingressClasses: ingressClasses ?? [],
resourceQuota: {
enabled: !!memory || !!cpu,
memory: `${megaBytesValue(memory)}`,
memory: `${megaBytesValue(convertBase2ToMiB(memory))}`,
cpu: `${parseCPU(cpu)}`,
},
registries: registriesUsed ?? [],

View File

@ -1,4 +1,4 @@
import { parseCPU } from './resourceQuotaUtils';
import { parseCPU, convertBase2ToMiB } from './resourceQuotaUtils';
// test parseCPU with '', '2', '100m', '100u'
describe('parseCPU', () => {
@ -15,3 +15,19 @@ describe('parseCPU', () => {
expect(parseCPU('100u')).toBe(0.0001);
});
});
// test convertBase2ToMiB
describe('convertBase2ToMiB', () => {
it('should return empty string for empty string', () => {
expect(convertBase2ToMiB('')).toBe('');
});
it('should return 2Mi for 2Mi', () => {
expect(convertBase2ToMiB('2Mi')).toBe('2Mi');
});
it('should return 1024Mi for 1Gi', () => {
expect(convertBase2ToMiB('1Gi')).toBe('1024Mi');
});
it('should return 1024Mi for 1Ti', () => {
expect(convertBase2ToMiB('1Ti')).toBe('1048576Mi');
});
});

View File

@ -51,6 +51,40 @@ export function bytesValue(mem: string | number) {
return safeFilesizeParser(mem, 10) * 1000 * 1000;
}
/**
* Coverts Ki, Gi, Ti, Pi, Ei suffix values to Mi string
* Used for kubernetes memory conversions currently
*/
export function convertBase2ToMiB(value: string | number) {
if (typeof value === 'number') {
return value;
}
// Extract the numeric part and suffix
const match = value.match(/^(\d+(?:\.\d+)?)([A-Za-z]*)$/);
if (!match) {
return value;
}
const numericValue = parseFloat(match[1]);
const suffix = match[2];
switch (suffix) {
case 'Mi':
return `${numericValue}Mi`;
case 'Gi':
return `${numericValue * 1024}Mi`;
case 'Ti':
return `${numericValue * 1024 * 1024}Mi`;
case 'Pi':
return `${numericValue * 1024 * 1024 * 1024}Mi`;
case 'Ei':
return `${numericValue * 1024 * 1024 * 1024 * 1024}Mi`;
default:
return value;
}
}
/**
* The default base is 2, you can use base 10 if you want
* https://github.com/patrickkettner/filesize-parser#readme