1- import { NextRequest } from 'next/server'
1+ import { NextRequest } from 'next/server' ;
22
3- import { jsonRes } from '@/services/backend/response'
4- import { authSession } from '@/services/backend/auth'
5- import { getK8s } from '@/services/backend/kubernetes'
3+ import { jsonRes } from '@/services/backend/response' ;
4+ import { authSession } from '@/services/backend/auth' ;
5+ import { getK8s } from '@/services/backend/kubernetes' ;
6+ import { devboxKey } from '@/constants/devbox' ;
67
7- export const dynamic = 'force-dynamic'
8+ export const dynamic = 'force-dynamic' ;
89
910export async function POST ( req : NextRequest ) {
1011 try {
11- const { devboxName } = ( await req . json ( ) ) as { devboxName : string }
12- const headerList = req . headers
12+ const { devboxName } = ( await req . json ( ) ) as { devboxName : string } ;
13+ const headerList = req . headers ;
1314
14- const { k8sCustomObjects, namespace, k8sCore } = await getK8s ( {
15+ const { k8sCustomObjects, namespace, k8sCore, k8sNetworkingApp } = await getK8s ( {
1516 kubeconfig : await authSession ( headerList )
16- } )
17+ } ) ;
1718
1819 // restart = stopped + running
1920
@@ -33,12 +34,12 @@ export async function POST(req: NextRequest) {
3334 'Content-Type' : 'application/merge-patch+json'
3435 }
3536 }
36- )
37+ ) ;
3738
3839 // 2.get devbox pod and ensure the devbox pod is deleted,when the devbox pod is deleted,the devbox will be restarted
39- let pods
40- const maxRetries = 10
41- let retries = 0
40+ let pods ;
41+ const maxRetries = 10 ;
42+ let retries = 0 ;
4243
4344 do {
4445 const {
@@ -50,22 +51,78 @@ export async function POST(req: NextRequest) {
5051 undefined ,
5152 undefined ,
5253 `app.kubernetes.io/name=${ devboxName } `
53- )
54- pods = items
54+ ) ;
55+ pods = items ;
5556
5657 if ( pods . length > 0 ) {
57- await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) )
58+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ;
5859 }
5960
60- retries ++
61- } while ( pods . length > 0 && retries < maxRetries )
61+ retries ++ ;
62+ } while ( pods . length > 0 && retries < maxRetries ) ;
6263
6364 if ( retries === maxRetries ) {
64- throw new Error ( 'Max retries reached while waiting for devbox pod to be deleted' )
65+ throw new Error ( 'Max retries reached while waiting for devbox pod to be deleted' ) ;
6566 }
66- console . log ( 'devbox pod is deleted' )
67+ console . log ( 'devbox pod is deleted' ) ;
6768
6869 // 3. running
70+ // get ingress and modify ingress annotations
71+ const ingressesResponse = await k8sNetworkingApp . listNamespacedIngress (
72+ namespace ,
73+ undefined ,
74+ undefined ,
75+ undefined ,
76+ undefined ,
77+ `${ devboxKey } =${ devboxName } `
78+ ) ;
79+ const ingresses : any = ( ingressesResponse . body as { items : any [ ] } ) . items ;
80+
81+ ingresses . forEach ( async ( ingress : any ) => {
82+ const annotationsIngressClass =
83+ ingress . metadata ?. annotations ?. [ 'kubernetes.io/ingress.class' ] ;
84+ const specIngressClass = ingress . spec ?. ingressClassName ;
85+
86+ if (
87+ ( annotationsIngressClass && annotationsIngressClass === 'pause' ) ||
88+ ( specIngressClass && specIngressClass === 'pause' )
89+ ) {
90+ if ( annotationsIngressClass ) {
91+ await k8sNetworkingApp . patchNamespacedIngress (
92+ ingress . metadata . name ,
93+ namespace ,
94+ { metadata : { annotations : { 'kubernetes.io/ingress.class' : 'nginx' } } } ,
95+ undefined ,
96+ undefined ,
97+ undefined ,
98+ undefined ,
99+ undefined ,
100+ {
101+ headers : {
102+ 'Content-Type' : 'application/merge-patch+json'
103+ }
104+ }
105+ ) ;
106+ } else if ( specIngressClass ) {
107+ await k8sNetworkingApp . patchNamespacedIngress (
108+ ingress . metadata . name ,
109+ namespace ,
110+ { spec : { ingressClassName : 'nginx' } } ,
111+ undefined ,
112+ undefined ,
113+ undefined ,
114+ undefined ,
115+ undefined ,
116+ {
117+ headers : {
118+ 'Content-Type' : 'application/merge-patch+json'
119+ }
120+ }
121+ ) ;
122+ }
123+ }
124+ } ) ;
125+
69126 await k8sCustomObjects . patchNamespacedCustomObject (
70127 'devbox.sealos.io' ,
71128 'v1alpha1' ,
@@ -81,15 +138,15 @@ export async function POST(req: NextRequest) {
81138 'Content-Type' : 'application/merge-patch+json'
82139 }
83140 }
84- )
141+ ) ;
85142
86143 return jsonRes ( {
87144 data : 'success pause devbox'
88- } )
145+ } ) ;
89146 } catch ( err : any ) {
90147 return jsonRes ( {
91148 code : 500 ,
92149 error : err
93- } )
150+ } ) ;
94151 }
95152}
0 commit comments