-
Notifications
You must be signed in to change notification settings - Fork 2k
/
getKindTemplate.ts
44 lines (40 loc) · 1.34 KB
/
getKindTemplate.ts
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
import { authSession } from '@/services/backend/auth';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
import { ApiResp } from '@/services/kubernet';
import { TemplateType } from '@/types/app';
import fs from 'fs';
import yaml from 'js-yaml';
import type { NextApiRequest, NextApiResponse } from 'next';
import path from 'path';
export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
const { templateName } = req.query as { templateName: string };
let user_namespace = '';
try {
const { namespace } = await getK8s({
kubeconfig: await authSession(req.headers)
});
user_namespace = namespace;
} catch (error) {
console.log(error, 'errpr-');
}
const originalPath = process.cwd();
const targetPath = path.resolve(originalPath, 'FastDeployTemplates', 'template');
const yamlString = fs.readFileSync(`${targetPath}/${templateName}.yaml`, 'utf-8');
const yamlData = yaml.loadAll(yamlString);
const templateYaml: TemplateType = yamlData.find(
(item: any) => item.kind === 'Template'
) as TemplateType;
jsonRes(res, {
code: 200,
data: templateYaml
});
} catch (err: any) {
console.log(err);
jsonRes(res, {
code: 500,
error: err
});
}
}