-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathroute.ts
More file actions
136 lines (125 loc) · 4.3 KB
/
Copy pathroute.ts
File metadata and controls
136 lines (125 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { devboxKey, ingressProtocolKey, publicDomainKey } from '@/constants/devbox';
import { MockDevboxDetail } from '@/constants/mock';
import { authSession } from '@/services/backend/auth';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
import { devboxDB } from '@/services/db/init';
import { ProtocolType } from '@/types/devbox';
import { PortInfos } from '@/types/ingress';
import { KBDevboxTypeV2 } from '@/types/k8s';
import { adaptDevboxDetailV2 } from '@/utils/adapt';
import { NextRequest } from 'next/server';
export const dynamic = 'force-dynamic';
export async function GET(req: NextRequest) {
try {
const headerList = req.headers;
const { searchParams } = req.nextUrl;
const devboxName = searchParams.get('devboxName') as string;
const mock = searchParams.get('mock') === 'true';
if (mock) {
return jsonRes({
data: MockDevboxDetail
});
}
if (!devboxName) {
return jsonRes({
code: 400,
error: 'devboxName is required'
});
}
const { k8sCustomObjects, namespace, k8sCore, k8sNetworkingApp } = await getK8s({
kubeconfig: await authSession(headerList)
});
const { body: devboxBody } = (await k8sCustomObjects.getNamespacedCustomObject(
'devbox.sealos.io',
'v1alpha2',
namespace,
'devboxes',
devboxName
)) as { body: KBDevboxTypeV2 };
const template = await devboxDB.template.findUnique({
where: {
uid: devboxBody.spec.templateID
},
select: {
templateRepository: {
select: {
uid: true,
iconId: true,
icon: true,
name: true,
kind: true,
description: true
}
},
uid: true,
image: true,
name: true
}
});
if (!template) {
return jsonRes({
code: 500,
error: 'template not found'
});
}
const label = `${devboxKey}=${devboxName}`;
// get ingresses, service, configmaps, and pvcs
const [ingressesResponse, serviceResponse, configMapsResponse, pvcsResponse] =
await Promise.all([
k8sNetworkingApp
.listNamespacedIngress(namespace, undefined, undefined, undefined, undefined, label)
.catch(() => null),
k8sCore.readNamespacedService(devboxName, namespace, undefined).catch(() => null),
k8sCore
.listNamespacedConfigMap(namespace, undefined, undefined, undefined, undefined, label)
.catch(() => null),
k8sCore
.listNamespacedPersistentVolumeClaim(
namespace,
undefined,
undefined,
undefined,
undefined,
label
)
.catch(() => null)
]);
const ingresses = ingressesResponse?.body.items || [];
const service = serviceResponse?.body;
const configMaps = configMapsResponse?.body.items || [];
const pvcs = pvcsResponse?.body.items || [];
const ingressList = ingresses.map((item) => {
const defaultDomain = item.metadata?.labels?.[publicDomainKey];
const tlsHost = item.spec?.tls?.[0]?.hosts?.[0];
return {
networkName: item.metadata?.name,
port: item.spec?.rules?.[0]?.http?.paths?.[0]?.backend?.service?.port?.number,
protocol: item.metadata?.annotations?.[ingressProtocolKey],
openPublicDomain: !!defaultDomain,
publicDomain: defaultDomain === tlsHost ? tlsHost : defaultDomain,
customDomain: defaultDomain === tlsHost ? '' : tlsHost
};
});
const portInfos: PortInfos =
service?.spec?.ports?.map((svcport) => {
const ingressInfo = ingressList.find((ingress) => ingress.port === svcport.port);
return {
portName: svcport.name!,
port: svcport.port,
protocol: ingressInfo?.protocol as ProtocolType,
networkName: ingressInfo?.networkName,
openPublicDomain: !!ingressInfo?.openPublicDomain,
publicDomain: ingressInfo?.publicDomain,
customDomain: ingressInfo?.customDomain
};
}) || [];
const data = adaptDevboxDetailV2([devboxBody, portInfos, template, configMaps, pvcs]);
return jsonRes({ data });
} catch (err: any) {
return jsonRes({
code: 500,
error: err
});
}
}