diff --git a/frontend/providers/devbox/app/api/restartDevbox/route.ts b/frontend/providers/devbox/app/api/restartDevbox/route.ts index 69d7ffdb5e2..966177fd1c2 100644 --- a/frontend/providers/devbox/app/api/restartDevbox/route.ts +++ b/frontend/providers/devbox/app/api/restartDevbox/route.ts @@ -1,19 +1,20 @@ -import { NextRequest } from 'next/server' +import { NextRequest } from 'next/server'; -import { jsonRes } from '@/services/backend/response' -import { authSession } from '@/services/backend/auth' -import { getK8s } from '@/services/backend/kubernetes' +import { jsonRes } from '@/services/backend/response'; +import { authSession } from '@/services/backend/auth'; +import { getK8s } from '@/services/backend/kubernetes'; +import { devboxKey } from '@/constants/devbox'; -export const dynamic = 'force-dynamic' +export const dynamic = 'force-dynamic'; export async function POST(req: NextRequest) { try { - const { devboxName } = (await req.json()) as { devboxName: string } - const headerList = req.headers + const { devboxName } = (await req.json()) as { devboxName: string }; + const headerList = req.headers; - const { k8sCustomObjects, namespace, k8sCore } = await getK8s({ + const { k8sCustomObjects, namespace, k8sCore, k8sNetworkingApp } = await getK8s({ kubeconfig: await authSession(headerList) - }) + }); // restart = stopped + running @@ -33,12 +34,12 @@ export async function POST(req: NextRequest) { 'Content-Type': 'application/merge-patch+json' } } - ) + ); // 2.get devbox pod and ensure the devbox pod is deleted,when the devbox pod is deleted,the devbox will be restarted - let pods - const maxRetries = 10 - let retries = 0 + let pods; + const maxRetries = 10; + let retries = 0; do { const { @@ -50,22 +51,78 @@ export async function POST(req: NextRequest) { undefined, undefined, `app.kubernetes.io/name=${devboxName}` - ) - pods = items + ); + pods = items; if (pods.length > 0) { - await new Promise((resolve) => setTimeout(resolve, 3000)) + await new Promise((resolve) => setTimeout(resolve, 3000)); } - retries++ - } while (pods.length > 0 && retries < maxRetries) + retries++; + } while (pods.length > 0 && retries < maxRetries); if (retries === maxRetries) { - throw new Error('Max retries reached while waiting for devbox pod to be deleted') + throw new Error('Max retries reached while waiting for devbox pod to be deleted'); } - console.log('devbox pod is deleted') + console.log('devbox pod is deleted'); // 3. running + // get ingress and modify ingress annotations + const ingressesResponse = await k8sNetworkingApp.listNamespacedIngress( + namespace, + undefined, + undefined, + undefined, + undefined, + `${devboxKey}=${devboxName}` + ); + const ingresses: any = (ingressesResponse.body as { items: any[] }).items; + + ingresses.forEach(async (ingress: any) => { + const annotationsIngressClass = + ingress.metadata?.annotations?.['kubernetes.io/ingress.class']; + const specIngressClass = ingress.spec?.ingressClassName; + + if ( + (annotationsIngressClass && annotationsIngressClass === 'pause') || + (specIngressClass && specIngressClass === 'pause') + ) { + if (annotationsIngressClass) { + await k8sNetworkingApp.patchNamespacedIngress( + ingress.metadata.name, + namespace, + { metadata: { annotations: { 'kubernetes.io/ingress.class': 'nginx' } } }, + undefined, + undefined, + undefined, + undefined, + undefined, + { + headers: { + 'Content-Type': 'application/merge-patch+json' + } + } + ); + } else if (specIngressClass) { + await k8sNetworkingApp.patchNamespacedIngress( + ingress.metadata.name, + namespace, + { spec: { ingressClassName: 'nginx' } }, + undefined, + undefined, + undefined, + undefined, + undefined, + { + headers: { + 'Content-Type': 'application/merge-patch+json' + } + } + ); + } + } + }); + await k8sCustomObjects.patchNamespacedCustomObject( 'devbox.sealos.io', 'v1alpha1', @@ -81,15 +138,15 @@ export async function POST(req: NextRequest) { 'Content-Type': 'application/merge-patch+json' } } - ) + ); return jsonRes({ data: 'success pause devbox' - }) + }); } catch (err: any) { return jsonRes({ code: 500, error: err - }) + }); } }