Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat: use GiB as disk unit" #547

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('1Gi');
expect(addedCluster?.spec.engine.storage.size.toString()).toBe('1G');
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 Gi');
await expect(page.getByTestId('disk-resource-sum')).toHaveText(' = 3 GB');
};
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}Gi`,
size: `${customOptions?.disk || 25}G`,
},
// 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}Gi`,
size: `${dbPayload.disk}G`,
},
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}Gi`,
size: `${dbPayload.disk}G`,
},
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,
'Gi',
'GB',
Messages.labels.disk,
diskCapacityExceeded
)}
endSuffix="Gi"
endSuffix="GB"
numberOfNodes={numberOfNodes}
/>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ export const matchFieldsValueToResourceSize = (
return ResourceSize.custom;
}

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

const res = Object.values(DEFAULT_SIZES).findIndex(
(item) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ export const DbClusterPayloadToFormValues = (
dbCluster?.spec?.engine?.resources?.cpu.toString() || '0'
),
[DbWizardFormFields.disk]: memoryParser(
dbCluster?.spec?.engine?.storage?.size.toString(),
'Gi'
dbCluster?.spec?.engine?.storage?.size.toString()
),
[DbWizardFormFields.memory]: memoryParser(
(dbCluster?.spec?.engine?.resources?.memory || 0).toString(),
'G'
(dbCluster?.spec?.engine?.resources?.memory || 0).toString()
),
[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 Gi')).toBeInTheDocument();
expect(screen.getByText('Disk: 30 GB')).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} Gi`}`}
text={`Disk: ${Number.isNaN(parsedDisk) ? '' : `${parsedDisk} GB`}`}
/>
</>
);
Expand Down
51 changes: 16 additions & 35 deletions ui/apps/everest/src/utils/k8ResourceParser/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
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,
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,
};

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

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

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

const valueBytes = value * memoryMultipliers[sourceUnit];

return valueBytes / memoryMultipliers[targetUnit];
return parseInt(unitMatch[1], 10) * memoryMultipliers[unitMatch[2]];
}

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

describe('memory parser', () => {
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);
});
// 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]);
})
);
});
Loading