Skip to content

Commit

Permalink
chore: remove proxy config for mongo with no sharding
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-silva committed Oct 11, 2024
1 parent 95c8f03 commit c0a4432
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 91 deletions.
50 changes: 32 additions & 18 deletions ui/apps/everest/src/components/cluster-form/resources/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ export const getDefaultNumberOfconfigServersByNumberOfNodes = (
} else return '7';
};

const numberOfResourcesValidator = (
numberOfResourcesStr: string,
customNrOfResoucesStr: string,
fieldPath: string,
ctx: z.RefinementCtx
) => {
if (numberOfResourcesStr === CUSTOM_NR_UNITS_INPUT_VALUE) {
const intNr = parseInt(customNrOfResoucesStr, 10);

if (Number.isNaN(intNr) || intNr < 1) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Please enter a valid number',
path: [fieldPath],
});
}
}
};

export const resourcesFormSchema = (passthrough?: boolean) => {
const objectShape = {
[DbWizardFormFields.shardNr]: z.string().optional(),
Expand Down Expand Up @@ -199,26 +218,21 @@ export const resourcesFormSchema = (passthrough?: boolean) => {
},
ctx
) => {
[
[numberOfNodes, customNrOfNodes, DbWizardFormFields.customNrOfNodes],
[
numberOfResourcesValidator(
numberOfNodes,
customNrOfNodes,
DbWizardFormFields.customNrOfNodes,
ctx
);

if (dbType !== DbType.Mongo || (dbType === DbType.Mongo && !!sharding)) {
numberOfResourcesValidator(
numberOfProxies,
customNrOfProxies,
customNrOfNodes,
DbWizardFormFields.customNrOfProxies,
],
].forEach(([nr, customNr, path]) => {
if (nr === CUSTOM_NR_UNITS_INPUT_VALUE) {
const intNr = parseInt(customNr, 10);

if (Number.isNaN(intNr) || intNr < 1) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Please enter a valid number',
path: [path],
});
}
}
});
ctx
);
}

