-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: jingyang <3161362058@qq.com>
- Loading branch information
Showing
8 changed files
with
127 additions
and
13 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
frontend/providers/applaunchpad/src/pages/api/v1alpha/updateReplica.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters