1
1
const { config } = require ( "../config" ) ;
2
2
const k8s = require ( "@kubernetes/client-node" ) ;
3
3
4
+ const delay = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
5
+
4
6
// Define the deployment name and namespace
5
7
const deploymentName = config . deploymentName ;
6
8
const namespace = config . namespace ;
7
9
8
10
const kc = new k8s . KubeConfig ( ) ;
9
11
kc . loadFromDefault ( ) ;
10
- const k8sApi = kc . makeApiClient ( k8s . AppsV1Api ) ;
12
+ const appsK8sApi = kc . makeApiClient ( k8s . AppsV1Api ) ;
13
+ const coreK8sApi = kc . makeApiClient ( k8s . CoreV1Api ) ;
11
14
12
15
exports . start = async ( interaction ) => {
13
16
await interaction . reply ( "Starting server..." ) ;
14
17
console . log ( "Starting server..." ) ;
15
18
16
19
// find the particular deployment
17
- const res = await k8sApi . readNamespacedDeployment ( deploymentName , namespace ) ;
20
+ const res = await appsK8sApi . readNamespacedDeployment (
21
+ deploymentName ,
22
+ namespace
23
+ ) ;
18
24
let deployment = res . body ;
19
25
20
26
// edit
21
27
deployment . spec . replicas = 1 ;
22
28
23
29
// 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
+ } ) ;
29
100
30
101
await interaction . followUp ( "Server is running!" ) ;
31
102
} ;
@@ -35,29 +106,34 @@ exports.stop = async (interaction) => {
35
106
console . log ( "Stopping server..." ) ;
36
107
37
108
// find the particular deployment
38
- const res = await k8sApi . readNamespacedDeployment ( deploymentName , namespace ) ;
109
+ const res = await appsK8sApi . readNamespacedDeployment (
110
+ deploymentName ,
111
+ namespace
112
+ ) ;
39
113
let deployment = res . body ;
40
114
41
115
// edit
42
116
deployment . spec . replicas = 0 ;
43
117
44
118
// replace
45
- await k8sApi . replaceNamespacedDeployment (
119
+ await appsK8sApi . replaceNamespacedDeployment (
46
120
deploymentName ,
47
121
namespace ,
48
122
deployment
49
123
) ;
50
124
51
125
await interaction . followUp ( "Server is stopped!" ) ;
52
-
53
126
} ;
54
127
55
128
exports . status = async ( interaction ) => {
56
129
await interaction . reply ( "Getting server status..." ) ;
57
130
console . log ( "Getting server status..." ) ;
58
131
59
132
// find the particular deployment
60
- const res = await k8sApi . readNamespacedDeployment ( deploymentName , namespace ) ;
133
+ const res = await appsK8sApi . readNamespacedDeployment (
134
+ deploymentName ,
135
+ namespace
136
+ ) ;
61
137
62
138
if ( res . body . spec . replicas == 0 ) {
63
139
await interaction . followUp ( "Server is stopped!" ) ;
0 commit comments