-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKubeServiceUpdated.js
164 lines (143 loc) · 4.33 KB
/
KubeServiceUpdated.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const express = require("express");
const { exec, execSync } = require("child_process");
const fs = require("fs");
const k8s = require("@kubernetes/client-node");
const app = express();
app.use(express.json());
const deploymentMap = {};
const kc = new k8s.KubeConfig();
const kubeconfigPath = "/etc/rancher/k3s/k3s.yaml";
kc.loadFromFile(kubeconfigPath);
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const k8sAppsV1Api = kc.makeApiClient(k8s.AppsV1Api);
app.post("/deploy", (req, res) => {
const imageUrl = req.body.src;
const taskID = 1;
if (!imageUrl) {
return res.status(400).send({ error: "No image URL provided" });
}
const deploymentName = `dynamic-deployment-${Math.random()
.toString(36)
.substring(7)}`;
const filenameBase = deploymentName.replace(/[^a-zA-Z0-9-]/g, "");
deploymentMap[taskID] = deploymentName;
const serviceYaml = `
apiVersion: v1
kind: Service
metadata:
name: ${deploymentName}-service
spec:
type: NodePort
selector:
app: ${deploymentName}
ports:
- protocol: TCP
port: 8080
targetPort: 8080
`;
const deploymentYaml = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${deploymentName}
spec:
replicas: 1
selector:
matchLabels:
app: ${deploymentName}
template:
metadata:
labels:
app: ${deploymentName}
spec:
containers:
- name: ${deploymentName}-container
image: ${imageUrl}
`;
fs.writeFileSync(`${filenameBase}.yaml`, deploymentYaml);
fs.writeFileSync(`${filenameBase}-service.yaml`, serviceYaml);
exec(
`kubectl apply -f ${filenameBase}.yaml && kubectl apply -f ${filenameBase}-service.yaml`,
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return res
.status(500)
.send({ error: "Deployment or Service creation failed" });
//.send(`Deployment or Service creation failed: ${error}`);
}
let nodePort;
try {
const getServiceCommand = `kubectl get svc ${deploymentName}-service -o=jsonpath='{.spec.ports[0].nodePort}'`;
nodePort = execSync(getServiceCommand).toString().trim();
} catch (error) {
console.error("Error fetching NodePort:", error);
return res.status(500).send({ error: "Failed to fetch NodePort" });
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
res.send({
message: `Deployment ${deploymentName} and Service created`,
nodePort: nodePort,
});
}
);
});
app.get("/cluster-status", (req, res) => {
console.log("check k3s status: ", req.params);
res.json({
deployment_status: true,
});
});
app.get("/deployment-status/:taskId", async (req, res) => {
const { taskId } = req.params;
console.log("taskId1: ", taskId);
if (!taskId) {
return res.status(400).send({ error: "No task ID provided" });
}
try {
const deploymentName = deploymentMap[taskId];
if (!deploymentName) {
return res
.status(404)
.send({ error: "Deployment not found for provided task ID" });
}
const deployment = await k8sAppsV1Api.readNamespacedDeployment(
deploymentName,
"default"
);
const status = deployment.body.status;
res.json({ conditions: status.conditions });
} catch (error) {
console.error("Error fetching deployment status:", error);
res.status(500).send({ error: "Failed to fetch deployment status" });
}
});
app.get("/logs/:taskId", async (req, res) => {
const { taskId } = req.params;
if (!taskId) {
return res.status(400).send({ error: "No task ID provided" });
}
try {
const deploymentName = deploymentMap[taskId];
if (!deploymentName) {
return res
.status(404)
.send({ error: "Deployment not found for provided task ID" });
}
const command = `kubectl logs -l app=${deploymentName} --all-containers=true`;
const logStream = exec(command);
logStream.stdout.pipe(res);
logStream.on("error", (error) => {
console.error("Error streaming logs:", error);
res.status(500).send({ error: "Failed to stream logs" });
});
} catch (error) {
console.error("Error fetching deployment:", error);
res.status(500).send({ error: "Failed to fetch deployment" });
}
});
const port = 3000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});