1
- import { NextRequest } from 'next/server'
1
+ import { NextRequest } from 'next/server' ;
2
2
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' ;
6
7
7
- export const dynamic = 'force-dynamic'
8
+ export const dynamic = 'force-dynamic' ;
8
9
9
10
export async function POST ( req : NextRequest ) {
10
11
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 ;
13
14
14
- const { k8sCustomObjects, namespace, k8sCore } = await getK8s ( {
15
+ const { k8sCustomObjects, namespace, k8sCore, k8sNetworkingApp } = await getK8s ( {
15
16
kubeconfig : await authSession ( headerList )
16
- } )
17
+ } ) ;
17
18
18
19
// restart = stopped + running
19
20
@@ -33,12 +34,12 @@ export async function POST(req: NextRequest) {
33
34
'Content-Type' : 'application/merge-patch+json'
34
35
}
35
36
}
36
- )
37
+ ) ;
37
38
38
39
// 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 ;
42
43
43
44
do {
44
45
const {
@@ -50,22 +51,78 @@ export async function POST(req: NextRequest) {
50
51
undefined ,
51
52
undefined ,
52
53
`app.kubernetes.io/name=${ devboxName } `
53
- )
54
- pods = items
54
+ ) ;
55
+ pods = items ;
55
56
56
57
if ( pods . length > 0 ) {
57
- await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) )
58
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ;
58
59
}
59
60
60
- retries ++
61
- } while ( pods . length > 0 && retries < maxRetries )
61
+ retries ++ ;
62
+ } while ( pods . length > 0 && retries < maxRetries ) ;
62
63
63
64
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' ) ;
65
66
}
66
- console . log ( 'devbox pod is deleted' )
67
+ console . log ( 'devbox pod is deleted' ) ;
67
68
68
69
// 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
+
69
126
await k8sCustomObjects . patchNamespacedCustomObject (
70
127
'devbox.sealos.io' ,
71
128
'v1alpha1' ,
@@ -81,15 +138,15 @@ export async function POST(req: NextRequest) {
81
138
'Content-Type' : 'application/merge-patch+json'
82
139
}
83
140
}
84
- )
141
+ ) ;
85
142
86
143
return jsonRes ( {
87
144
data : 'success pause devbox'
88
- } )
145
+ } ) ;
89
146
} catch ( err : any ) {
90
147
return jsonRes ( {
91
148
code : 500 ,
92
149
error : err
93
- } )
150
+ } ) ;
94
151
}
95
152
}
0 commit comments