Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(devbox): restart not adjust ingress bug #5352

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
});
}
}
Loading