-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.js
214 lines (211 loc) · 6.23 KB
/
utils.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* jshint esversion: 6 */
// check if podName is in pods array
module.exports = {
// Initial json of all the drones
// Used to reset unassigned drones later
getInitialPositions() {
var homePositions = []
for (let i = 0; i < MAX_DRONES; i++) {
var location, color;
var midPoint = Math.round(MAX_DRONES / 2) - 1;
if (i <= midPoint) { //first half of drones go in front
location = {
x: (MAX_X / 3) * (i + 1),
y: MAX_Y / 5,
z: 0
}
} else { //others go in the back
location = {
x: (MAX_X / 3) * (i - midPoint),
y: MAX_Y * 2 / 4,
z: 0
}
}
var color = white
homePositions.push({
droneId: i,
podName: null,
location,
color
});
}
return homePositions;
},
assignPodsToDrones(pods) {
console.log(">assignPodsToDrones");
// go through each running pod
for (var runningPod of pods.body.items) {
podName = runningPod.metadata.name;
if (this.indexOfPodinDrones(podName) == -1) { // new pod
const freeDroneIndex = this.getFreeDroneIndex();
if (freeDroneIndex > -1) {
this.assignDrone(freeDroneIndex, podName);
} else {
console.log("ERROR! NO MORE FREE DRONES FOUND!")
continue;
}
}
this.updateDroneStatus(runningPod);
}
console.log("<assignPodsToDrones");
},
assignDrone(freeDroneIndex, podName) {
console.log(`assignAndTakeOffDrone(${freeDroneIndex})`)
drones[freeDroneIndex].podName = podName;
drones[freeDroneIndex].location.z = 1;
},
updateDroneStatus(runningPod) {
console.log(`${runningPod.metadata.name} is ${runningPod.status.phase}`);
var indexOfPod = this.indexOfPodinDrones(podName)
if (runningPod.status.phase == 'Running') {
drones[indexOfPod].location.z = 1;
if (ENABLELIGHTBOXES) {
this.moveDroneToLightBox(indexOfPod, runningPod);
} else {
drones[indexOfPod].color = blue;
}
} else if (runningPod.status.phase == 'Pending') {
if (ENABLELIGHTBOXES) {
drones[indexOfPod].color = yellow;
drones[indexOfPod].location.y = MAX_Y / 2.0
drones[indexOfPod].location.x = Math.random() * MAX_X
} else {
drones[indexOfPod].color = blue;
}
drones[indexOfPod].location.z = 0.75;
} else if (runningPod.status.phase == 'Terminating') {
landAndResetDrone(indexOfPod);
}
},
moveDroneToLightBox(freeDroneIndex, runningPod) {
let podWorkerNode = runningPod.spec.nodeName;
if (!podWorkerNode) {
console.log(`${runningPod.metadata.name} has no worker node.`)
return;
}
let lightBox = this.getLightBoxForNode(podWorkerNode)
drones[freeDroneIndex].location.x = lightBox.location.x;
drones[freeDroneIndex].location.y = lightBox.location.y;
drones[freeDroneIndex].color = lightBox.color;
},
getLightBoxForNode(podWorkerNode) {
console.log("getLightBoxForNode: " + podWorkerNode);
for (lightBox of lightBoxes) {
if (lightBox.workerNode == podWorkerNode) {
return lightBox
}
}
},
getFreeDroneIndex() {
for (let j = 0; j < drones.length; j++) {
if (drones[j].podName == null) {
return j
}
}
return -1;
},
indexOfPodinDrones(podName) {
for (let j = 0; j < drones.length; j++) {
if (drones[j].podName === podName) {
return j
}
}
return -1
},
landAndResetDrone(resetDroneIndex) {
console.log(`>landAndResetDrone with index ${resetDroneIndex}`)
var initialPositions = this.getInitialPositions();
drones[resetDroneIndex] = initialPositions[drones[resetDroneIndex].droneId]
//move this drone to the back of the array so it doesn't get reassigned immediately
drones.push(drones.splice(resetDroneIndex, 1)[0]);
},
getRunningPods(pods) {
const runningPods = [];
for (let i = 0; i < pods.body.items.length; i++) {
const { phase } = pods.body.items[i].status;
if (phase === 'Running' || phase === 'ContainerCreating' || phase === 'Pending') {
runningPods.push(pods.body.items[i]);
}
}
return runningPods;
},
removeDeletedPods(pods) {
console.log(">removeDeletedPods")
const runningPods = this.getRunningPods(pods);
var runningPodNames = [];
for (runningPod of runningPods) {
runningPodNames.push(runningPod.metadata.name)
}
for (let j = 0; j < drones.length; j++) {
if (drones[j].podName != null && !runningPodNames.includes(drones[j].podName)) { // a drone is assigned to a pod that isn't running anymore
this.landAndResetDrone(j)
if (ENABLELIGHTBOXES) {
return; // For B scenarios, only land 1 node at a time to avoid collisions
}
}
}
console.log("<removeDeletedPods");
},
initKubeClient(Client, config) {
try {
client = new Client({
config: config.fromKubeconfig(),
version: '1.9',
});
} catch (err) {
try {
client = new Client({
config: config.getInCluster(),
});
client.loadSpec();
} catch (error) {
console.error('Can\'t connect to Kube API, did you set your $KUBECONFIG?');
process.kill();
}
}
return client;
},
// blue, green and purple
initLightsBoxes(numLightBoxes) {
console.log(`initAvailableLightsBoxes(${numLightBoxes})`);
const lights = [];
var color;
for (let i = 0; i < numLightBoxes; i++) {
switch (i) {
case 0:
color = red;
break;
case 1:
color = blue;
break;
case 2:
color = green;
break;
}
lights.push({
lightBoxesId: i,
location: {
x: (MAX_X / 4) * (i + 1),
y: MAX_Y * 4 / 5,
},
color
});
}
console.log(lights);
return lights;
},
assignWorkerNodesToLights() {
for (i in workernodes) {
lightBoxes[i].workerNode = workernodes[i].metadata.name;
}
console.log(lightBoxes)
},
};
getColorJSON = (r, g, b) => {
return { r, g, b }
}
red = getColorJSON(255, 0, 0);
green = getColorJSON(0, 255, 0);
blue = getColorJSON(0, 0, 255);
yellow = getColorJSON(255, 255, 0);
white = getColorJSON(255, 255, 255);