From 9125932cd491cdebbb579371bfe9a1439ec55602 Mon Sep 17 00:00:00 2001 From: zhujingyang <72259332+zjy365@users.noreply.github.com> Date: Wed, 17 Jan 2024 20:40:59 +0800 Subject: [PATCH] feat:db adapt mongo instance count (#4490) * feat:db adapt mongo instance count Signed-off-by: jingyang <3161362058@qq.com> * app logo Signed-off-by: jingyang <3161362058@qq.com> --------- Signed-off-by: jingyang <3161362058@qq.com> --- .../src/components/desktop_content/index.tsx | 3 +- .../components/signin/auth/useProtocol.tsx | 2 +- .../desktop/src/components/signin/index.tsx | 3 +- frontend/desktop/src/constants/config.ts | 1 + frontend/providers/adminer/public/logo.svg | 30 ++++- .../applaunchpad/public/logo-colorful.svg | 9 -- .../providers/applaunchpad/public/logo.svg | 10 +- .../costcenter/public/logo-colorful.svg | 10 -- frontend/providers/costcenter/public/logo.svg | 11 +- .../cronjob/public/logo-colorful.svg | 9 -- frontend/providers/cronjob/public/logo.svg | 10 +- .../dbprovider/public/locales/en/common.json | 3 +- .../dbprovider/public/locales/zh/common.json | 3 +- .../dbprovider/public/logo-colorful.svg | 19 --- frontend/providers/dbprovider/public/logo.svg | 24 +++- frontend/providers/dbprovider/src/api/db.ts | 3 + .../src/pages/api/adapter/mongodb.ts | 108 ++++++++++++++++++ .../src/pages/db/edit/components/Form.tsx | 12 ++ .../dbprovider/src/pages/db/edit/index.tsx | 6 +- .../src/services/backend/response.ts | 3 +- frontend/providers/license/public/logo.svg | 10 +- .../objectstorage/public/logo-colorful.svg | 9 -- .../providers/objectstorage/public/logo.svg | 12 +- .../template/public/logo-colorful.svg | 9 -- frontend/providers/template/public/logo.svg | 10 +- .../terminal/public/logo-colorful.svg | 9 -- frontend/providers/terminal/public/logo.svg | 12 +- 27 files changed, 247 insertions(+), 103 deletions(-) create mode 100644 frontend/desktop/src/constants/config.ts delete mode 100644 frontend/providers/applaunchpad/public/logo-colorful.svg delete mode 100644 frontend/providers/costcenter/public/logo-colorful.svg delete mode 100644 frontend/providers/cronjob/public/logo-colorful.svg delete mode 100644 frontend/providers/dbprovider/public/logo-colorful.svg create mode 100644 frontend/providers/dbprovider/src/pages/api/adapter/mongodb.ts delete mode 100644 frontend/providers/objectstorage/public/logo-colorful.svg delete mode 100644 frontend/providers/template/public/logo-colorful.svg delete mode 100644 frontend/providers/terminal/public/logo-colorful.svg diff --git a/frontend/desktop/src/components/desktop_content/index.tsx b/frontend/desktop/src/components/desktop_content/index.tsx index 1b084aef33a..51de3ab30f7 100644 --- a/frontend/desktop/src/components/desktop_content/index.tsx +++ b/frontend/desktop/src/components/desktop_content/index.tsx @@ -11,6 +11,7 @@ import { createMasterAPP, masterApp } from 'sealos-desktop-sdk/master'; import IframeWindow from './iframe_window'; import styles from './index.module.scss'; import useDriver from '@/hooks/useDriver'; +import { BackgroundImageUrl } from '@/constants/config'; const TimeComponent = dynamic(() => import('./time'), { ssr: false @@ -86,7 +87,7 @@ export default function DesktopContent(props: any) { diff --git a/frontend/desktop/src/components/signin/auth/useProtocol.tsx b/frontend/desktop/src/components/signin/auth/useProtocol.tsx index e974965e748..36574dab25c 100644 --- a/frontend/desktop/src/components/signin/auth/useProtocol.tsx +++ b/frontend/desktop/src/components/signin/auth/useProtocol.tsx @@ -12,7 +12,7 @@ const useProtocol = ({ const { t, i18n } = useTranslation(); const [isAgree, setIsAgree] = useState(false); const [isInvalid, setIsInvalid] = useState(false); - console.log(service_protocol, private_protocol); + const Protocol = () => ( diff --git a/frontend/desktop/src/constants/config.ts b/frontend/desktop/src/constants/config.ts new file mode 100644 index 00000000000..2d4cc49600d --- /dev/null +++ b/frontend/desktop/src/constants/config.ts @@ -0,0 +1 @@ +export const BackgroundImageUrl = '/images/bg-blue.svg'; diff --git a/frontend/providers/adminer/public/logo.svg b/frontend/providers/adminer/public/logo.svg index e15156818cd..e001dbbcad4 100644 --- a/frontend/providers/adminer/public/logo.svg +++ b/frontend/providers/adminer/public/logo.svg @@ -1 +1,29 @@ - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/frontend/providers/applaunchpad/public/logo-colorful.svg b/frontend/providers/applaunchpad/public/logo-colorful.svg deleted file mode 100644 index 3dc67a4d75e..00000000000 --- a/frontend/providers/applaunchpad/public/logo-colorful.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/frontend/providers/applaunchpad/public/logo.svg b/frontend/providers/applaunchpad/public/logo.svg index f99324f2e5e..3dc67a4d75e 100644 --- a/frontend/providers/applaunchpad/public/logo.svg +++ b/frontend/providers/applaunchpad/public/logo.svg @@ -1 +1,9 @@ - \ No newline at end of file + + + + + + + + + diff --git a/frontend/providers/costcenter/public/logo-colorful.svg b/frontend/providers/costcenter/public/logo-colorful.svg deleted file mode 100644 index c06a2437d9a..00000000000 --- a/frontend/providers/costcenter/public/logo-colorful.svg +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/frontend/providers/costcenter/public/logo.svg b/frontend/providers/costcenter/public/logo.svg index abcd81daea1..c06a2437d9a 100644 --- a/frontend/providers/costcenter/public/logo.svg +++ b/frontend/providers/costcenter/public/logo.svg @@ -1 +1,10 @@ - \ No newline at end of file + + + + + + + + + + diff --git a/frontend/providers/cronjob/public/logo-colorful.svg b/frontend/providers/cronjob/public/logo-colorful.svg deleted file mode 100644 index e65326cb41b..00000000000 --- a/frontend/providers/cronjob/public/logo-colorful.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/frontend/providers/cronjob/public/logo.svg b/frontend/providers/cronjob/public/logo.svg index d826088c31e..e65326cb41b 100644 --- a/frontend/providers/cronjob/public/logo.svg +++ b/frontend/providers/cronjob/public/logo.svg @@ -1,3 +1,9 @@ - - + + + + + + + + diff --git a/frontend/providers/dbprovider/public/locales/en/common.json b/frontend/providers/dbprovider/public/locales/en/common.json index 28fdec83fc2..8b298ca7861 100644 --- a/frontend/providers/dbprovider/public/locales/en/common.json +++ b/frontend/providers/dbprovider/public/locales/en/common.json @@ -258,5 +258,6 @@ "Wednesday": "Wednesday", "Failed to turn off automatic backup": "Failed to turn off automatic backup", "Automatic backup is turned off": "Automatic backup is turned off", - "Are you sure you want to turn off automatic backup": "Are you sure you want to turn off automatic backup?" + "Are you sure you want to turn off automatic backup": "Are you sure you want to turn off automatic backup?", + "db instances tip": "The number of {{db}} instances is recommended to be an odd number" } \ No newline at end of file diff --git a/frontend/providers/dbprovider/public/locales/zh/common.json b/frontend/providers/dbprovider/public/locales/zh/common.json index 8f835058ea3..66b9d75208c 100644 --- a/frontend/providers/dbprovider/public/locales/zh/common.json +++ b/frontend/providers/dbprovider/public/locales/zh/common.json @@ -260,5 +260,6 @@ "Backup Name cannot empty": "备份名称不能为空", "Failed to turn off automatic backup": "关闭自动备份失败", "Automatic backup is turned off": "已关闭自动备份", - "Are you sure you want to turn off automatic backup": "确定关闭自动备份吗" + "Are you sure you want to turn off automatic backup": "确定关闭自动备份吗", + "db instances tip": "{{db}} 实例数量建议为奇数" } \ No newline at end of file diff --git a/frontend/providers/dbprovider/public/logo-colorful.svg b/frontend/providers/dbprovider/public/logo-colorful.svg deleted file mode 100644 index 7fba92be49a..00000000000 --- a/frontend/providers/dbprovider/public/logo-colorful.svg +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/frontend/providers/dbprovider/public/logo.svg b/frontend/providers/dbprovider/public/logo.svg index d09f08ca92c..7fba92be49a 100644 --- a/frontend/providers/dbprovider/public/logo.svg +++ b/frontend/providers/dbprovider/public/logo.svg @@ -1,5 +1,19 @@ - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + diff --git a/frontend/providers/dbprovider/src/api/db.ts b/frontend/providers/dbprovider/src/api/db.ts index 385bb0a45be..149260ba71b 100644 --- a/frontend/providers/dbprovider/src/api/db.ts +++ b/frontend/providers/dbprovider/src/api/db.ts @@ -64,3 +64,6 @@ export const delDBServiceByName = (name: string) => DELETE('/api/delServiceByNam export const getDBStatefulSetByName = (name: string, dbType: DBType) => GET(`/api/getStatefulSetByName?name=${name}&dbType=${dbType}`); + +export const adapterMongoHaConfig = (payload: { name: string }) => + POST('/api/adapter/mongodb', payload); diff --git a/frontend/providers/dbprovider/src/pages/api/adapter/mongodb.ts b/frontend/providers/dbprovider/src/pages/api/adapter/mongodb.ts new file mode 100644 index 00000000000..ad91648d9aa --- /dev/null +++ b/frontend/providers/dbprovider/src/pages/api/adapter/mongodb.ts @@ -0,0 +1,108 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import { ApiResp } from '@/services/kubernet'; +import { authSession } from '@/services/backend/auth'; +import { getK8s } from '@/services/backend/kubernetes'; +import { jsonRes } from '@/services/backend/response'; +import yaml from 'js-yaml'; +import { adaptPod } from '@/utils/adapt'; + +export const json2HaConfig = ({ + name, + namespace, + enable +}: { + name: string; + namespace: string; + enable: string; +}) => { + const template = { + apiVersion: 'v1', + kind: 'ConfigMap', + metadata: { + annotations: { + MaxLagOnSwitchover: '0', + enable: enable, + ttl: '5' + }, + labels: { + 'app.kubernetes.io/instance': name, + 'app.kubernetes.io/managed-by': 'kubeblocks', + 'apps.kubeblocks.io/component-name': 'mongodb' + }, + name: `${name}-mongodb-haconfig`, + namespace: namespace + } + }; + return yaml.dump(template); +}; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + const { name } = req.body as { + name: string; + }; + const { applyYamlList, namespace, k8sCore } = await getK8s({ + kubeconfig: await authSession(req) + }); + const yaml = json2HaConfig({ + name, + namespace, + enable: 'true' + }); + const result = await applyYamlList([yaml], 'update'); + + const startTime = Date.now(); + let allItemsStarted = false; + const pollingTime = 5 * 60 * 1000; + const pollingInterval = 30 * 1000; + let pollTimeout: NodeJS.Timeout; + + const pollStatus = async () => { + const pods = await k8sCore + .listNamespacedPod( + namespace, + undefined, + undefined, + undefined, + undefined, + `app.kubernetes.io/instance=${name}` + ) + .then((res) => res.body.items.map(adaptPod)); + + allItemsStarted = pods.every((item) => { + return item.status.every((i) => i.started); + }); + + if (allItemsStarted) { + const yaml = json2HaConfig({ + name, + namespace, + enable: 'false' + }); + const result = await applyYamlList([yaml], 'update'); + clearTimeout(pollTimeout); + } + + const elapsedTime = Date.now() - startTime; + if (elapsedTime < pollingTime) { + pollTimeout = setTimeout(pollStatus, pollingInterval); + } else { + const yaml = json2HaConfig({ + name, + namespace, + enable: 'false' + }); + const result = await applyYamlList([yaml], 'update'); + } + }; + + pollTimeout = setTimeout(pollStatus, 1 * 60 * 1000); + + jsonRes(res, { data: result }); + } catch (err: any) { + jsonRes(res, { + code: 500, + error: err + }); + } +} diff --git a/frontend/providers/dbprovider/src/pages/db/edit/components/Form.tsx b/frontend/providers/dbprovider/src/pages/db/edit/components/Form.tsx index 0417497a764..1ad36e1ab47 100644 --- a/frontend/providers/dbprovider/src/pages/db/edit/components/Form.tsx +++ b/frontend/providers/dbprovider/src/pages/db/edit/components/Form.tsx @@ -340,6 +340,18 @@ const Form = ({ size="sm" /> )} + {(getValues('dbType') === DBTypeEnum.mongodb || + getValues('dbType') === DBTypeEnum.mysql) && + getValues('replicas') > 1 && ( + } + text={t('db instances tip', { + db: getValues('dbType') + })} + size="sm" + /> + )} diff --git a/frontend/providers/dbprovider/src/pages/db/edit/index.tsx b/frontend/providers/dbprovider/src/pages/db/edit/index.tsx index 05961e8236e..fb05f429e9d 100644 --- a/frontend/providers/dbprovider/src/pages/db/edit/index.tsx +++ b/frontend/providers/dbprovider/src/pages/db/edit/index.tsx @@ -7,7 +7,7 @@ import { useForm } from 'react-hook-form'; import { editModeMap } from '@/constants/editApp'; import { defaultDBEditValue } from '@/constants/db'; import debounce from 'lodash/debounce'; -import { applyYamlList } from '@/api/db'; +import { adapterMongoHaConfig, applyYamlList, getPodsByDBName } from '@/api/db'; import { useConfirm } from '@/hooks/useConfirm'; import type { DBEditType } from '@/types/db'; import { useToast } from '@/hooks/useToast'; @@ -99,9 +99,12 @@ const EditApp = ({ dbName, tabType }: { dbName?: string; tabType?: 'form' | 'yam }); const submitSuccess = async (formData: DBEditType) => { + const needMongoAdapter = + formData.dbType === 'mongodb' && formData.replicas !== oldDBEditData.current?.replicas; setIsLoading(true); try { !isEdit && (await applyYamlList([limitRangeYaml], 'create')); + needMongoAdapter && (await adapterMongoHaConfig({ name: formData.dbName })); } catch (err) {} try { const yamlList = generateYamlList(formData).map((item) => item.value); @@ -117,6 +120,7 @@ const EditApp = ({ dbName, tabType }: { dbName?: string; tabType?: 'form' | 'yam }); } await applyYamlList(yamlList, isEdit ? 'replace' : 'create'); + toast({ title: t(applySuccess), status: 'success' diff --git a/frontend/providers/dbprovider/src/services/backend/response.ts b/frontend/providers/dbprovider/src/services/backend/response.ts index 0aa716c0335..1136186a21e 100644 --- a/frontend/providers/dbprovider/src/services/backend/response.ts +++ b/frontend/providers/dbprovider/src/services/backend/response.ts @@ -26,8 +26,7 @@ export const jsonRes = ( } else if (error?.code && error.code in ERROR_TEXT) { msg = ERROR_TEXT[error.code]; } - console.log('error:', error?.body || error); - console.log('error message:', msg); + console.log('===jsonRes===\n', error); } res.json({ diff --git a/frontend/providers/license/public/logo.svg b/frontend/providers/license/public/logo.svg index 956fd4ac3f9..e189b86ba3e 100644 --- a/frontend/providers/license/public/logo.svg +++ b/frontend/providers/license/public/logo.svg @@ -1,3 +1,9 @@ - - + + + + + + + + diff --git a/frontend/providers/objectstorage/public/logo-colorful.svg b/frontend/providers/objectstorage/public/logo-colorful.svg deleted file mode 100644 index 3d4a2d33cdd..00000000000 --- a/frontend/providers/objectstorage/public/logo-colorful.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/frontend/providers/objectstorage/public/logo.svg b/frontend/providers/objectstorage/public/logo.svg index d9bf6fe09fc..3d4a2d33cdd 100644 --- a/frontend/providers/objectstorage/public/logo.svg +++ b/frontend/providers/objectstorage/public/logo.svg @@ -1,5 +1,9 @@ - - - - + + + + + + + + diff --git a/frontend/providers/template/public/logo-colorful.svg b/frontend/providers/template/public/logo-colorful.svg deleted file mode 100644 index d137fa55a25..00000000000 --- a/frontend/providers/template/public/logo-colorful.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/frontend/providers/template/public/logo.svg b/frontend/providers/template/public/logo.svg index 3ca255f8f62..d137fa55a25 100644 --- a/frontend/providers/template/public/logo.svg +++ b/frontend/providers/template/public/logo.svg @@ -1,3 +1,9 @@ - - + + + + + + + + diff --git a/frontend/providers/terminal/public/logo-colorful.svg b/frontend/providers/terminal/public/logo-colorful.svg deleted file mode 100644 index 578ada6905a..00000000000 --- a/frontend/providers/terminal/public/logo-colorful.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/frontend/providers/terminal/public/logo.svg b/frontend/providers/terminal/public/logo.svg index 1a482cb713a..578ada6905a 100644 --- a/frontend/providers/terminal/public/logo.svg +++ b/frontend/providers/terminal/public/logo.svg @@ -1,3 +1,9 @@ - - - \ No newline at end of file + + + + + + + + +