From 4b40fda1d4d72b9311bc58fe3ed0729fd309efb3 Mon Sep 17 00:00:00 2001 From: Fabio Silva Date: Thu, 31 Oct 2024 15:14:30 +0000 Subject: [PATCH 1/2] fix: wrong proxy expose type for psmdb --- .../api/db-cluster/useUpdateDbCluster.ts | 66 ++++++++----------- .../everest/src/hooks/api/db-cluster/utils.ts | 40 ++++++----- .../database-form/database-form.utils.ts | 42 +++++++----- .../pages/databases/DbClusterView.utils.ts | 12 +++- .../databases/expandedRow/ExpandedRow.tsx | 31 +++++---- .../cards/resources-details.tsx | 61 +++++++++-------- .../src/shared-types/dbCluster.types.ts | 12 ++-- ui/apps/everest/src/utils/db.tsx | 10 +-- 8 files changed, 146 insertions(+), 128 deletions(-) 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 885d0bdde..37296f75c 100644 --- a/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts +++ b/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts @@ -25,6 +25,7 @@ import { import { getProxySpec } from './utils'; import { DbType } from '@percona/types'; import { DbEngineType } from 'shared-types/dbEngines.types'; +import { dbEngineToDbType } from '@percona/utils'; type UpdateDbClusterArgType = { dbPayload: DbWizardType; @@ -99,35 +100,19 @@ const formValuesToPayloadOverrides = ( monitoringConfigName: dbPayload?.monitoringInstance!, }), }, - proxy: - dbPayload.dbType === DbType.Mongo && !dbPayload.sharding - ? {} - : { - ...dbCluster.spec.proxy, - ...getProxySpec( - dbPayload.dbType, - dbPayload.numberOfProxies, - dbPayload.customNrOfProxies || '', - dbPayload.externalAccess, - dbPayload.proxyCpu, - dbPayload.proxyMemory, - dbPayload.sharding, - dbPayload.sourceRanges || [] - ), - // replicas: numberOfNodes, - // expose: { - // ...dbCluster.spec.proxy.expose, - // type: dbPayload.externalAccess - // ? ProxyExposeType.external - // : ProxyExposeType.internal, - // ...(!!dbPayload.externalAccess && - // dbPayload.sourceRanges && { - // ipSourceRanges: dbPayload.sourceRanges.flatMap((source) => - // source.sourceRange ? [source.sourceRange] : [] - // ), - // }), - // }, - }, + proxy: { + ...dbCluster.spec.proxy, + ...getProxySpec( + dbPayload.dbType, + dbPayload.numberOfProxies, + dbPayload.customNrOfProxies || '', + dbPayload.externalAccess, + dbPayload.proxyCpu, + dbPayload.proxyMemory, + dbPayload.sharding, + dbPayload.sourceRanges || [] + ), + }, ...(dbPayload.dbType === DbType.Mongo && { sharding: { enabled: dbPayload.sharding, @@ -278,17 +263,18 @@ export const useUpdateDbClusterResources = () => size: `${newResources.disk}${newResources.diskUnit}`, }, }, - proxy: - dbCluster.spec.engine.type === DbEngineType.PSMDB && !sharding - ? {} - : ({ - ...dbCluster.spec.proxy, - replicas: newResources.numberOfProxies, - resources: { - cpu: `${newResources.proxyCpu}`, - memory: `${newResources.proxyMemory}G`, - }, - } as Proxy), + proxy: getProxySpec( + dbEngineToDbType(dbCluster.spec.engine.type), + newResources.numberOfProxies.toString(), + '', + (dbCluster.spec.proxy as Proxy).expose.type === 'external', + newResources.proxyCpu, + newResources.proxyMemory, + !!sharding, + ((dbCluster.spec.proxy as Proxy).expose.ipSourceRanges || []).map( + (sourceRange) => ({ sourceRange }) + ) + ), ...(dbCluster.spec.engine.type === DbEngineType.PSMDB && sharding && { sharding: { diff --git a/ui/apps/everest/src/hooks/api/db-cluster/utils.ts b/ui/apps/everest/src/hooks/api/db-cluster/utils.ts index 3b8a26647..90d48ad76 100644 --- a/ui/apps/everest/src/hooks/api/db-cluster/utils.ts +++ b/ui/apps/everest/src/hooks/api/db-cluster/utils.ts @@ -1,8 +1,25 @@ import { DbType } from '@percona/types'; import { CUSTOM_NR_UNITS_INPUT_VALUE } from 'components/cluster-form'; -import { Proxy, ProxyExposeType } from 'shared-types/dbCluster.types'; +import { + Proxy, + ProxyExposeConfig, + ProxyExposeType, +} from 'shared-types/dbCluster.types'; import { dbTypeToProxyType } from 'utils/db'; +const getExposteConfig = ( + externalAccess: boolean, + sourceRanges?: Array<{ sourceRange?: string }> +): ProxyExposeConfig => ({ + type: externalAccess ? ProxyExposeType.external : ProxyExposeType.internal, + ...(!!externalAccess && + sourceRanges && { + ipSourceRanges: sourceRanges.flatMap((source) => + source.sourceRange ? [source.sourceRange] : [] + ), + }), +}); + export const getProxySpec = ( dbType: DbType, numberOfProxies: string, @@ -12,12 +29,11 @@ export const getProxySpec = ( memory: number, sharding: boolean, sourceRanges?: Array<{ sourceRange?: string }> -): Proxy | Record => { - console.log('dbType', dbType); - console.log('sharding', sharding); +): Proxy | ProxyExposeConfig => { if (dbType === DbType.Mongo && !sharding) { - console.log('returning empty object'); - return {}; + return { + expose: getExposteConfig(externalAccess, sourceRanges), + } as unknown as ProxyExposeConfig; } const proxyNr = parseInt( numberOfProxies === CUSTOM_NR_UNITS_INPUT_VALUE @@ -35,16 +51,6 @@ export const getProxySpec = ( cpu: `${cpu}`, memory: `${memory}G`, }, - expose: { - type: externalAccess - ? ProxyExposeType.external - : ProxyExposeType.internal, - ...(!!externalAccess && - sourceRanges && { - ipSourceRanges: sourceRanges.flatMap((source) => - source.sourceRange ? [source.sourceRange] : [] - ), - }), - }, + expose: getExposteConfig(externalAccess, sourceRanges), }; }; 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 0138cdc6d..165a9891d 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 @@ -29,7 +29,9 @@ import { getDefaultNumberOfconfigServersByNumberOfNodes, matchFieldsValueToResourceSize, NODES_DB_TYPE_MAP, + ResourceSize, } from 'components/cluster-form'; +import { isProxy } from 'utils/db.tsx'; const replicasToNodes = (replicas: string, dbType: DbType): string => { const nodeOptions = NODES_DB_TYPE_MAP[dbType]; @@ -49,7 +51,9 @@ export const DbClusterPayloadToFormValues = ( ): DbWizardType => { const backup = dbCluster?.spec?.backup; const replicas = dbCluster?.spec?.engine?.replicas.toString(); - const proxies = (dbCluster?.spec?.proxy?.replicas || 0).toString(); + const proxies = ( + isProxy(dbCluster?.spec?.proxy) ? dbCluster?.spec?.proxy?.replicas || 0 : 0 + ).toString(); const diskValues = memoryParser( dbCluster?.spec?.engine?.storage?.size.toString() ); @@ -59,6 +63,9 @@ export const DbClusterPayloadToFormValues = ( replicas, dbEngineToDbType(dbCluster?.spec?.engine?.type) ); + const sourceRangesSource = isProxy(dbCluster?.spec?.proxy) + ? dbCluster?.spec?.proxy?.expose.ipSourceRanges + : dbCluster?.spec?.proxy.ipSourceRanges; return { //basic info @@ -75,17 +82,15 @@ export const DbClusterPayloadToFormValues = ( ) : dbCluster?.metadata?.name, [DbWizardFormFields.dbVersion]: dbCluster?.spec?.engine?.version || '', - [DbWizardFormFields.externalAccess]: - dbCluster?.spec?.proxy?.expose?.type === ProxyExposeType.external, + [DbWizardFormFields.externalAccess]: isProxy(dbCluster?.spec?.proxy) + ? dbCluster?.spec?.proxy?.expose?.type === ProxyExposeType.external + : dbCluster?.spec?.proxy.type === ProxyExposeType.external, // [DbWizardFormFields.internetFacing]: true, [DbWizardFormFields.engineParametersEnabled]: !!dbCluster?.spec?.engine?.config, [DbWizardFormFields.engineParameters]: dbCluster?.spec?.engine?.config, - [DbWizardFormFields.sourceRanges]: dbCluster?.spec?.proxy?.expose - ?.ipSourceRanges - ? dbCluster?.spec?.proxy?.expose?.ipSourceRanges.map((item) => ({ - sourceRange: item, - })) + [DbWizardFormFields.sourceRanges]: sourceRangesSource + ? sourceRangesSource.map((sourceRange) => ({ sourceRange })) : [{ sourceRange: '' }], [DbWizardFormFields.monitoring]: !!dbCluster?.spec?.monitoring?.monitoringConfigName, @@ -102,10 +107,12 @@ export const DbClusterPayloadToFormValues = ( dbEngineToDbType(dbCluster?.spec?.engine?.type), dbCluster?.spec?.engine?.resources ), - [DbWizardFormFields.resourceSizePerProxy]: matchFieldsValueToResourceSize( - dbEngineToDbType(dbCluster?.spec?.engine?.type), - dbCluster?.spec?.proxy.resources - ), + [DbWizardFormFields.resourceSizePerProxy]: isProxy(dbCluster?.spec?.proxy) + ? matchFieldsValueToResourceSize( + dbEngineToDbType(dbCluster?.spec?.engine?.type), + dbCluster?.spec?.proxy.resources + ) + : ResourceSize.small, [DbWizardFormFields.sharding]: dbCluster?.spec?.sharding?.enabled || false, [DbWizardFormFields.shardConfigServers]: ( sharding?.configServer?.replicas || @@ -118,9 +125,9 @@ export const DbClusterPayloadToFormValues = ( [DbWizardFormFields.cpu]: cpuParser( dbCluster?.spec?.engine?.resources?.cpu.toString() || '0' ), - [DbWizardFormFields.proxyCpu]: cpuParser( - dbCluster?.spec?.proxy?.resources?.cpu.toString() || '0' - ), + [DbWizardFormFields.proxyCpu]: isProxy(dbCluster?.spec?.proxy) + ? cpuParser(dbCluster?.spec?.proxy?.resources?.cpu.toString() || '0') + : 0, [DbWizardFormFields.disk]: diskValues.value, [DbWizardFormFields.diskUnit]: diskValues.originalUnit, [DbWizardFormFields.memory]: memoryParser( @@ -128,7 +135,10 @@ export const DbClusterPayloadToFormValues = ( 'G' ).value, [DbWizardFormFields.proxyMemory]: memoryParser( - (dbCluster?.spec?.proxy?.resources?.memory || 0).toString(), + (isProxy(dbCluster?.spec?.proxy) + ? dbCluster?.spec?.proxy?.resources?.memory || 0 + : 0 + ).toString(), 'G' ).value, [DbWizardFormFields.storageClass]: diff --git a/ui/apps/everest/src/pages/databases/DbClusterView.utils.ts b/ui/apps/everest/src/pages/databases/DbClusterView.utils.ts index d56d4cf69..fd30816ac 100644 --- a/ui/apps/everest/src/pages/databases/DbClusterView.utils.ts +++ b/ui/apps/everest/src/pages/databases/DbClusterView.utils.ts @@ -55,9 +55,15 @@ export const convertDbClusterPayloadToTableFormat = ( memory: cluster.spec.engine.resources?.memory || '', storage: cluster.spec.engine.storage.size, nodes: cluster.spec.engine.replicas, - proxies: cluster.spec.proxy.replicas || 0, - proxyCpu: cluster.spec.proxy.resources?.cpu || '', - proxyMemory: cluster.spec.proxy.resources?.memory || '', + proxies: isProxy(cluster.spec.proxy) + ? cluster.spec.proxy.replicas || 0 + : 0, + proxyCpu: isProxy(cluster.spec.proxy) + ? cluster.spec.proxy.resources?.cpu || '' + : '', + proxyMemory: isProxy(cluster.spec.proxy) + ? cluster.spec.proxy.resources?.memory || '' + : '', hostName: cluster.status ? cluster.status.hostname : '', exposetype: isProxy(cluster.spec.proxy) ? cluster.spec.proxy.expose.type diff --git a/ui/apps/everest/src/pages/databases/expandedRow/ExpandedRow.tsx b/ui/apps/everest/src/pages/databases/expandedRow/ExpandedRow.tsx index 82ef8644a..c0ee20030 100644 --- a/ui/apps/everest/src/pages/databases/expandedRow/ExpandedRow.tsx +++ b/ui/apps/everest/src/pages/databases/expandedRow/ExpandedRow.tsx @@ -52,6 +52,7 @@ export const ExpandedRow = ({ port, raw, } = row.original; + console.log('row.original', row.original); const parsedDiskValues = memoryParser(storage.toString()); const parsedMemoryValues = memoryParser(memory.toString()); const parsedProxyMemoryValues = memoryParser(proxyMemory.toString()); @@ -169,19 +170,23 @@ export const ExpandedRow = ({ value={storageResourcesStr} /> - - - - + {proxies > 0 && ( + <> + + + + + + )} - 1 ? 'plural' : 'singular']}`} - loading={loading} - > - - - + {numberOfProxiesInt > 0 && ( + 1 ? 'plural' : 'singular']}`} + loading={loading} + > + + + + )} {openEditModal && ( @@ -240,8 +247,8 @@ export const ResourcesDetails = ({ shardConfigServers: sharding?.configServer?.replicas.toString(), shardNr: sharding?.shards.toString(), }), - numberOfNodes, - numberOfProxies, + numberOfNodes: numberOfNodesStr, + numberOfProxies: numberOfProxiesStr, customNrOfNodes: replicas, customNrOfProxies: proxies, resourceSizePerNode: matchFieldsValueToResourceSize( diff --git a/ui/apps/everest/src/shared-types/dbCluster.types.ts b/ui/apps/everest/src/shared-types/dbCluster.types.ts index 288712dab..c237c0265 100644 --- a/ui/apps/everest/src/shared-types/dbCluster.types.ts +++ b/ui/apps/everest/src/shared-types/dbCluster.types.ts @@ -70,12 +70,14 @@ interface Engine { config?: string; } +export interface ProxyExposeConfig { + type: ProxyExposeType; + ipSourceRanges?: string[]; +} + export interface Proxy { replicas?: number; - expose: { - type: ProxyExposeType; - ipSourceRanges?: string[]; - }; + expose: ProxyExposeConfig; resources?: Resources; type: ProxyType; } @@ -106,7 +108,7 @@ export interface Spec { allowUnsafeConfiguration?: boolean; backup?: Backup; engine: Engine; - proxy: Proxy | Record; + proxy: Proxy | ProxyExposeConfig; paused?: boolean; dataSource?: DataSource; monitoring: Monitoring; diff --git a/ui/apps/everest/src/utils/db.tsx b/ui/apps/everest/src/utils/db.tsx index 2508bc6af..3f98fa488 100644 --- a/ui/apps/everest/src/utils/db.tsx +++ b/ui/apps/everest/src/utils/db.tsx @@ -1,7 +1,7 @@ import { MongoIcon, MySqlIcon, PostgreSqlIcon } from '@percona/ui-lib'; import { DbType } from '@percona/types'; import { ProxyType } from 'shared-types/dbEngines.types'; -import { Proxy } from 'shared-types/dbCluster.types'; +import { Proxy, ProxyExposeConfig } from 'shared-types/dbCluster.types'; export const dbTypeToIcon = (dbType: DbType) => { switch (dbType) { @@ -41,10 +41,6 @@ export const dbTypeToProxyType = (dbType: DbType): ProxyType => { } }; -export const isProxy = ( - proxy: Proxy | Record -): proxy is Proxy => { - return ( - proxy && typeof proxy.expose === 'object' && typeof proxy.type === 'string' - ); +export const isProxy = (proxy: Proxy | ProxyExposeConfig): proxy is Proxy => { + return proxy && typeof (proxy as Proxy).expose === 'object'; }; From 1691322ea9d8ad4d9c1a8e621d2deb2bd4e82a7f Mon Sep 17 00:00:00 2001 From: Fabio Silva Date: Mon, 4 Nov 2024 10:27:29 +0000 Subject: [PATCH 2/2] chore: remove log --- ui/apps/everest/src/pages/databases/expandedRow/ExpandedRow.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/apps/everest/src/pages/databases/expandedRow/ExpandedRow.tsx b/ui/apps/everest/src/pages/databases/expandedRow/ExpandedRow.tsx index c0ee20030..96ae0a5a2 100644 --- a/ui/apps/everest/src/pages/databases/expandedRow/ExpandedRow.tsx +++ b/ui/apps/everest/src/pages/databases/expandedRow/ExpandedRow.tsx @@ -52,7 +52,7 @@ export const ExpandedRow = ({ port, raw, } = row.original; - console.log('row.original', row.original); + const parsedDiskValues = memoryParser(storage.toString()); const parsedMemoryValues = memoryParser(memory.toString()); const parsedProxyMemoryValues = memoryParser(proxyMemory.toString());