Skip to content

Commit 756290d

Browse files
authored
feat: added log parsing to send join code as reply (#19)
1 parent bbf0f2f commit 756290d

File tree

1 file changed

+87
-11
lines changed

1 file changed

+87
-11
lines changed

utils/utils.js

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,102 @@
11
const { config } = require("../config");
22
const k8s = require("@kubernetes/client-node");
33

4+
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
5+
46
// Define the deployment name and namespace
57
const deploymentName = config.deploymentName;
68
const namespace = config.namespace;
79

810
const kc = new k8s.KubeConfig();
911
kc.loadFromDefault();
10-
const k8sApi = kc.makeApiClient(k8s.AppsV1Api);
12+
const appsK8sApi = kc.makeApiClient(k8s.AppsV1Api);
13+
const coreK8sApi = kc.makeApiClient(k8s.CoreV1Api);
1114

1215
exports.start = async (interaction) => {
1316
await interaction.reply("Starting server...");
1417
console.log("Starting server...");
1518

1619
// find the particular deployment
17-
const res = await k8sApi.readNamespacedDeployment(deploymentName, namespace);
20+
const res = await appsK8sApi.readNamespacedDeployment(
21+
deploymentName,
22+
namespace
23+
);
1824
let deployment = res.body;
1925

2026
// edit
2127
deployment.spec.replicas = 1;
2228

2329
// replace
24-
await k8sApi.replaceNamespacedDeployment(
25-
deploymentName,
26-
namespace,
27-
deployment
28-
);
30+
await appsK8sApi
31+
.replaceNamespacedDeployment(deploymentName, namespace, deployment)
32+
.then(async function (response) {
33+
// wait after starting the container because it may have some old logs
34+
// and the old join code may be returned, so we wait a bit for the new container
35+
// to log some new lines and then start the check loop
36+
await delay(config.joinCodeLoopTimeoutMillis);
37+
38+
let podObj,
39+
podContainer,
40+
joinCode = "";
41+
42+
await coreK8sApi.listNamespacedPod(namespace).then((res) => {
43+
res.body.items.forEach((pod) => {
44+
if (
45+
pod.metadata.labels["app.kubernetes.io/name"] ==
46+
config.deploymentName
47+
) {
48+
podObj = pod;
49+
pod.spec.containers.forEach((container) => {
50+
podContainer = container;
51+
});
52+
}
53+
});
54+
});
55+
56+
console.log("pod: ", podObj.metadata.name);
57+
console.log("container:", podContainer.name);
58+
59+
for (let i = 0; i < config.joinCodeLoopCount; i++) {
60+
await coreK8sApi
61+
.readNamespacedPodLog(
62+
podObj.metadata.name,
63+
namespace,
64+
podContainer.name,
65+
false,
66+
undefined,
67+
undefined,
68+
true,
69+
false,
70+
undefined,
71+
10
72+
)
73+
.then((log) => {
74+
let data = log.body;
75+
76+
let index = data.indexOf(
77+
`Session "${config.serverName}" with join code`
78+
);
79+
if (index !== -1) {
80+
// Get the next word after the string
81+
let words = data.slice(index).split(" ");
82+
joinCode = words[5]; // The next word is at index 5
83+
}
84+
85+
console.log("Join code not present yet, retryin...");
86+
});
87+
88+
if (joinCode != "") {
89+
await interaction.followUp(
90+
`Server started successfully! Join code is ${joinCode}`
91+
);
92+
console.log("Server started successfully! Join code is", joinCode);
93+
94+
break;
95+
}
96+
97+
await delay(config.joinCodeLoopTimeoutMillis);
98+
}
99+
});
29100

30101
await interaction.followUp("Server is running!");
31102
};
@@ -35,29 +106,34 @@ exports.stop = async (interaction) => {
35106
console.log("Stopping server...");
36107

37108
// find the particular deployment
38-
const res = await k8sApi.readNamespacedDeployment(deploymentName, namespace);
109+
const res = await appsK8sApi.readNamespacedDeployment(
110+
deploymentName,
111+
namespace
112+
);
39113
let deployment = res.body;
40114

41115
// edit
42116
deployment.spec.replicas = 0;
43117

44118
// replace
45-
await k8sApi.replaceNamespacedDeployment(
119+
await appsK8sApi.replaceNamespacedDeployment(
46120
deploymentName,
47121
namespace,
48122
deployment
49123
);
50124

51125
await interaction.followUp("Server is stopped!");
52-
53126
};
54127

55128
exports.status = async (interaction) => {
56129
await interaction.reply("Getting server status...");
57130
console.log("Getting server status...");
58131

59132
// find the particular deployment
60-
const res = await k8sApi.readNamespacedDeployment(deploymentName, namespace);
133+
const res = await appsK8sApi.readNamespacedDeployment(
134+
deploymentName,
135+
namespace
136+
);
61137

62138
if (res.body.spec.replicas == 0) {
63139
await interaction.followUp("Server is stopped!");

0 commit comments

Comments
 (0)