if (
numberOfNodes === CUSTOM_NR_UNITS_INPUT_VALUE &&
Expand Down
68 changes: 37 additions & 31 deletions ui/apps/everest/src/components/cluster-form/resources/resources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,10 @@ const ResourcesForm = ({
allowDiskInputUpdate,
pairProxiesWithNodes,
showSharding,
showProxies,
}: {
dbType: DbType;
showProxies: boolean;
disableDiskInput?: boolean;
allowDiskInputUpdate?: boolean;
pairProxiesWithNodes?: boolean;
Expand Down Expand Up @@ -559,37 +561,41 @@ const ResourcesForm = ({
disableCustom={dbType === DbType.Mysql}
/>
</Accordion>
<Accordion
expanded={expanded === 'proxies'}
data-testid="proxies-accordion"
onChange={handleAccordionChange('proxies')}
sx={{
px: 2,
}}
>
<CustomAccordionSummary
unitPlural={proxyUnitNames.plural}
nr={parseInt(proxiesAccordionSummaryNumber, 10)}
/>
<Divider />
<ResourcesToggles
dbType={dbType}
unit={proxyUnitNames.singular}
unitPlural={proxyUnitNames.plural}
options={NODES_DB_TYPE_MAP[dbType]}
sizeOptions={PROXIES_DEFAULT_SIZES[dbType]}
resourceSizePerUnitInputName={DbWizardFormFields.resourceSizePerProxy}
cpuInputName={DbWizardFormFields.proxyCpu}
memoryInputName={DbWizardFormFields.proxyMemory}
numberOfUnitsInputName={DbWizardFormFields.numberOfProxies}
customNrOfUnitsInputName={DbWizardFormFields.customNrOfProxies}
/>
{proxyFieldError && (
<FormHelperText error={true}>
{proxyFieldError?.message}
</FormHelperText>
)}
</Accordion>
{!!showProxies && (
<Accordion
expanded={expanded === 'proxies'}
data-testid="proxies-accordion"
onChange={handleAccordionChange('proxies')}
sx={{
px: 2,
}}
>
<CustomAccordionSummary
unitPlural={proxyUnitNames.plural}
nr={parseInt(proxiesAccordionSummaryNumber, 10)}
/>
<Divider />
<ResourcesToggles
dbType={dbType}
unit={proxyUnitNames.singular}
unitPlural={proxyUnitNames.plural}
options={NODES_DB_TYPE_MAP[dbType]}
sizeOptions={PROXIES_DEFAULT_SIZES[dbType]}
resourceSizePerUnitInputName={
DbWizardFormFields.resourceSizePerProxy
}
cpuInputName={DbWizardFormFields.proxyCpu}
memoryInputName={DbWizardFormFields.proxyMemory}
numberOfUnitsInputName={DbWizardFormFields.numberOfProxies}
customNrOfUnitsInputName={DbWizardFormFields.customNrOfProxies}
/>
{proxyFieldError && (
<FormHelperText error={true}>
{proxyFieldError?.message}
</FormHelperText>
)}
</Accordion>
)}
{!!showSharding && !!sharding && (
<CustomPaper sx={{ mt: 2 }}>
<Typography variant="sectionHeading">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const formValuesToPayloadMapping = (
dbPayload.externalAccess,
dbPayload.proxyCpu,
dbPayload.proxyMemory,
dbPayload.sharding,
dbPayload.sourceRanges || []
),
...(dbPayload.dbType === DbType.Mongo && {
Expand Down
78 changes: 44 additions & 34 deletions ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

import { UseMutationOptions, useMutation } from '@tanstack/react-query';
import { updateDbClusterFn } from 'api/dbClusterApi';
import { DbCluster } from 'shared-types/dbCluster.types';
import { DbCluster, Proxy } from 'shared-types/dbCluster.types';
import { DbWizardType } from 'pages/database-form/database-form-schema.ts';
import cronConverter from 'utils/cron-converter';
import { CUSTOM_NR_UNITS_INPUT_VALUE } from 'components/cluster-form';
import { getProxySpec } from './utils';
import { DbType } from '@percona/types';
import { DbEngineType } from 'shared-types/dbEngines.types';

type UpdateDbClusterArgType = {
dbPayload: DbWizardType;
Expand Down Expand Up @@ -95,31 +96,35 @@ const formValuesToPayloadOverrides = (
monitoringConfigName: dbPayload?.monitoringInstance!,
}),
},
proxy: {
...dbCluster.spec.proxy,
...getProxySpec(
dbPayload.dbType,
dbPayload.numberOfProxies,
dbPayload.customNrOfProxies || '',
dbPayload.externalAccess,
dbPayload.proxyCpu,
dbPayload.proxyMemory,
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:
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] : []
// ),
// }),
// },
},
...(dbPayload.dbType === DbType.Mongo && {
sharding: {
enabled: dbPayload.sharding,
Expand Down Expand Up @@ -235,6 +240,7 @@ export const useUpdateDbClusterResources = () =>
mutationFn: ({
dbCluster,
newResources,
sharding,
}: {
dbCluster: DbCluster;
newResources: {
Expand All @@ -247,6 +253,7 @@ export const useUpdateDbClusterResources = () =>
proxyMemory: number;
numberOfProxies: number;
};
sharding: boolean;
}) =>
updateDbClusterFn(dbCluster.metadata.name, dbCluster.metadata.namespace, {
...dbCluster,
Expand All @@ -264,14 +271,17 @@ export const useUpdateDbClusterResources = () =>
size: `${newResources.disk}${newResources.diskUnit}`,
},
},
proxy: {
...dbCluster.spec.proxy,
replicas: newResources.numberOfProxies,
resources: {
cpu: `${newResources.proxyCpu}`,
memory: `${newResources.proxyMemory}G`,
},
},
proxy:
dbCluster.spec.engine.type === DbEngineType.PSMDB && !sharding
? {}
: ({
...dbCluster.spec.proxy,
replicas: newResources.numberOfProxies,
resources: {
cpu: `${newResources.proxyCpu}`,
memory: `${newResources.proxyMemory}G`,
},
} as Proxy),
},
}),
});
Expand Down
12 changes: 11 additions & 1 deletion ui/apps/everest/src/hooks/api/db-cluster/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@ export const getProxySpec = (
externalAccess: boolean,
cpu: number,
memory: number,
sharding: boolean,
sourceRanges?: Array<{ sourceRange?: string }>
): Proxy => {
): Proxy | Record<string, never> => {
console.log('dbType', dbType);
console.log('sharding', sharding);
if (dbType === DbType.Mongo && !sharding) {
console.log('returning empty object');
return {};
}
const proxyNr = parseInt(
numberOfProxies === CUSTOM_NR_UNITS_INPUT_VALUE
? customNrOfProxies
: numberOfProxies,
10
);
// const showResources =
// dbType !== DbType.Mongo || (dbType === DbType.Mongo && !sharding);

return {
type: dbTypeToProxyType(dbType),
replicas: proxyNr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const ResourcesStep = () => {
disableDiskInput={mode === 'edit'}
allowDiskInputUpdate={mode !== 'edit'}
showSharding={dbType === DbType.Mongo}
showProxies={dbType !== DbType.Mongo}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const DbClusterPayloadToFormValues = (
): DbWizardType => {
const backup = dbCluster?.spec?.backup;
const replicas = dbCluster?.spec?.engine?.replicas.toString();
const proxies = dbCluster?.spec?.proxy?.replicas.toString();
const proxies = (dbCluster?.spec?.proxy?.replicas || 0).toString();
const diskValues = memoryParser(
dbCluster?.spec?.engine?.storage?.size.toString()
);
Expand Down
5 changes: 4 additions & 1 deletion ui/apps/everest/src/pages/databases/DbClusterView.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { DbClusterForNamespaceResult } from '../../hooks/api/db-clusters/useDbCl
import { Messages } from './dbClusterView.messages';
import { DbClusterTableElement } from './dbClusterView.types';
import { Backup, BackupStatus } from 'shared-types/backups.types';
import { isProxy } from 'utils/db';

const DB_CLUSTER_STATUS_HUMANIFIED: Record<DbClusterStatus, string> = {
[DbClusterStatus.ready]: Messages.statusProvider.up,
Expand Down Expand Up @@ -55,7 +56,9 @@ export const convertDbClusterPayloadToTableFormat = (
storage: cluster.spec.engine.storage.size,
nodes: cluster.spec.engine.replicas,
hostName: cluster.status ? cluster.status.hostname : '',
exposetype: cluster.spec.proxy.expose.type,
exposetype: isProxy(cluster.spec.proxy)
? cluster.spec.proxy.expose.type
: undefined,
port: cluster.status?.port,
monitoringConfigName:
cluster.spec.monitoring?.monitoringConfigName ?? '',
Expand Down
2 changes: 1 addition & 1 deletion ui/apps/everest/src/pages/databases/dbClusterView.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface DbClusterTableElement {
nodes: number;
hostName: string;
port?: number;
exposetype: ProxyExposeType;
exposetype?: ProxyExposeType;
monitoringConfigName?: string;
raw: DbCluster;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
import { dbEngineToDbType } from '@percona/utils';
import { DB_CLUSTER_QUERY, useUpdateDbClusterResources } from 'hooks';
import { DbType } from '@percona/types';
import { isProxy } from 'utils/db';

export const ResourcesDetails = ({
dbCluster,
Expand All @@ -58,7 +59,9 @@ export const ResourcesDetails = ({
const parsedMemoryValues = memoryParser(memory.toString());
const dbType = dbEngineToDbType(dbCluster.spec.engine.type);
const replicas = dbCluster.spec.engine.replicas.toString();
const proxies = dbCluster.spec.proxy.replicas.toString();
const proxies = isProxy(dbCluster.spec.proxy)
? (dbCluster.spec.proxy.replicas || 0).toString()
: '';
const numberOfNodes = NODES_DB_TYPE_MAP[dbType].includes(replicas)
? replicas
: CUSTOM_NR_UNITS_INPUT_VALUE;
Expand Down Expand Up @@ -103,6 +106,7 @@ export const ResourcesDetails = ({
10
),
},
sharding: !!sharding?.enabled,
},
{
onSuccess: () => {
Expand Down Expand Up @@ -213,7 +217,9 @@ export const ResourcesDetails = ({
),
resourceSizePerProxy: matchFieldsValueToResourceSize(
dbType,
dbCluster.spec.proxy.resources
isProxy(dbCluster.spec.proxy)
? dbCluster.spec.proxy.resources
: undefined
),
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const ResourcesEditModal = ({
showSharding={false}
disableDiskInput
allowDiskInputUpdate={false}
showProxies={dbType !== DbType.Mongo}
/>
</FormDialog>
);
Expand Down
Loading

0 comments on commit c0a4432

Please sign in to comment.