Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
Signed-off-by: jingyang <3161362058@qq.com>
  • Loading branch information
zjy365 committed Nov 21, 2023
1 parent 19a0f16 commit ccaffd9
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 13 deletions.
101 changes: 101 additions & 0 deletions frontend/providers/applaunchpad/src/pages/api/v1alpha/updateReplica.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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 { pauseKey } from '@/constants/app';

type UpdateReplicaParams = {
appName: string;
replica: string;
};
export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
const { appName, replica } = req.body as UpdateReplicaParams;
console.log(appName, replica);

if (!appName) {
throw new Error('appName is empty');
}

let result;

if (Number(replica) === 0) {
await PauseApp({ appName, replica, req });
result = 'Application suspended successfully';
} else {
result = 'test';
}

jsonRes(res, { data: result });
} catch (err: any) {
jsonRes(res, {
code: 500,
error: err
});
}
}

export async function PauseApp({
appName,
replica,
req
}: UpdateReplicaParams & { req: NextApiRequest }) {
const { apiClient, k8sAutoscaling, getDeployApp, namespace } = await getK8s({
kubeconfig: await authSession(req.headers)
});

const app = await getDeployApp(appName);
if (!app.metadata?.name || !app?.metadata?.annotations || !app.spec) {
throw new Error('app data error');
}

// store restart data
const restartAnnotations: Record<string, string> = {
target: '',
value: ''
};

const requestQueue: Promise<any>[] = [];

// check whether there are hpa
try {
const { body: hpa } = await k8sAutoscaling.readNamespacedHorizontalPodAutoscaler(
appName,
namespace
);
restartAnnotations.target = hpa?.spec?.metrics?.[0]?.resource?.name || 'cpu';
restartAnnotations.value = `${
hpa?.spec?.metrics?.[0]?.resource?.target?.averageUtilization || 50
}`;
requestQueue.push(k8sAutoscaling.deleteNamespacedHorizontalPodAutoscaler(appName, namespace)); // delete HorizontalPodAutoscaler
} catch (error: any) {
if (error?.statusCode !== 404) {
return Promise.reject('无法读取到hpa');
}
}

// replace source file
app.metadata.annotations[pauseKey] = JSON.stringify(restartAnnotations);
app.spec.replicas = 0;

requestQueue.push(apiClient.replace(app));

return await Promise.all(requestQueue);
}

export async function StartApp({
appName,
replica,
req
}: UpdateReplicaParams & { req: NextApiRequest }) {
const { apiClient, getDeployApp, applyYamlList } = await getK8s({
kubeconfig: await authSession(req.headers)
});

const app = await getDeployApp(appName);

if (!app.metadata?.name || !app?.metadata?.annotations || !app.spec) {
throw new Error('app data error');
}
}
4 changes: 2 additions & 2 deletions frontend/providers/dbprovider/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"Advanced Configuration": "Advanced Configuration",
"Option": "Option",
"Internet Migration": "Internet Migration",
"Dump Import": "Dump Import",
"File Migration": "File Migration",
"Source Database": "Source Database",
"Database Host": "Database Host",
"DB Name": "DataBase Name",
Expand Down Expand Up @@ -184,4 +184,4 @@
"Database UserName Empty": "Database UserName Empty",
"Database Password Empty": "Database Password Empty",
"Migration Failed": "Migration Failed"
}
}
4 changes: 2 additions & 2 deletions frontend/providers/dbprovider/public/locales/zh/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"Advanced Configuration": "高级配置",
"Option": "选填",
"Internet Migration": "公网迁移",
"Dump Import": "Dump 导入",
"File Migration": "文件迁移",
"Source Database": "源数据库",
"Database Host": "数据库 Host",
"DB Name": "数据库名字",
Expand Down Expand Up @@ -224,4 +224,4 @@
"Database UserName Empty": "缺少数据库用户名",
"Database Password Empty": "缺少数据库密码",
"Migration Failed": "迁移失败"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const AppBaseInfo = ({ db = defaultDBDetail }: { db: DBDetailType }) => {
);
}, [db.dbType]);

const { data: systemEnvs } = useQuery(['getDBSecret'], () => getAppEnv());
const { data: systemEnvs } = useQuery(['getSystemEnvs'], () => getAppEnv());

const { data: secret } = useQuery(
['getDBSecret', db.dbName],
Expand Down Expand Up @@ -378,12 +378,20 @@ const AppBaseInfo = ({ db = defaultDBDetail }: { db: DBDetailType }) => {
{t('Billing Standards')}
</Text>
<Center mt="16px" color={'#24282C'} fontSize={'24px'} fontWeight={600}>
0.05
0.5
<MyIcon ml="8px" name="currency" w="32px" h="32px"></MyIcon>/ {t('Hour')}
</Center>
<Center p="8px" mt="20px" bg="#EBF7FD" color={'#1D8CDC'}>
<Center
p="8px"
mt="20px"
bg="#EBF7FD"
color={'#1D8CDC'}
cursor={'pointer'}
w="100%"
onClick={() => window.open('https://forum.laf.run/d/1092')}
>
<MyIcon name="infoCircle" w="16px" fill={'#48A7EC'}></MyIcon>
防止用户删除数据库不完全(service没删)
免费方案
</Center>
<Button mt="32px" variant={'primary'} onClick={openNetWorkService}>
{t('Turn On')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ ${t('common.Surplus')}: ${`${item.limit - item.used} ${sourceMap[item.type]?.uni
}, [userQuota, t]);

return userQuota.length === 0 ? null : (
<Box h="50%" borderBottom={'1px solid #EAEBF0'} px="28px" pt="36px">
<Box
h="50%"
// borderBottom={'1px solid #EAEBF0'}
px="28px"
pt="36px"
>
<Text color={'#485058'} fontWeight={500} fontSize={'14px'}>
{t('app.Resource Quota')}
</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default function DumpImport({ db }: { db?: DBDetailType }) {
<Flex borderRadius={'4px'} border={'1px solid #EAEBF0'} h="100%">
<Box flex={'0 1 256px'} borderRight={'1px solid #EAEBF0'}>
<QuotaBox />
{db && (
{/* {db && (
<PriceBox
components={[
{
Expand All @@ -211,7 +211,7 @@ export default function DumpImport({ db }: { db?: DBDetailType }) {
: [])
]}
/>
)}
)} */}
</Box>
<Box flex={1} pt="35px" px="68px">
<Text color={'#24282C'} fontSize={'16px'} fontWeight={500}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const AppDetail = ({
...(MigrateSupported
? [
{ label: 'Internet Migration', value: TabEnum.InternetMigration },
{ label: 'Dump Import', value: TabEnum.DumpImport }
{ label: 'File Migration', value: TabEnum.DumpImport }
]
: [])
];
Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/dbprovider/src/utils/json2Yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ export const json2DumpCR = async (data: DumpForm) => {
},
{
name: 'COLLECTION_NAME',
value: ''
value: data.collectionName
},
{ name: 'TABLES_NAME', value: '' }
]
Expand Down

0 comments on commit ccaffd9

Please sign in to comment.