Skip to content

Commit

Permalink
feat: use GiB as disk unit (#478)
Browse files Browse the repository at this point in the history
* feat: use GiB as disk unit

* fix: e2e

* fix: unit test
  • Loading branch information
fabio-silva authored Jul 9, 2024
1 parent 01db87a commit 385a439
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ test.describe('DB Cluster creation', () => {
addedCluster?.spec.engine.resources?.cpu.toString()
);
expect(addedCluster?.spec.engine.resources?.memory.toString()).toBe('1G');
expect(addedCluster?.spec.engine.storage.size.toString()).toBe('1G');
expect(addedCluster?.spec.engine.storage.size.toString()).toBe('1Gi');
expect(addedCluster?.spec.proxy.expose.type).toBe('internal');
expect(addedCluster?.spec.proxy.replicas).toBe(3);
// expect(addedCluster?.spec.proxy.expose.ipSourceRanges).toEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export const resourcesStepCheck = async (page: Page) => {
expect(await page.getByText('x 3 nodes').count()).toBe(3);
await expect(page.getByTestId('cpu-resource-sum')).toHaveText('= 1.8 CPU');
await expect(page.getByTestId('memory-resource-sum')).toHaveText('= 3 GB');
await expect(page.getByTestId('disk-resource-sum')).toHaveText(' = 3 GB');
await expect(page.getByTestId('disk-resource-sum')).toHaveText(' = 3 Gi');
};
2 changes: 1 addition & 1 deletion ui/apps/everest/.e2e/utils/db-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const createDbClusterFn = async (
},
storage: {
class: customOptions?.storageClass! || storageClassNames,
size: `${customOptions?.disk || 25}G`,
size: `${customOptions?.disk || 25}Gi`,
},
// TODO return engineParams to tests
// config: dbPayload.engineParametersEnabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const formValuesToPayloadMapping = (
},
storage: {
class: dbPayload.storageClass!,
size: `${dbPayload.disk}G`,
size: `${dbPayload.disk}Gi`,
},
config: dbPayload.engineParametersEnabled
? dbPayload.engineParameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const formValuesToPayloadOverrides = (
storage: {
...dbCluster.spec.engine.storage,
class: dbPayload.storageClass!,
size: `${dbPayload.disk}G`,
size: `${dbPayload.disk}Gi`,
},
config: dbPayload.engineParametersEnabled
? dbPayload.engineParameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ export const ResourcesStep = () => {
label={Messages.labels.disk.toUpperCase()}
helperText={checkResourceText(
resourcesInfo?.available?.diskSize,
'GB',
'Gi',
Messages.labels.disk,
diskCapacityExceeded
)}
endSuffix="GB"
endSuffix="Gi"
numberOfNodes={numberOfNodes}
/>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ export const matchFieldsValueToResourceSize = (
return ResourceSize.custom;
}

const size = memoryParser(dbCluster?.spec?.engine?.storage?.size.toString());
const memory = memoryParser(resources.memory.toString());
const size = memoryParser(
dbCluster?.spec?.engine?.storage?.size.toString(),
'Gi'
);
const memory = memoryParser(resources.memory.toString(), 'G');

const res = Object.values(DEFAULT_SIZES).findIndex(
(item) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ export const DbClusterPayloadToFormValues = (
dbCluster?.spec?.engine?.resources?.cpu.toString() || '0'
),
[DbWizardFormFields.disk]: memoryParser(
dbCluster?.spec?.engine?.storage?.size.toString()
dbCluster?.spec?.engine?.storage?.size.toString(),
'Gi'
),
[DbWizardFormFields.memory]: memoryParser(
(dbCluster?.spec?.engine?.resources?.memory || 0).toString()
(dbCluster?.spec?.engine?.resources?.memory || 0).toString(),
'G'
),
[DbWizardFormFields.storageClass]:
dbCluster?.spec?.engine?.storage?.class || null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('DatabasePreview', () => {

expect(screen.getByText('Nº nodes: 1')).toBeInTheDocument();
expect(screen.getByText('CPU: 1 CPU')).toBeInTheDocument();
expect(screen.getByText('Disk: 30 GB')).toBeInTheDocument();
expect(screen.getByText('Disk: 30 Gi')).toBeInTheDocument();
});

it('should get updated form values', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const ResourcesPreviewSection = ({
}`}
/>
<PreviewContentText
text={`Disk: ${Number.isNaN(parsedDisk) ? '' : `${parsedDisk} GB`}`}
text={`Disk: ${Number.isNaN(parsedDisk) ? '' : `${parsedDisk} Gi`}`}
/>
</>
);
Expand Down
51 changes: 35 additions & 16 deletions ui/apps/everest/src/utils/k8ResourceParser/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
const memoryMultipliers = {
k: 10 ** -6,
M: 10 ** -3,
G: 1,
T: 1000,
P: 1000 ** 2,
E: 1000 ** 3,
Ki: 1024 * 10 ** -9,
Mi: 1024 ** 2 * 10 ** -9,
Gi: 1024 ** 3 * 10 ** -9,
Ti: 1024 ** 4 * 10 ** -9,
Pi: 1024 ** 5 * 10 ** -9,
Ei: 1024 ** 6 * 10 ** -9,
type kubernetesUnit =
| 'k'
| 'M'
| 'G'
| 'T'
| 'P'
| 'E'
| 'Ki'
| 'Mi'
| 'Gi'
| 'Ti'
| 'Pi'
| 'Ei';

const memoryMultipliers: Record<kubernetesUnit, number> = {
k: 10 ** 3,
M: 10 ** 6,
G: 10 ** 9,
T: 10 ** 12,
P: 10 ** 15,
E: 10 ** 18,
Ki: 1024,
Mi: 1024 ** 2,
Gi: 1024 ** 3,
Ti: 1024 ** 4,
Pi: 1024 ** 5,
Ei: 1024 ** 6,
};

export const cpuParser = (input: string) => {
Expand All @@ -24,12 +38,17 @@ export const cpuParser = (input: string) => {
return parseFloat(input);
};

export const memoryParser = (input: string) => {
export const memoryParser = (input: string, targetUnit?: kubernetesUnit) => {
const unitMatch = input.match(/^([0-9]+)([A-Za-z]{1,2})$/);

if (unitMatch) {
if (targetUnit && unitMatch) {
const value = parseInt(unitMatch[1], 10);
// @ts-ignore
return parseInt(unitMatch[1], 10) * memoryMultipliers[unitMatch[2]];
const sourceUnit: kubernetesUnit = unitMatch[2];

const valueBytes = value * memoryMultipliers[sourceUnit];

return valueBytes / memoryMultipliers[targetUnit];
}

return parseInt(input, 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,10 @@ describe('cpu parser', () => {
});

describe('memory parser', () => {
// pattern is [description, input, output]
const tests: [string, string, number][] = [
['parses full numbers', '1', 1],
['parses kilo strings', '1k', 1 * 10 ** -6],
['parses Mega strings', '2M', 2 * 10 ** -3],
['parses Giga strings', '3G', 3],
['parses Tera strings', '4T', 4 * 1000],
['parses Peta strings', '5P', 5 * 1000 ** 2],
['parses Exa strings', '6E', 6 * 1000 ** 3],
['parses kibi strings', '1Ki', 1 * (1024 * 10 ** -9)],
['parses Mebi strings', '2Mi', 2 * (1024 ** 2 * 10 ** -9)],
['parses Gibi strings', '3Gi', 3 * (1024 ** 3 * 10 ** -9)],
['parses Tebi strings', '4Ti', 4 * (1024 ** 4 * 10 ** -9)],
['parses Pebi strings', '5Pi', 5 * (1024 ** 5 * 10 ** -9)],
['parses Exbi strings', '6Ei', 6 * (1024 ** 6 * 10 ** -9)],
];

tests.map((t) =>
it(`${t[0]} (${t[1]} to ${t[2]})`, () => {
expect(memoryParser(t[1])).toEqual(t[2]);
})
);
it('correctly parses memory strings', () => {
expect(memoryParser('1')).toEqual(1);
expect(memoryParser('1k', 'G')).toEqual(1 * 10 ** -6);
expect(memoryParser('1G', 'Gi')).toEqual(10 ** 9 / 1024 ** 3);
expect(memoryParser('1G', 'G')).toEqual(1);
});
});

0 comments on commit 385a439

Please sign in to comment.