mirror of https://github.com/portainer/portainer
fix(kubernetes): Namespace resource limits and requests display consistent value (#1055)
parent
965ef5246b
commit
541f281b29
|
@ -4,7 +4,11 @@ import { StorageClass } from '@/react/portainer/environments/types';
|
||||||
import { Registry } from '@/react/portainer/registries/types/registry';
|
import { Registry } from '@/react/portainer/registries/types/registry';
|
||||||
|
|
||||||
import { NamespaceFormValues, PortainerNamespace } from '../types';
|
import { NamespaceFormValues, PortainerNamespace } from '../types';
|
||||||
import { megaBytesValue, parseCPU } from '../resourceQuotaUtils';
|
import {
|
||||||
|
megaBytesValue,
|
||||||
|
parseCPU,
|
||||||
|
convertBase2ToMiB,
|
||||||
|
} from '../resourceQuotaUtils';
|
||||||
import { IngressControllerClassMap } from '../../cluster/ingressClass/types';
|
import { IngressControllerClassMap } from '../../cluster/ingressClass/types';
|
||||||
|
|
||||||
interface ComputeInitialValuesParams {
|
interface ComputeInitialValuesParams {
|
||||||
|
@ -41,7 +45,7 @@ export function computeInitialValues({
|
||||||
ingressClasses: ingressClasses ?? [],
|
ingressClasses: ingressClasses ?? [],
|
||||||
resourceQuota: {
|
resourceQuota: {
|
||||||
enabled: !!memory || !!cpu,
|
enabled: !!memory || !!cpu,
|
||||||
memory: `${megaBytesValue(memory)}`,
|
memory: `${megaBytesValue(convertBase2ToMiB(memory))}`,
|
||||||
cpu: `${parseCPU(cpu)}`,
|
cpu: `${parseCPU(cpu)}`,
|
||||||
},
|
},
|
||||||
registries: registriesUsed ?? [],
|
registries: registriesUsed ?? [],
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { parseCPU } from './resourceQuotaUtils';
|
import { parseCPU, convertBase2ToMiB } from './resourceQuotaUtils';
|
||||||
|
|
||||||
// test parseCPU with '', '2', '100m', '100u'
|
// test parseCPU with '', '2', '100m', '100u'
|
||||||
describe('parseCPU', () => {
|
describe('parseCPU', () => {
|
||||||
|
@ -15,3 +15,19 @@ describe('parseCPU', () => {
|
||||||
expect(parseCPU('100u')).toBe(0.0001);
|
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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -51,6 +51,40 @@ export function bytesValue(mem: string | number) {
|
||||||
return safeFilesizeParser(mem, 10) * 1000 * 1000;
|
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
|
* The default base is 2, you can use base 10 if you want
|
||||||
* https://github.com/patrickkettner/filesize-parser#readme
|
* https://github.com/patrickkettner/filesize-parser#readme
|
||||||
|
|
Loading…
Reference in New Issue