Skip to content

Commit

Permalink
fix: db miss dbtype & update operation log (labring#5315)
Browse files Browse the repository at this point in the history
* fix db miss dbtype

* update template

* support withdrawal

* update api

* update

* update launchpad

* update OperationLog

* add redirect

* update name

* update

* update

* update ops

* fix launchpad gpu

* update
zjy365 authored Dec 30, 2024
1 parent 585b50f commit f45112f
Showing 29 changed files with 767 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -76,7 +76,10 @@ const AppBaseInfo = ({ app = MOCK_APP_DETAIL }: { app: AppDetailType }) => {
iconName: 'deployMode',
items: app.hpa.use
? [
{ label: `${app.hpa.target} ${t('target_value')}`, value: `${app.hpa.value}%` },
{
label: `${app.hpa.target} ${t('target_value')}`,
value: `${app.hpa.value}${app.hpa.target === 'gpu' ? '' : '%'}`
},
{
label: 'Number of Instances',
value: `${app.hpa.minReplicas} ~ ${app.hpa.maxReplicas}`
Original file line number Diff line number Diff line change
@@ -281,13 +281,17 @@ const Form = ({
const cpuList = formSliderListConfig[key].cpu;
const memoryList = formSliderListConfig[key].memory;

const sortedCpuList =
cpu !== undefined ? [...new Set([...cpuList, cpu])].sort((a, b) => a - b) : cpuList;
const sortedCpuList = !!gpuType
? cpuList
: cpu !== undefined
? [...new Set([...cpuList, cpu])].sort((a, b) => a - b)
: cpuList;

const sortedMemoryList =
memory !== undefined
? [...new Set([...memoryList, memory])].sort((a, b) => a - b)
: memoryList;
const sortedMemoryList = !!gpuType
? memoryList
: memory !== undefined
? [...new Set([...memoryList, memory])].sort((a, b) => a - b)
: memoryList;

return {
cpu: sliderNumber2MarkList({
@@ -304,7 +308,7 @@ const Form = ({
}, [formSliderListConfig, getValues]);

// eslint-disable-next-line react-hooks/exhaustive-deps
const SliderList = useMemo(() => countSliderList(), [already]);
const SliderList = useMemo(() => countSliderList(), [already, refresh]);

return (
<>
@@ -600,7 +604,7 @@ const Form = ({
}
})}
/>
<Box>%</Box>
<Box>{getValues('hpa.target') === 'gpu' ? '' : '%'}</Box>
<Tip
ml={4}
icon={<InfoOutlineIcon />}
@@ -675,6 +679,9 @@ const Form = ({
const inventory = countGpuInventory(type);
if (type === '' || (selected && inventory > 0)) {
setValue('gpu.type', type);
const sliderList = countSliderList();
setValue('cpu', sliderList.cpu[1].value);
setValue('memory', sliderList.memory[1].value);
}
}}
/>
2 changes: 1 addition & 1 deletion frontend/providers/applaunchpad/src/utils/adapt.ts
Original file line number Diff line number Diff line change
@@ -413,7 +413,7 @@ export const sliderNumber2MarkList = ({
type: 'cpu' | 'memory';
gpuAmount?: number;
}) => {
const newVal = val.map((item) => item * gpuAmount);
const newVal = val.map((item) => item);

return newVal.map((item) => ({
label: type === 'memory' ? (item >= 1024 ? `${item / 1024} G` : `${item} M`) : `${item / 1000}`,
11 changes: 9 additions & 2 deletions frontend/providers/dbprovider/public/locales/en/common.json
Original file line number Diff line number Diff line change
@@ -145,7 +145,7 @@
"db_name": "DataBase Name",
"db_table": "DataBase Table",
"dbconfig": {
"change_history": "Modification History",
"change_history": "Operation History",
"commit": "submit",
"confirm_updates": "Please confirm the parameters you modified:",
"get_config_err": "Failed to obtain configuration file",
@@ -330,5 +330,12 @@
"within_1_hour": "Within 1 hour",
"within_5_minutes": "Within 5 minutes",
"yaml_file": "YAML",
"you_have_successfully_deployed_database": "You have successfully deployed and created a database!"
"you_have_successfully_deployed_database": "You have successfully deployed and created a database!",
"change_log": "History",
"VerticalScaling": "Vertical Scaling",
"VerticalScalingCPU": "Vertical Scaling (CPU)",
"VerticalScalingMemory": "Vertical Scaling (Memory)",
"HorizontalScaling": "Horizontal Scaling (Instance)",
"VolumeExpansion": "Volume Expansion (Disk)",
"Stop": "Stop"
}
13 changes: 10 additions & 3 deletions frontend/providers/dbprovider/public/locales/zh/common.json
Original file line number Diff line number Diff line change
@@ -145,7 +145,7 @@
"db_name": "数据库名字",
"db_table": "数据库表",
"dbconfig": {
"change_history": "修改历史",
"change_history": "变更历史",
"commit": "提交",
"confirm_updates": "请确认您修改的参数:",
"get_config_err": "获取配置文件失败",
@@ -331,5 +331,12 @@
"within_1_hour": "一小时内",
"within_5_minutes": "五分钟内",
"yaml_file": "YAML 文件",
"you_have_successfully_deployed_database": "您已成功部署创建一个数据库!"
}
"you_have_successfully_deployed_database": "您已成功部署创建一个数据库!",
"change_log": "变更历史",
"VerticalScaling": "纵向扩展",
"VerticalScalingCPU": "纵向扩展(CPU)",
"VerticalScalingMemory": "纵向扩展(内存)",
"HorizontalScaling": "横向扩展(实例数)",
"VolumeExpansion": "卷扩展(磁盘)",
"Stop": "停止"
}
10 changes: 8 additions & 2 deletions frontend/providers/dbprovider/src/api/db.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import type {
import { LogTypeEnum } from '@/constants/log';
import { MonitorChartDataResult } from '@/types/monitor';
import { adaptDBDetail, adaptDBListItem, adaptEvents, adaptPod } from '@/utils/adapt';
import { json2Restart } from '@/utils/json2Yaml';
import { json2BasicOps } from '@/utils/json2Yaml';
import { TFile } from '@/utils/kubeFileSystem';
import { LogResult } from '@/utils/logParsers/LogParser';
import { V1Service, V1StatefulSet } from '@kubernetes/client-node';
@@ -62,7 +62,7 @@ export const restartPodByName = (podName: string) => GET(`/api/pod/restartPod?po

/* db operation */
export const restartDB = (data: { dbName: string; dbType: DBType }) => {
const yaml = json2Restart(data);
const yaml = json2BasicOps({ ...data, type: 'Restart' });
return applyYamlList([yaml], 'update');
};

@@ -106,6 +106,12 @@ export const getOpsRequest = ({
dbType
});

export const getOperationLog = ({ name, dbType }: { name: string; dbType: DBType }) =>
GET<OpsRequestItemType[]>(`/api/opsrequest/operationlog`, {
name,
dbType
});

export const getLogFiles = ({
podName,
dbType,
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion frontend/providers/dbprovider/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -146,7 +146,7 @@ function App({ Component, pageProps }: AppProps) {
try {
if (e.data?.type === 'InternalAppCall' && e.data?.name) {
router.push({
pathname: '/db/detail',
pathname: '/redirect',
query: {
name: e.data.name
}
12 changes: 7 additions & 5 deletions frontend/providers/dbprovider/src/pages/api/createDB.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { jsonRes } from '@/services/backend/response';
import { ApiResp } from '@/services/kubernet';
import { KbPgClusterType } from '@/types/cluster';
import { BackupItemType, DBEditType } from '@/types/db';
import { json2Account, json2ClusterOps, json2CreateCluster } from '@/utils/json2Yaml';
import { json2Account, json2ResourceOps, json2CreateCluster } from '@/utils/json2Yaml';
import type { NextApiRequest, NextApiResponse } from 'next';
import { updateBackupPolicyApi } from './backup/updatePolicy';
import { BackupSupportedDBTypeList } from '@/constants/db';
@@ -38,17 +38,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
const opsRequests = [];

if (cpu !== dbForm.cpu || memory !== dbForm.memory) {
const verticalScalingYaml = json2ClusterOps(dbForm, 'VerticalScaling');
const verticalScalingYaml = json2ResourceOps(dbForm, 'VerticalScaling');
opsRequests.push(verticalScalingYaml);
}

if (replicas !== dbForm.replicas) {
const horizontalScalingYaml = json2ClusterOps(dbForm, 'HorizontalScaling');
const horizontalScalingYaml = json2ResourceOps(dbForm, 'HorizontalScaling');
opsRequests.push(horizontalScalingYaml);
}

if (dbForm.storage > storage) {
const volumeExpansionYaml = json2ClusterOps(dbForm, 'VolumeExpansion');
const volumeExpansionYaml = json2ResourceOps(dbForm, 'VolumeExpansion');
opsRequests.push(volumeExpansionYaml);
}

@@ -97,7 +97,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
}

const account = json2Account(dbForm);
const cluster = json2CreateCluster(dbForm, backupInfo);
const cluster = json2CreateCluster(dbForm, backupInfo, {
storageClassName: process.env.STORAGE_CLASSNAME
});
await applyYamlList([account, cluster], 'create');
const { body } = (await k8sCustomObjects.getNamespacedCustomObject(
'apps.kubeblocks.io',
7 changes: 5 additions & 2 deletions frontend/providers/dbprovider/src/pages/api/getDBByName.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { ApiResp } from '@/services/kubernet';
import { authSession } from '@/services/backend/auth';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
import { KbPgClusterType } from '@/types/cluster';

export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
@@ -29,13 +30,15 @@ export async function getCluster(req: NextApiRequest, name: string) {
kubeconfig: await authSession(req)
});

const { body } = await k8sCustomObjects.getNamespacedCustomObject(
const { body } = (await k8sCustomObjects.getNamespacedCustomObject(
'apps.kubeblocks.io',
'v1alpha1',
namespace,
'clusters',
name
);
)) as {
body: KbPgClusterType;
};

return body;
}
137 changes: 137 additions & 0 deletions frontend/providers/dbprovider/src/pages/api/opsrequest/operationlog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { crLabelKey, DBReconfigStatusMap } from '@/constants/db';
import { authSession } from '@/services/backend/auth';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
import { ApiResp } from '@/services/kubernet';
import { KubeBlockOpsRequestType } from '@/types/cluster';
import { DBType, OpsRequestItemType } from '@/types/db';
import { cpuFormatToC, memoryFormatToGi } from '@/utils/tools';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
const { name } = req.query as {
name: string;
};

const { k8sCustomObjects, namespace } = await getK8s({
kubeconfig: await authSession(req)
});

let labelSelector = `app.kubernetes.io/instance=${name},${crLabelKey}=${name}`;

const opsrequestsList = (await k8sCustomObjects.listNamespacedCustomObject(
'apps.kubeblocks.io',
'v1alpha1',
namespace,
'opsrequests',
undefined,
undefined,
undefined,
undefined,
labelSelector
)) as {
body: {
items: KubeBlockOpsRequestType[];
};
};

const opsrequests = opsrequestsList.body.items
.map((item) => {
const simpleTypes = ['Start', 'Stop', 'Restart'];

const configurations = (() => {
if (simpleTypes.includes(item.spec.type)) {
return [{ parameterName: item.spec.type, newValue: '-', oldValue: '-' }];
}

if (item.spec.type === 'VerticalScaling') {
const componentName = item.spec?.verticalScaling?.[0]?.componentName!;
const oldConfig = item.status?.lastConfiguration?.components?.[componentName];
const newConfig = item.spec?.verticalScaling?.[0];

const changedConfigs = [];

if (oldConfig?.limits?.cpu !== newConfig?.limits?.cpu) {
changedConfigs.push({
parameterName: item.spec.type + 'CPU',
newValue: cpuFormatToC(newConfig?.limits?.cpu) || '-',
oldValue: cpuFormatToC(oldConfig?.limits?.cpu) || '-'
});
}

if (oldConfig?.limits?.memory !== newConfig?.limits?.memory) {
changedConfigs.push({
parameterName: item.spec.type + 'Memory',
newValue: memoryFormatToGi(newConfig?.limits?.memory) || '-',
oldValue: memoryFormatToGi(oldConfig?.limits?.memory) || '-'
});
}

return changedConfigs;
}

if (item.spec.type === 'HorizontalScaling') {
const componentName = item.spec?.horizontalScaling?.[0]?.componentName!;
const oldReplicas =
item.status?.lastConfiguration?.components?.[componentName]?.replicas;
const newReplicas = item.spec?.horizontalScaling?.[0]?.replicas;

return [
{
parameterName: 'HorizontalScaling',
newValue: String(newReplicas || '-'),
oldValue: String(oldReplicas || '-')
}
];
}

if (item.spec.type === 'VolumeExpansion') {
const componentName = item.spec?.volumeExpansion?.[0]?.componentName!;
const oldStorage =
item.status?.lastConfiguration?.components?.[componentName]?.volumeClaimTemplates?.[0]
?.storage;
const newStorage = item.spec?.volumeExpansion?.[0]?.volumeClaimTemplates?.[0]?.storage;

return [
{
parameterName: 'VolumeExpansion',
newValue: String(newStorage || '-'),
oldValue: String(oldStorage || '-')
}
];
}

return [
{
parameterName: item.spec.type,
newValue: '-',
oldValue: '-'
}
];
})();

return {
id: item.metadata.uid,
name: item.metadata.name,
status:
item.status?.phase && DBReconfigStatusMap[item.status.phase]
? DBReconfigStatusMap[item.status.phase]
: DBReconfigStatusMap.Creating,
startTime: new Date(item.metadata.creationTimestamp),
namespace: item.metadata.namespace,
configurations
};
})
.filter((item) => item.configurations.length > 0) as OpsRequestItemType[];

jsonRes(res, {
data: opsrequests
});
} catch (err: any) {
jsonRes(res, {
code: 500,
error: err
});
}
}
Loading
Oops, something went wrong.

0 comments on commit f45112f

Please sign in to comment.