From 385a439a30b3f2384161f91a13df21efccbab503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Silva?= Date: Tue, 9 Jul 2024 15:30:52 +0100 Subject: [PATCH] feat: use GiB as disk unit (#478) * feat: use GiB as disk unit * fix: e2e * fix: unit test --- .../create-db-cluster.e2e.ts | 2 +- .../create-db-cluster/steps/resources-step.ts | 2 +- ui/apps/everest/.e2e/utils/db-cluster.ts | 2 +- .../api/db-cluster/useCreateDbCluster.ts | 2 +- .../api/db-cluster/useUpdateDbCluster.ts | 2 +- .../steps/resources/resources-step.tsx | 4 +- .../steps/resources/resources-step.utils.ts | 7 ++- .../database-form/database-form.utils.ts | 6 ++- .../database-preview.test.tsx | 2 +- .../sections/resources-section.tsx | 2 +- .../src/utils/k8ResourceParser/index.ts | 51 +++++++++++++------ .../k8ResourceParser/k8ResourceParser.test.ts | 28 +++------- 12 files changed, 59 insertions(+), 51 deletions(-) diff --git a/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/create-db-cluster.e2e.ts b/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/create-db-cluster.e2e.ts index fd978c307..88a6d4f99 100644 --- a/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/create-db-cluster.e2e.ts +++ b/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/create-db-cluster.e2e.ts @@ -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([ diff --git a/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/steps/resources-step.ts b/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/steps/resources-step.ts index 695c29726..8d373961a 100644 --- a/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/steps/resources-step.ts +++ b/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/steps/resources-step.ts @@ -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'); }; diff --git a/ui/apps/everest/.e2e/utils/db-cluster.ts b/ui/apps/everest/.e2e/utils/db-cluster.ts index 79a79da29..5353dac89 100644 --- a/ui/apps/everest/.e2e/utils/db-cluster.ts +++ b/ui/apps/everest/.e2e/utils/db-cluster.ts @@ -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 diff --git a/ui/apps/everest/src/hooks/api/db-cluster/useCreateDbCluster.ts b/ui/apps/everest/src/hooks/api/db-cluster/useCreateDbCluster.ts index 26627a8f2..ed0000155 100644 --- a/ui/apps/everest/src/hooks/api/db-cluster/useCreateDbCluster.ts +++ b/ui/apps/everest/src/hooks/api/db-cluster/useCreateDbCluster.ts @@ -81,7 +81,7 @@ const formValuesToPayloadMapping = ( }, storage: { class: dbPayload.storageClass!, - size: `${dbPayload.disk}G`, + size: `${dbPayload.disk}Gi`, }, config: dbPayload.engineParametersEnabled ? dbPayload.engineParameters diff --git a/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts b/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts index f7b2d8ec0..fca8de80e 100644 --- a/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts +++ b/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts @@ -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 diff --git a/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.tsx b/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.tsx index 3018830a6..ade6c1108 100644 --- a/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.tsx +++ b/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.tsx @@ -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} /> diff --git a/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.utils.ts b/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.utils.ts index 670ed6dfd..48647d740 100644 --- a/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.utils.ts +++ b/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.utils.ts @@ -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) => diff --git a/ui/apps/everest/src/pages/database-form/database-form.utils.ts b/ui/apps/everest/src/pages/database-form/database-form.utils.ts index 15e0c728e..b71f79ef7 100644 --- a/ui/apps/everest/src/pages/database-form/database-form.utils.ts +++ b/ui/apps/everest/src/pages/database-form/database-form.utils.ts @@ -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, diff --git a/ui/apps/everest/src/pages/database-form/database-preview/database-preview.test.tsx b/ui/apps/everest/src/pages/database-form/database-preview/database-preview.test.tsx index 470dda063..c21cf7560 100644 --- a/ui/apps/everest/src/pages/database-form/database-preview/database-preview.test.tsx +++ b/ui/apps/everest/src/pages/database-form/database-preview/database-preview.test.tsx @@ -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 () => { diff --git a/ui/apps/everest/src/pages/database-form/database-preview/sections/resources-section.tsx b/ui/apps/everest/src/pages/database-form/database-preview/sections/resources-section.tsx index 3de516744..54cdda8d6 100644 --- a/ui/apps/everest/src/pages/database-form/database-preview/sections/resources-section.tsx +++ b/ui/apps/everest/src/pages/database-form/database-preview/sections/resources-section.tsx @@ -23,7 +23,7 @@ export const ResourcesPreviewSection = ({ }`} /> ); diff --git a/ui/apps/everest/src/utils/k8ResourceParser/index.ts b/ui/apps/everest/src/utils/k8ResourceParser/index.ts index 8c905af48..7c3dc50f5 100644 --- a/ui/apps/everest/src/utils/k8ResourceParser/index.ts +++ b/ui/apps/everest/src/utils/k8ResourceParser/index.ts @@ -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 = { + 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) => { @@ -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); diff --git a/ui/apps/everest/src/utils/k8ResourceParser/k8ResourceParser.test.ts b/ui/apps/everest/src/utils/k8ResourceParser/k8ResourceParser.test.ts index 2adbe7f00..f8cc9f39c 100644 --- a/ui/apps/everest/src/utils/k8ResourceParser/k8ResourceParser.test.ts +++ b/ui/apps/everest/src/utils/k8ResourceParser/k8ResourceParser.test.ts @@ -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); + }); });