This repository was archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
795 lines (634 loc) · 23.6 KB
/
index.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
#!/usr/bin/env node
// ========================================================================
// Copyright 2014 Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================
var express = require('express');
var app = express();
var http = require('http');
var https = require('https');
var httpServer = http.Server(app);
var io = require('socket.io')(httpServer);
var fs = require('fs');
var spawn = require('child_process').spawn;
var path = require("flavored-path");
var cli = require("cli").enable('status');
var queuedConnection = require('./QueuedConnection.js');
var portmanager = require("./PortManager.js");
var appOptions = {
'KubernetesServer': ['s', 'URL of the Kubernetes Server', 'url'],
'NumMinions': ['m', 'Number of minions in Kubernetes cluster', 'number'],
'PodRefreshInterval': ['p', 'Time between requesting the list of pods from the master (in milliseconds)', 'number', 3000],
'OperationRefreshInterval': ['o', 'Time between checking the status on pending operations (in milliseconds)', 'number', 1000],
'KubePath': ['k', 'Kubernetes repo path', 'path', '../kubernetes'],
'KubeAuthPath': ['a', 'Path to the kubernetes authorization file', 'path', '~/.kubernetes_auth'],
'KubeApiVersion': ['v', 'Version of the Kubernetes api to query against', 'string', 'v1beta1'],
'ListenPort': ['p', 'The port the server should listen on', 'number', 3000],
'MaxReplicas': ['r', 'The maximum number of replicas the server will allow a client to create at once', 'number', 300],
'DefaultImage': ['i', 'The default docker image to use when creating pods', 'string', 'dockerfile/nginx'],
};
var options = process.clioptions = cli.parse(appOptions);
var environmentVariables = [ "AZ_CS", "KUBERNETES_PATH", "NUM_MINIONS" ];
for (var option in appOptions) {
var type = typeof appOptions[option][2] !== 'undefined' ? appOptions[option][2] : 'string';
if (type !== 'number' && type !== 'bool') {
type = 'string';
}
var required = typeof appOptions[option][3] === 'undefined';
if (required && typeof options[option] !== type) {
cli.error("Required \"" + option + "\" parameter not set. Use the -h or --help args to see help information.");
cli.getUsage();
process.exit(1);
}
}
var listenPort = options.ListenPort;
var sockets = [];
var pods = [];
var createMode = 'pods';
var MaxReplicas = options.MaxReplicas;
var KnownImages = [
'dockerfile/nginx',
'brendanburns/redis-slave',
'dockerfile/redis',
];
if (typeof options.DefaultImage !== 'undefined' && KnownImages[0] != options.DefaultImage) {
if (KnownImages.indexOf(options.DefaultImage) !== -1) {
delete KnownImages[KnownImages.indexOf(options.DefaultImage)];
}
KnownImages.splice(0, 0, options.DefaultImage);
}
var activeOperations = {
// "id": {}
};
var podRefreshInterval = options.PodRefreshInterval;
var operationRefreshInterval = options.operationRefreshInterval;
// Load kubernetes auth info
var kubernetes_auth_file = path.get("~/.kubernetes_auth");
if (!fs.existsSync(kubernetes_auth_file)) {
cli.fatal("Error: Could not find kubrenetes auth file: " + kubernetes_auth_file);
}
var kubernetesAuth = JSON.parse(fs.readFileSync(kubernetes_auth_file).toString());
var kubeServer = options.KubernetesServer;
var kubeApiVersion = options.KubeApiVersion;
var kubeAllowedApiVersions = [ "v1beta1", "v1beta2" ];
if (kubeAllowedApiVersions.indexOf(kubeApiVersion) === -1) {
kubeAllowedApiVersions.push(kubeApiVersion);
}
var PortManager = new portmanager.PortManager(options.NumMinions);
var QueuedConnectionManager = new queuedConnection.QueuedConnectionManager({
'host': kubeServer,
'apiVersion': kubeApiVersion,
'user': kubernetesAuth['User'],
'password': kubernetesAuth['Password'],
});
cli.info("Using kubernetes server: " + kubeServer);
cli.info("Using kubernetes default API version: " + kubeApiVersion);
cli.info("Using " + options.DefaultImage + " as the default image when creating pods");
cli.info("Expected number of kubernetes minions: " + options.NumMinions);
console.log();
// Setup tasks that need to occur on an interval
queryRunningPods();
cli.debug("Querying running pods every " + options.PodRefreshInterval + " milliseconds");
setInterval(function(){
queryRunningPods();
}, options.PodRefreshInterval);
cli.debug("Querying running operations every " + options.OperationRefreshInterval + " milliseconds");
setInterval(function(){
CheckActiveOperationStatuses();
}, operationRefreshInterval);
io.on('connection', function(socket){
cli.debug('a user connected');
sockets.push(socket);
socket.on('create_replicated_pod', function(startParameters) {
cli.info("Creating containers with name " + startParameters.Name + ", number of contaiers: " + startParameters.Replicas + ", id: " + startParameters.Labels.Id);
var createRequest = new PodCreateRequest(startParameters);
if (createRequest === null) {
cli.error("Error: Invalid input for PodCreateRequest");
return;
}
HandlePodCreateRequest(createRequest);
});
socket.on('get_pods', function() {
socket.emit('pods', pods);
});
socket.on('delete_all_pods', function() {
deleteRunningPods();
});
socket.on('get_base_config', function(startParameters){
var createRequest = new PodCreateRequest(startParameters);
if (createRequest == null || typeof createRequest !== 'object' || Object.keys(createRequest).length === 0) {
cli.error("Error: Invalid input for PodCreateRequest " + JSON.stringify(startParameters) + " " + JSON.stringify(createRequest));
return;
}
socket.emit(
'set_base_config',
{
'pods': GetPodConfigObject(createRequest, 1337, 'pods'),
'replicas': GetPodConfigObject(createRequest, 1337, 'replicas')
});
});
socket.on('disconnect', function () {
cli.debug("User disconnected");
var index = sockets.indexOf(socket);
delete sockets[index];
});
});
app.use('/', express.static(__dirname + '/client'));
httpServer.listen(listenPort, function(){
cli.info('K8s Visualizer server listening on *:' + listenPort);
});
function queryRunningPods() {
var requestOptions = QueuedConnectionManager.GetRequestOptionsForApi("pods", "GET");
requestOptions['connectionPriority'] = 'immediate';
var resultHandler = function(error, result) {
if (error !== null || typeof result !== 'object') {
cli.error("Error querying running pods: " + " " + error + " " + result);
return;
}
parseListQueryOutput(result);
}
QueuedConnectionManager.GetJSONQueryResultConnection(requestOptions, resultHandler);
}
function deleteRunningPods() {
cli.debug('Deleting all pods');
for (var p in pods) {
deletePod(pods[p].BaseObject.id);
}
}
function deletePod(id) {
var requestOptions = QueuedConnectionManager.GetRequestOptionsForApi("pods/" + id, "DELETE");
var resultHandler = function(error, result) {
if (error !== null || typeof result !== 'object') {
cli.error("Error deleting running pods: " + " " + error + " " + result);
return;
}
cli.ok("Pod delete complete");
for (var p in pods) {
if (pods[p].Id === id) {
delete pods[p];
break;
}
}
PushPodsToSockets(pods, sockets);
}
QueuedConnectionManager.GetJSONQueryResultConnection(requestOptions, resultHandler);
}
function parseListQueryOutput(response) {
switch (response.kind) {
case "PodList":
parsePodList(response.items);
break;
default:
cli.error("Unknown list type received from query: " + response.kind);
}
}
function parsePodList(raw_pods) {
var new_pods = [];
usedPorts = [];
PortManager.Reset();
for (var p in raw_pods) {
var pod = raw_pods[p];
var images = [];
for (var c in pod.desiredState.manifest.containers) {
var container = pod.desiredState.manifest.containers[c];
images.push(container.image);
for (p in container.ports) {
PortManager.AddUsedPort(parseInt(container.ports[p].hostPort))
}
}
if (pod.id in activeOperations) {
var loaded = true;
try {
if (typeof pod.currentState === 'undefined' || typeof pod.currentState.info === 'undefined') {
loaded = false;
} else {
for (var c in pod.desiredState.manifest.containers) {
loaded &= pod.desiredState.manifest.containers[c].name in pod.currentState.info;
if (!loaded) break;
}
}
} catch (exc) {
cli.debug("podcheck error");
loaded = false;
}
if (loaded) {
cli.debug("Marking pod complete");
delete activeOperations[pod.id];
}
}
new_pods.push(
new Pod(
pod.id,
images,
pod.currentState.host,
pod.labels,
pod));
}
cli.info("" + new_pods.length + " Pods found");
pods = new_pods;
PushPodsToSockets(new_pods, sockets);
if (pods.length === 0) {
cli.debug("Reset port PortManager")
PortManager.Reset();
}
}
var allowOperationStatusChecks = true;
var activeOperationQueries = {};
function CheckActiveOperationStatuses() {
if (!allowOperationStatusChecks) return;
for (var o in activeOperations) {
(function (id, operationNo) {
if (id in activeOperationQueries) {
return;
}
var requestOptions = QueuedConnectionManager.GetRequestOptionsForApi("operations/" + operationNo, "GET");
activeOperationQueries[id] = true;
var resultHandler = function(error, result) {
if (error !== null || typeof result !== 'object') {
cli.error("Error querying operation status: " + error + " " + result);
if (id in activeOperations) {
delete activeOperations[id];
}
return;
}
if (id in activeOperationQueries) {
delete activeOperationQueries[id];
}
ParseOperationStatus(id, result);
}
QueuedConnectionManager.GetJSONQueryResultConnection(requestOptions, resultHandler);
})(o, activeOperations[o].details);
}
}
function ParseOperationStatus(id, status) {
if (typeof status.kind !== 'undefined' && status.kind === "Status") {
activeOperations[id] = status;
} else if (id in activeOperations) {
cli.ok("Operation complete for item: " + id);
delete activeOperations[id];
for (var p in pods) {
if (pods[p].BaseObject.desiredState.manifest.id == id) {
pods[p].CreateStatus = false;
PushPodsToSockets(pods, sockets);
break;
}
}
}
};
function ParsePodListOutput(output) {
var lines = output.split("\n");
var new_pods = [];
// Iterate from the third line to the end
for (var i = 2; i < lines.length; i++) {
try {
var trimmed = lines[i].trim();
if (trimmed == "") continue;
var cols = lines[i].split(/\s+/g);
var Name = cols[0];
var Images = cols[1].split(',');
var Host = cols[2];
var Labels = cols[3].split(',');
var labels_obj = {};
for (var j = 0; j < Labels.length; j++) {
var label_split = Labels[j].split('=');
labels_obj[label_split[0]] = label_split[1];
}
new_pods.push(new Pod(Name, Images, Host, labels_obj));
} catch (exc) {
}
}
if (new_pods.length !== 0) {
pods = new_pods;
}
}
function PushPodsToSockets(pods, sockets) {
for (var s in sockets) {
sockets[s].emit('pods', pods);
}
}
function HandlePodCreateRequest(podCreateRequest) {
cli.debug("Create mode is " + createMode);
switch (createMode) {
case 'replicas':
CreateReplicatedPod(podCreateRequest);
break;
case 'pods':
CreateIndividualPodsFromReplicatedPodRequest(podCreateRequest);
break;
default:
cli.error("Error: unknown create mode: " + createMode)
}
}
function GetPodConfigObject(podCreateRequest, hostPort, mode, i) {
var useI = typeof i != 'undefined';
var id = podCreateRequest.Labels['Id'] + (useI ? '-' + i : '');
var nameSlug = podCreateRequest.Name.replace(/[^a-zA-Z0-9]+/g, '').toLowerCase();
switch (typeof mode === 'string' ? mode : createMode) {
case 'replicas':
return {
"id": id,
"kind": "ReplicationController",
"apiVersion": "v1beta2",
"desiredState": {
"replicas": podCreateRequest.Replicas,
"replicaSelector": podCreateRequest.Labels,
"podTemplate": {
"desiredState": {
"manifest": {
"version": 'v1beta2',
"id": id,
"volumes": [],
"containers": [{
"name": nameSlug,
"image": podCreateRequest.Image,
"ports": [{"containerPort": 80, "hostPort": hostPort, 'protocol': 'TCP'}]
}]
},
'restartPolicy': {},
},
"labels": podCreateRequest.Labels
}},
"labels": podCreateRequest.Labels
};
break;
case 'pods':
return {
"kind": "Pod",
"id": id,
"apiVersion": kubeApiVersion,
"desiredState": {
"manifest": {
"id": id,
"version": kubeApiVersion,
"containers": [{
"name": nameSlug + (useI ? i: ''),
"version": kubeApiVersion,
"image": podCreateRequest.Image,
"ports": [{"containerPort": 80, "hostPort": hostPort, 'protocol': 'TCP'}]
}]
},
},
"labels": podCreateRequest.Labels
};
break;
default:
return null;
}
}
function CreateReplicatedPod(podCreateRequest) {
if (podCreateRequest.Name == "") return;
cli.debug("Executing podCreateRequest: " + podCreateRequest);
// Compute a port for this pod
var hostPort = PortManager.NextPort(podCreateRequest.Replicas);
if (hostPort == null) {
// We goofed
cli.error("Error: Could not get hostPort for podCreateRequest");
return;
}
var podConfig = GetPodConfigObject(podCreateRequest, hostPort, createMode);
cli.debug("About to create replication controller request for pod: " + JSON.stringify(podConfig));
var requestOptions = QueuedConnectionManager.GetRequestOptionsForApi("replicationControllers", "POST");
var resultHandler = function(error, result) {
if (error !== null || typeof result !== 'object') {
cli.error("Error creating replication controller: " + error + " " + result);
return;
}
if (typeof result.kind !== 'undefined' && result.kind === "Status") {
cli.ok("Adding replicationController " + podConfig.id + " to active operations");
ParseOperationStatus(podConfig.id, result);
}
}
QueuedConnectionManager.GetJSONQueryResultConnection(requestOptions, resultHandler, podConfig);
}
function CreateIndividualPodsFromReplicatedPodRequest(podCreateRequest) {
if (podCreateRequest.Name == "") return;
cli.debug("Executing podCreateRequest: " + JSON.stringify(podCreateRequest));
var id = null;
for (var i = 0; i < podCreateRequest.Replicas; i++) {
var isRaw = typeof podCreateRequest.RawRequest != 'undefined' && podCreateRequest.RawRequest != null;
if (isRaw && !podCreateRequest.ValidateRawRequest(1)) {
cli.error("podCreateRequest validation failed. Cannot continue creating pod");
return;
}
var hostPort = null;
var config = null;
if (isRaw) {
cli.debug("Creating individual pods from RawRequest");
// Do this the lazy way
config = JSON.parse(JSON.stringify(podCreateRequest.RawRequest));
// edit the id
if (id == null) {
id = config.id;
}
config.id = id + "-" + i;
config.desiredState.manifest.id = id + "-" + i;
} else {
hostPort = PortManager.NextPort();
if (hostPort == null) {
// We goofed
cli.error("Error: Could not get hostPort for podCreateRequest");
return;
}
cli.debug("Trying port " + hostPort);
var config = GetPodConfigObject(podCreateRequest, hostPort, createMode, i)
if (config == null) {
cli.error("An error occured retrieving the config");
}
}
(function (podConfig) {
var t = 1;
cli.debug("About to create pod " + i + ": " + JSON.stringify(podConfig));
var requestOptions = QueuedConnectionManager.GetRequestOptionsForApi("pods", "POST");
var resultHandler = function(error, result) {
if (error !== null || typeof result !== 'object') {
cli.error("Error querying operation status: " + error + " " + result);
if (t === 1) {
// Try once more
t ++;
QueuedConnectionManager.GetJSONQueryResultConnection(requestOptions, resultHandler, podConfig);
}
return;
}
if (typeof result.kind !== 'undefined' && result.kind === "Status") {
cli.ok("Adding " + podConfig.id + " to active operations");
ParseOperationStatus(podConfig.id, result);
var pod = new Pod(podConfig.labels.name, [], '', podConfig.labels, podConfig);
pod.CreateStatus = result;
pods.push(pod);
PushPodsToSockets(pods, sockets);
}
}
QueuedConnectionManager.GetJSONQueryResultConnection(requestOptions, resultHandler, podConfig);
})(config);
}
}
//// Define Objects
function Pod(Name, Images, Host, Labels, BaseObject) {
return {
'Name': typeof Name === 'string' ? Name : '',
'Images': typeof Images === 'object' ? Images : [],
'Host': typeof Host === 'string' ? Host : '',
'Labels': typeof Labels === 'object' ? Labels : {},
'CreateStatus': BaseObject.desiredState.manifest.id in activeOperations ? activeOperations[BaseObject.desiredState.manifest.id] : false,
'BaseObject': typeof BaseObject === 'object' ? BaseObject : {}
};
}
var PodCreateRequest = (function(){
function PodCreateRequest(fromObj) {
if (typeof fromObj.RawRequest !== 'undefined' && fromObj.RawRequest != null) {
if (typeof fromObj.RawRequest !== 'object') {
cli.error("Error: RawRequest is an invalid type: " + typeof fromObj.RawRequest);
return null;
}
this.Name = typeof fromObj.RawRequest.labels.name === 'string' ? fromObj.RawRequest.labels.name : '';
this.Image = '';
this.Labels = typeof fromObj.RawRequest.labels === 'object' ? fromObj.RawRequest.labels : {};
this.RawRequest = fromObj.RawRequest;
} else {
this.Name = typeof fromObj.Name === 'string' ? fromObj.Name : '';
this.Image = typeof fromObj.Image === 'object' ? fromObj.Image : '';
this.Labels = typeof fromObj.Labels === 'object' ? fromObj.Labels : {};
this.RawRequest = null;
}
this.Replicas = typeof fromObj.Replicas !== 'undefined' ? fromObj.Replicas : 1;
if (typeof this.Replicas === 'string') {
this.Replicas = parseInt(this.Replicas);
}
if (this.Replicas > MaxReplicas) {
this.Replicas = MaxReplicas;
}
if (typeof this.Labels['name'] !== 'string') {
this.Labels['name'] = this.Name;
}
if (typeof this.Labels['Id'] === 'number') {
this.Labels['Id'] = "" + this.Labels['Id'];
}
if (typeof this.Labels['Id'] !== 'string') {
cli.error("No id found for podcreaterequest");
return null;
}
if (this.Name.length === 0) {
return null;
}
if (KnownImages.indexOf(this.Image) === -1) {
cli.info("Unkown image " + this.Image + ". using default: " + KnownImages[0]);
this.Image = KnownImages[0];
}
}
function validateField(obj, field, type, allowed, defaultValue, message) {
if (!!defaultValue && typeof obj[field] === 'undefined') {
obj[field] = defaultValue;
} else if (typeof obj[field] === 'undefined') {
cli.error("Field is undefined");
cli.error(message + obj[field]);
return false;
}
if (typeof obj[field] !== type) {
cli.error("Field type does not match: " + typeof obj[field]);
cli.error(message + obj[field]);
return false;
}
if (!!allowed && allowed.indexOf(obj[field]) === -1) {
cli.error("Field value is not allowed" + obj[field]);
cli.error(message, obj[field]);
return false;
}
return true;
}
PodCreateRequest.prototype.ValidateRawRequest = function(forNumContainers) {
if (this.RawRequest == null || typeof this.RawRequest == 'undefined') {
return false;
}
if (typeof forNumContainers === 'undefined') {
forNumContainers = this.Replicas;
}
// Validate base object
if (!validateField(this.RawRequest, 'apiVersion', 'string', kubeAllowedApiVersions, kubeApiVersion, "PodCreateRequest validation failed: Invalid api version '%s'")) {
return false;
}
if (!validateField(this.RawRequest, 'kind', 'string', ['Pod'], 'Pod', "PodCreateRequest validation failed: Invalid object kind '%s'")) {
return false;
}
if (!validateField(this.RawRequest, 'id', 'string', null, null, "PodCreateRequest validation failed: Bad pod id specified")) {
return false;
}
if (!validateField(this.RawRequest, 'labels', 'object', null, null, "PodCreateRequest validation failed: Bad labels specified")) {
return false;
}
if (!validateField(this.RawRequest.labels, 'name', 'string', null, null, "PodCreateRequest validation failed: No name label specified")) {
return false;
}
if (!validateField(this.RawRequest.labels, 'Id', 'string', null, null, "PodCreateRequest validation failed: No id label specified")) {
return false;
}
// Validate desired state
if (!validateField(this.RawRequest, 'desiredState', 'object', null, null, "PodCreateRequest validation failed: No desiredState specified")) {
return false;
}
if (!validateField(this.RawRequest.desiredState, 'manifest', 'object', null, null, "PodCreateRequest validation failed: No manifest specified")) {
return false;
}
if (!validateField(this.RawRequest.desiredState.manifest, 'id', 'string', null, null, "PodCreateRequest validation failed: No manifest id specified")) {
return false;
}
if (!validateField(this.RawRequest.desiredState.manifest, 'version', 'string', kubeAllowedApiVersions, kubeApiVersion, "PodCreateRequest validation failed: Bad manifest version specified")) {
return false;
}
if (!validateField(this.RawRequest.desiredState.manifest, 'containers', 'object', null, null, "PodCreateRequest validation failed: No manifest containers specified")) {
return false;
}
if (this.RawRequest.desiredState.manifest.containers.length === 0) {
cli.error("PodCreateRequest validation failed: No containers specified");
return false;
}
var usedPorts = [];
for (var c in this.RawRequest.desiredState.manifest.containers) {
var container = this.RawRequest.desiredState.manifest.containers[c];
if (!validateField(container, 'name', 'string', null, null, "PodCreateRequest validation failed: No container name specified %s")) {
return false;
}
if (!validateField(container, 'version', 'string', kubeAllowedApiVersions, kubeApiVersion, "PodCreateRequest validation failed: No container name specified %s")) {
return false;
}
if (!validateField(container, 'image', 'string', KnownImages, 'dockerfile/nginx', "PodCreateRequest validation failed: Invalid container image %s")) {
return false;
}
if (typeof container.ports !== 'undefined') {
for (var p in container.ports) {
var port = container.ports[p];
if (!validateField(port, 'containerPort', 'number', null, null, "PodCreateRequest validation failed: No containerPort specified")) {
return false;
}
if (!validateField(port, 'hostPort', 'number', null, null, "PodCreateRequest validation failed: No hostPort specified")) {
return false;
}
if (usedPorts.indexOf(port.hostPort) != -1) {
cli.error("PodCreateRequest validation failed: Two containers may not have the same host port " + port.hostPort);
return false;
}
usedPorts.push(port.hostPort);
if (typeof port.protocol !== 'undefined' && !validateField(port, 'protocol', 'string', ['TCP', 'UDP'], 'TCP', "PodCreateRequest validation failed: Invalid container port protocol %s")) {
return false;
}
if (PortManager.PortAvailable(port.hostPort, forNumContainers)) {
PortManager.AddUsedPort(port.hostPort);
} else {
var newPort = PortManager.NextPort(forNumContainers);
cli.info("PodCreateRequest validation: Notification: Changing port from " + port.hostPort + " to " + newPort);
port.hostPort = newPort;
}
}
}
}
return true;
}
return PodCreateRequest;
})();