From bfe142e725fed2dd6b97bf49e94c11f0e78a75e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Silva?= Date: Wed, 6 Nov 2024 14:50:12 +0000 Subject: [PATCH] fix: wrong proxy expose type for psmdb (#801) * fix: wrong proxy expose type for psmdb * chore: remove log --- .../api/db-cluster/useUpdateDbCluster.ts | 66 ++++++++----------- .../everest/src/hooks/api/db-cluster/utils.ts | 37 +++++++---- .../database-form/database-form.utils.ts | 44 ++++++++----- .../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 | 23 ++++--- 8 files changed, 160 insertions(+), 126 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 a33a3a4a5..bb2e6c8cd 100644 --- a/ui/apps/everest/src/hooks/api/db-cluster/utils.ts +++ b/ui/apps/everest/src/hooks/api/db-cluster/utils.ts @@ -1,7 +1,24 @@ import { DbType } from '@percona/types'; import { dbTypeToProxyType } from '@percona/utils'; 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'; + +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, @@ -12,9 +29,11 @@ export const getProxySpec = ( memory: number, sharding: boolean, sourceRanges?: Array<{ sourceRange?: string }> -): Proxy | Record => { +): Proxy | ProxyExposeConfig => { if (dbType === DbType.Mongo && !sharding) { - return {}; + return { + expose: getExposteConfig(externalAccess, sourceRanges), + } as unknown as ProxyExposeConfig; } const proxyNr = parseInt( numberOfProxies === CUSTOM_NR_UNITS_INPUT_VALUE @@ -32,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 756b5940f..d4059dcde 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,9 +29,11 @@ import { getDefaultNumberOfconfigServersByNumberOfNodes, matchFieldsValueToResourceSize, NODES_DB_TYPE_MAP, + ResourceSize, NODES_DEFAULT_SIZES, PROXIES_DEFAULT_SIZES, } from 'components/cluster-form'; +import { isProxy } from 'utils/db.tsx'; const replicasToNodes = (replicas: string, dbType: DbType): string => { const nodeOptions = NODES_DB_TYPE_MAP[dbType]; @@ -51,7 +53,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() ); @@ -61,6 +65,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 @@ -77,17 +84,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, @@ -104,10 +109,14 @@ export const DbClusterPayloadToFormValues = ( NODES_DEFAULT_SIZES[dbEngineToDbType(dbCluster?.spec?.engine?.type)], dbCluster?.spec?.engine?.resources ), - [DbWizardFormFields.resourceSizePerProxy]: matchFieldsValueToResourceSize( - PROXIES_DEFAULT_SIZES[dbEngineToDbType(dbCluster?.spec?.engine?.type)], - dbCluster?.spec?.proxy.resources - ), + [DbWizardFormFields.resourceSizePerProxy]: isProxy(dbCluster?.spec?.proxy) + ? matchFieldsValueToResourceSize( + PROXIES_DEFAULT_SIZES[ + dbEngineToDbType(dbCluster?.spec?.engine?.type) + ], + dbCluster?.spec?.proxy.resources + ) + : ResourceSize.small, [DbWizardFormFields.sharding]: dbCluster?.spec?.sharding?.enabled || false, [DbWizardFormFields.shardConfigServers]: ( sharding?.configServer?.replicas || @@ -120,9 +129,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( @@ -130,7 +139,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..96ae0a5a2 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; + 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 && ( @@ -243,8 +250,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 24057d53a..75f007c23 100644 --- a/ui/apps/everest/src/shared-types/dbCluster.types.ts +++ b/ui/apps/everest/src/shared-types/dbCluster.types.ts @@ -71,12 +71,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; } @@ -107,7 +109,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 dd463333f..8afca5cfc 100644 --- a/ui/apps/everest/src/utils/db.tsx +++ b/ui/apps/everest/src/utils/db.tsx @@ -1,6 +1,6 @@ import { MongoIcon, MySqlIcon, PostgreSqlIcon } from '@percona/ui-lib'; -import { DbType } from '@percona/types'; -import { Proxy } from 'shared-types/dbCluster.types'; +import { DbType, ProxyType } from '@percona/types'; +import { Proxy, ProxyExposeConfig } from 'shared-types/dbCluster.types'; export const dbTypeToIcon = (dbType: DbType) => { switch (dbType) { @@ -29,10 +29,17 @@ export const shortenOperatorName = (name: string) => { return name; }; -export const isProxy = ( - proxy: Proxy | Record -): proxy is Proxy => { - return ( - proxy && typeof proxy.expose === 'object' && typeof proxy.type === 'string' - ); +export const dbTypeToProxyType = (dbType: DbType): ProxyType => { + switch (dbType) { + case DbType.Mongo: + return 'mongos'; + case DbType.Mysql: + return 'haproxy'; + default: + return 'pgbouncer'; + } +}; + +export const isProxy = (proxy: Proxy | ProxyExposeConfig): proxy is Proxy => { + return proxy && typeof (proxy as Proxy).expose === 'object'; };