From 5dcd1f9da18ca2d466eadfcf26b8759680fe9b23 Mon Sep 17 00:00:00 2001 From: Alex Lee <3076438032@qq.com> Date: Fri, 3 Jan 2025 18:01:31 +0800 Subject: [PATCH] feat: start and pause will modify ingress (#5321) * feat: start and pause will modify ingress * chore: ingress compatibility * fix: code back restartDevbox * chore: adjust api --- frontend/providers/devbox/.env.template | 2 +- .../devbox/app/api/pauseDevbox/route.ts | 58 ++++++++++++++++++- .../devbox/app/api/restartDevbox/route.ts | 1 + .../devbox/app/api/startDevbox/route.ts | 58 ++++++++++++++++++- 4 files changed, 116 insertions(+), 3 deletions(-) diff --git a/frontend/providers/devbox/.env.template b/frontend/providers/devbox/.env.template index 3dd1c90bc8a..156ba6ef066 100644 --- a/frontend/providers/devbox/.env.template +++ b/frontend/providers/devbox/.env.template @@ -8,4 +8,4 @@ NODE_TLS_REJECT_UNAUTHORIZED= ROOT_RUNTIME_NAMESPACE= DATABASE_URL= RETAG_SVC_URL= -PRIVACY_URL= \ No newline at end of file +PRIVACY_URL= diff --git a/frontend/providers/devbox/app/api/pauseDevbox/route.ts b/frontend/providers/devbox/app/api/pauseDevbox/route.ts index baf8134ac79..5c3055cdbf9 100644 --- a/frontend/providers/devbox/app/api/pauseDevbox/route.ts +++ b/frontend/providers/devbox/app/api/pauseDevbox/route.ts @@ -3,6 +3,7 @@ import { NextRequest } from 'next/server' 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' @@ -12,10 +13,65 @@ export async function POST(req: NextRequest) { const headerList = req.headers - const { k8sCustomObjects, namespace } = await getK8s({ + const { k8sCustomObjects, namespace, k8sNetworkingApp } = await getK8s({ kubeconfig: await authSession(headerList) }) + // 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 === 'nginx') || + (specIngressClass && specIngressClass === 'nginx') + ) { + if (annotationsIngressClass) { + await k8sNetworkingApp.patchNamespacedIngress( + ingress.metadata.name, + namespace, + { metadata: { annotations: { 'kubernetes.io/ingress.class': 'pause' } } }, + undefined, + undefined, + undefined, + undefined, + undefined, + { + headers: { + 'Content-Type': 'application/merge-patch+json' + } + } + ) + } else if (specIngressClass) { + await k8sNetworkingApp.patchNamespacedIngress( + ingress.metadata.name, + namespace, + { spec: { ingressClassName: 'pause' } }, + undefined, + undefined, + undefined, + undefined, + undefined, + { + headers: { + 'Content-Type': 'application/merge-patch+json' + } + } + ) + } + } + }) + await k8sCustomObjects.patchNamespacedCustomObject( 'devbox.sealos.io', 'v1alpha1', diff --git a/frontend/providers/devbox/app/api/restartDevbox/route.ts b/frontend/providers/devbox/app/api/restartDevbox/route.ts index 41da009504f..69d7ffdb5e2 100644 --- a/frontend/providers/devbox/app/api/restartDevbox/route.ts +++ b/frontend/providers/devbox/app/api/restartDevbox/route.ts @@ -34,6 +34,7 @@ export async function POST(req: NextRequest) { } } ) + // 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 diff --git a/frontend/providers/devbox/app/api/startDevbox/route.ts b/frontend/providers/devbox/app/api/startDevbox/route.ts index 636cfa0630d..8c433080887 100644 --- a/frontend/providers/devbox/app/api/startDevbox/route.ts +++ b/frontend/providers/devbox/app/api/startDevbox/route.ts @@ -3,6 +3,7 @@ import { NextRequest } from 'next/server' import { authSession } from '@/services/backend/auth' import { getK8s } from '@/services/backend/kubernetes' import { jsonRes } from '@/services/backend/response' +import { devboxKey } from '@/constants/devbox' export const dynamic = 'force-dynamic' @@ -12,10 +13,65 @@ export async function POST(req: NextRequest) { const headerList = req.headers - const { k8sCustomObjects, namespace } = await getK8s({ + const { k8sCustomObjects, namespace, k8sNetworkingApp } = await getK8s({ kubeconfig: await authSession(headerList) }) + // 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',