Skip to content

Commit

Permalink
fix:launchpad resource update in checkPermission API (#5077)
Browse files Browse the repository at this point in the history
  • Loading branch information
zjy365 authored Sep 13, 2024
1 parent 79b0cae commit d36fa02
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion frontend/providers/applaunchpad/src/api/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export const getUserAccount = () => GET<AccountCRD>('/api/guide/getAccount');

export const getPriceBonus = () => GET('/api/guide/getBonus');

export const checkPermission = (payload: { appName: string; resourceType: 'deploy' | 'sts' }) =>
export const checkPermission = (payload: { appName: string }) =>
GET('/api/platform/checkPermission', payload);
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ import * as k8s from '@kubernetes/client-node';

export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
const { appName, resourceType } = req.query as {
appName: string;
resourceType: 'deploy' | 'sts';
};

if (!appName || !resourceType) {
throw new Error('appName or resourceType is empty');
}
const { appName } = req.query as { appName: string };
if (!appName) throw new Error('appName is empty');

const { k8sApp, namespace } = await getK8s({
kubeconfig: await authSession(req.headers)
Expand All @@ -32,7 +26,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
headers: { 'Content-type': k8s.PatchUtils.PATCH_FORMAT_STRATEGIC_MERGE_PATCH }
};

if (resourceType === 'deploy') {
let deploymentUpdated = false;

try {
await k8sApp.patchNamespacedDeployment(
appName,
namespace,
Expand All @@ -44,7 +40,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
undefined,
options
);
} else if (resourceType === 'sts') {
deploymentUpdated = true;
} catch (deployErr: any) {
if (deployErr?.response?.statusCode !== 404) {
throw deployErr;
}
}

if (!deploymentUpdated) {
await k8sApp.patchNamespacedStatefulSet(
appName,
namespace,
Expand All @@ -58,22 +61,19 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
);
}

jsonRes(res, {
code: 200,
data: 'success'
});
jsonRes(res, { code: 200, data: 'success' });
} catch (err: any) {
if (err?.body?.code === 403 && err?.body?.message.includes('40001')) {
return jsonRes(res, {
code: 200,
data: 'insufficient_funds',
message: err?.body?.message
message: err.body.message
});
}

jsonRes(res, {
code: 500,
error: err?.body
error: err?.body || err?.message
});
}
}
5 changes: 2 additions & 3 deletions frontend/providers/applaunchpad/src/pages/app/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ const EditApp = ({ appName, tabType }: { appName?: string; tabType: string }) =>
if (appName) {
try {
const result = await checkPermission({
appName: data.appName,
resourceType: !!data.storeList?.length ? 'sts' : 'deploy'
appName: data.appName
});
if (result === 'insufficient_funds') {
return toast({
Expand All @@ -370,7 +369,7 @@ const EditApp = ({ appName, tabType }: { appName?: string; tabType: string }) =>
} catch (error: any) {
return toast({
status: 'warning',
title: error
title: error?.message || 'Check Error'
});
}
}
Expand Down

0 comments on commit d36fa02

Please sign in to comment.