Skip to content

Commit

Permalink
fix: bug (#5352)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlhiter authored Jan 20, 2025
1 parent f0bfe7e commit c376548
Showing 1 changed file with 80 additions and 23 deletions.
103 changes: 80 additions & 23 deletions frontend/providers/devbox/app/api/restartDevbox/route.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 {
Expand All @@ -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',
Expand All @@ -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
})
});
}
}

0 comments on commit c376548

Please sign in to comment.