-
Notifications
You must be signed in to change notification settings - Fork 0
/
l7mp.js
1147 lines (979 loc) · 38.5 KB
/
l7mp.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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// L7mp: A programmable L7 meta-proxy
//
// Copyright 2019 by its authors.
// Some rights reserved. See AUTHORS.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const path = require('path');
const fs = require('fs');
const util = require('util');
const log = require('npmlog');
const YAML = require('yamljs');
const getVersion = require('git-repo-version');
const jsonFormat = require('format-json');
const Listener = require('./listener.js').Listener;
const Cluster = require('./cluster.js').Cluster;
const EndPoint = require('./cluster.js').EndPoint;
const Session = require('./session.js').Session;
const Rule = require('./rule.js').Rule;
const RuleList = require('./rule.js').RuleList;
const Route = require('./route.js').Route;
const L7mpOpenAPI= require('./l7mp-openapi.js').L7mpOpenAPI;
const {L7mpError, Ok, InternalError, BadRequestError, NotFoundError, ValidationError, GeneralError} = require('./error.js');
const MAX_NAME_ATTEMPTS = 20;
// WARNING: these dumpers are needed for development and testing only,
// will be removed later
global.dumper = function dumper(o, depth=1){
return util.inspect(o, {compact: 100000,
breakLength: Infinity,
depth: depth});
}
global.dump = function dump(o, depth=5){
console.log(dumper(o, depth));
}
global.toJSON = function toJSON(data, format='plain'){
switch(format){
case 'terse': return jsonFormat.terse(data);
case 'space': return jsonFormat.space(data);
case 'lines': return jsonFormat.lines(data);
case 'diffy': return jsonFormat.diffy(data);
case 'plain':
default: return jsonFormat.plain(data);
}
}
// validate object schemas beyond OpenAPI validation
const validate = (object, schema) => {
if(object === null || !object ||
!(object instanceof Object &&
Object.getPrototypeOf(object) == Object.prototype))
return 'An object (a set of key-value pairs) is expected';
let e = Object
.entries(schema)
.map( ([key, ref]) => [
key,
!ref.required || (key in object),
!ref.validate || (!ref.required && !(key in object)) ||
ref.validate(object[key])
])
.find( ([_, r, v]) => !r || !v );
return e && `Property ${e[0]} is ${ !e[1] ? 'required' : 'invalid' }`;
}
class L7mp {
constructor(n) {
this.name = n || `L7mp-${L7mp.index++}`;
this.static_config = {};
// object hierarchy
this.admin = {};
this.listeners = [];
this.clusters = [];
this.rulelists = [];
this.rules = [];
this.sessions = [];
this.routes = [];
this.endpoints = [];
this.cleanup = [];
}
dumpL7mp(options) {
log.silly('L7mp.dumpL7mp:', `"${this.name}"`);
return {
admin: this.getAdmin(),
listeners: this.listeners.map( l => this.dumpListener(l.name)),
clusters: this.clusters.map( c => this.dumpCluster(c.name)),
rulelists: this.rulelists.map( r => this.dumpRuleList(r.name)),
rules: this.rules.map( r => this.dumpRule(r.name)),
routes: this.routes.map( r => this.dumpRoute(r.name)),
endpoints: this.endpoints.map( e => this.dumpEndPoint(e.name)),
sessions: this.sessions.map(s => s.toJSON()),
};
}
newName(name, find){
let i = 0;
do{
if(i > MAX_NAME_ATTEMPTS){
let e = `Could not find new name after` +
`${MAX_NAME_ATTEMPTS} iterations`;
log.error('L7mp.newName:', e);
throw new Error(`Cannot create new name from ${name}: ${e}`);
}
var newname = name + (i > 0 ? `_${i}` : '');
i++;
} while(find.bind(this)(newname));
return newname;
}
async readConfig(config){
log.silly('L7mp.readConfig', config);
try {
this.static_config = {};
if(path.extname(config).toLowerCase() === '.yaml')
this.static_config = YAML.parse(fs.readFileSync(config, 'utf8'));
else
this.static_config = JSON.parse(fs.readFileSync(config));
} catch(err) {
log.error(`Could not read static configuration ${config}:`,
err.code ? `${err.code}: ${err.message}` : err.message);
}
}
async run(argv){
// validate and execute static config
if(this.admin.strict){
log.info('L7mp.run: Initializing OpenAPI backend');
this.openapi = new L7mpOpenAPI();
await this.openapi.init();
// mock a request object for the openapi-backend validator
let req = {
version: '1.1',
method: 'POST',
url: '/api/v1/setConf',
query: {},
headers: { host: 'localhost:65555'},
'content-type': 'application/json',
body: this.static_config,
};
let res = this.openapi.api.validator.validateRequest(req, 'setConf');
if(!res.valid)
log.error("Config file JSON validation error (strict mode: on):\n",
JSON.stringify(res.errors, null, 4));
}
this.applyAdmin(this.static_config.admin || {});
// override anything that was set in the config file
if(argv && 's' in argv) l7mp.admin.strict = true;
if(argv && 'l' in argv) log.level = argv.l;
log.info(`Starting l7mp version: ${this.admin.version} Log-level: ${log.level}`,
'Strict mode:', l7mp.admin.strict ? 'enabled' : 'disabled');
try {
let p = [];
if('clusters' in this.static_config)
this.static_config.clusters.map((c) => p.push(this.addCluster(c)));
if('listeners' in this.static_config)
this.static_config.listeners.map((l) => p.push(this.addListener(l)));
if('rulelists' in this.static_config)
this.static_config.rulelists.map((r) => p.push(this.addRuleList(r)));
if('rules' in this.static_config)
this.static_config.rules.map((r) => p.push(this.addRule(r)));
if('routes' in this.static_config)
this.static_config.routes.map((r) => p.push(this.addRoute(r)));
await Promise.all(p);
} catch(e) {
log.silly(dumper(e, 6));
log.error(`Could not initialize static configuration:`,
e.code ? `${e.code}: ${e.message}` : e.message);
}
}
////////////////////////////////////////////////////
//
// Admin API
//
////////////////////////////////////////////////////
applyAdmin(admin) {
log.silly('L7mp.applyAdmin:', dumper(admin));
this.admin.log_level = log.level = 'log_level' in admin ?
admin.log_level : log.level;
this.admin.strict = 'strict' in admin ? admin.strict : false;
if('log_file' in admin){
this.admin.log_file = admin.log_file;
switch(admin.log_file){
case 'stdout':
this.admin.log_stream = process.stdout;
break;
case 'stderr':
this.admin.log_stream = process.stderr;
break;
default:
this.admin.log_stream =
fs.createWriteStream(admin.log_file);
}
}
if('access_log_path' in admin){
this.admin.access_log_path = admin.access_log_path;
log.warn('L7mp.applyAdmin: access_log_path', 'TODO');
}
// VERSION: package.jon:version + git-commit-hash + git-commit-date
this.admin.version =
getVersion({ shaLength: 10, includeDate: true });
// if git-commit is missing, we get 'null', remove it
this.admin.version = this.admin.version.replace(/ null/, '');
}
getAdmin(){
log.silly('L7mp.getAdmin');
var admin = { log_level: this.admin.log_level,
strict: this.admin.strict,
version: this.admin.version };
if(this.admin.log_file) admin.log_file = this.admin.log_file;
if(this.admin.access_log_path)
admin.access_log_path = this.admin.access_log_path;
return admin;
}
////////////////////////////////////////////////////
//
// Listener API
//
////////////////////////////////////////////////////
async addListener(l, options) {
options = {autogenerated: false, ...options};
log.info('L7mp.addListener', dumper(l, 8), 'options:', dumper(options,3));
let schema = {
name: {
validate: (value) => /^\S+?$/.test(value), // alnum
required: true,
},
spec: {
validate: (value) => value instanceof Object,
required: true,
},
rules: {
validate: (value) => (value instanceof Array) || (typeof value === 'string'),
required: true,
}
};
let e = validate(l, schema);
if(e){
log.warn('L7mp.addListener:', e);
throw new Error(`Cannot add listener: ${e}`);
}
if(this.getListener(l.name)){
let e = `Listener "${l.name}" already defined`;
log.warn(`L7mp.addListener:`, e );
throw new Error(`Cannot add listener: ${e}`);
}
l.emitter = this.addSession.bind(this);
l.autogenerated = options.autogenerated;
var li = Listener.create(l);
// li.on('emit', (s) => this.addSession(s));
// if this is a reference to a named RuleList (aka:
// MatchActionTable), defer binding until runtime, otherwise,
// create a new rulelist
if(Array.isArray(l.rules)){
let rl = {};
rl.rules = l.rules;
rl.name = this.newName(`${li.name}-RuleList-${RuleList.index++}`,
this.getRuleList);
await this.addRuleList(rl, {autogenerated: true});
li.rules = rl.name;
} else if(typeof l.rules === 'string'){
li.rules = l.rules;
} else {
let e = `Invalid RuleList`;
log.warn(`L7mp.addListener:`, e );
throw new Error(`Cannot add listener: ${e}`);
}
this.listeners.push(li);
// this may fail: promise
try {
await li.run();
} catch(err){
log.silly(`L7mp.addListener:`, dumper(err, 6));
log.warn(`Cannot add listener: ${err.message}`);
this.deleteListener(li.name, {recursive: true});
throw err;
}
return li;
}
getListener(n){
log.silly('L7mp.getListener:', n);
return this.listeners.find( ({name}) => name === n );
}
dumpListener(n, options){
options = {recursive: false, ...options};
log.silly('L7mp.dumpListener:', n, 'options:', dumper(options, 4));
let l = this.getListener(n);
if(!l){
let error = `Unknown listener "${n}"`;
log.warn('L7mp.dumpListener:', error);
throw new NotFoundError(`Cannot dump Listener: ${error}`);
}
let rules = l.rules;
if(options.recursive){
let r = this.getRuleList(l.rules);
if(r){
let rl = this.dumpRuleList(r.name, options);
rules = rl.rules; // we have to "jump through" the rulelist
} else {
rules = `${l.rules}:<STALE>`;
}
}
return {
name: l.name,
spec: l.spec,
rules: rules,
options: l.options,
};
}
deleteListener(n, options){
options = {recursive: false, ...options};
log.info('L7mp.deleteListener:', n, 'options:', dumper(options, 4));
let i = this.listeners.findIndex( ({name}) => name === n);
if(i >= 0){
let l = this.listeners[i];
if(options.recursive){
let rulelist = this.getRuleList(l.rules);
if(rulelist && rulelist.autogenerated)
this.deleteRuleList(rulelist.name, options);
// sessions are always autogenerated
for(let s of this.sessions)
if(s.source && s.source.origin === l.name)
s.end(new Ok("Listener deleted from API"));
}
l.close();
this.listeners.splice(i, 1);
} else {
let e = `Unknown listener "${n}"`;
log.warn(`L7mp.deleteListener:`, e );
throw new Error(`Cannot delete listener: ${e}`);
}
}
////////////////////////////////////////////////////
//
// Cluster API
//
////////////////////////////////////////////////////
async addCluster(c, options) {
options = {autogenerated: false, ...options};
log.info('L7mp.addCluster', dumper(c, 8), 'options:', dumper(options,3));
let schema = {
name: {
validate: (value) => /^\S+?$/.test(value),
required: true,
},
spec: {
validate: (value) => value instanceof Object,
required: true,
},
};
let e = validate(c, schema);
if(e){
log.warn('L7mp.addCluster:', e);
throw new Error(`Cannot add cluster: ${e}`);
}
if(this.getCluster(c.name)){
let e = `Cluster "${c.name}" already defined`;
log.warn('L7mp.addCluster', e);
throw new Error(`Cannot add cluster: ${e}`);
}
c.autogenerated = options.autogenerated;
let cu = Cluster.create(c);
this.clusters.push(cu);
if(c.endpoints)
for(let e of c.endpoints){
e.name = e.name ||
this.newName(`${cu.name}-EndPoint-${EndPoint.index++}`,
this.getEndPoint);
this.addEndPoint(cu.name, e);
}
try {
await cu.run();
} catch(err){
log.silly(`L7mp.addCluster:`, dumper(err, 6));
log.warn(`Cannot add cluster: ${err.message}`);
this.deleteCluster(cu.name, {recursive: true});
throw err;
}
return cu;
}
getCluster(n){
log.silly('L7mp.getCluster:', n);
return this.clusters.find( ({name}) => name === n );
}
dumpCluster(n, options){
options = {recursive: false, ...options};
log.silly('L7mp.dumpCluster:', n, 'options:', dumper(options, 4));
let c = this.getCluster(n);
if(!c){
let error = `Unknown cluster "${n}"`;
log.warn('L7mp.dumpCluster:', error);
throw new NotFoundError(`Cannot dump Cluster: ${error}`);
}
let endpoints = c.endpoints.length;
if(options.recursive)
endpoints = c.virtual ? [c.virtualEndPoint()] :
c.endpoints.map(x => this.dumpEndPoint(x.name));
return {
name: c.name,
spec: c.spec,
endpoints: endpoints,
loadbalancer: c.loadbalancer.toJSON(),
options: c.options,
};
}
deleteCluster(n, options){
options = {recursive: false, ...options};
log.info('L7mp.deleteCluster:', n, 'options:', dumper(options, 4));
let i = this.clusters.findIndex( ({name}) => name === n);
if(i >= 0){
let c = this.clusters[i];
if(options.recursive){
for(let s of this.sessions)
if(s.destination.origin === c.name ||
s.chain.ingress.some(stage =>
stage.origin === c.name) ||
s.chain.egress.some(stage =>
stage.origin === c.name))
s.end(new Ok("Traversed Cluster deleted from API"));
}
// deleting a cluster always deletes the endpoints
let endpoints = c.endpoints.map(e => e.name);
for(let e of endpoints)
this.deleteEndPoint(e, options);
this.clusters.splice(i, 1);
} else {
let e = `Unknown cluster "${n}"`;
log.warn(`L7mp.deleteCluster:`, e );
throw new Error(`Cannot delete cluster: ${e}`);
}
}
////////////////////////////////////////////////////
//
// RuleList (aka MatchActionTable) API
//
////////////////////////////////////////////////////
async addRuleList(r, options) {
options = {autogenerated: false, ...options};
log.info('L7mp.addRuleList', dumper(r, 8), 'options:', dumper(options,3));
let schema = {
name: {
validate: (value) => /^\S+?$/.test(value),
required: true,
},
rules: {
validate: (value) => value instanceof Array,
required: true,
},
};
let e = validate(r, schema);
if(e){
log.warn('L7mp.addRuleList:', e);
throw new Error(`Cannot add RuleList: ${e}`);
}
if(this.getRuleList(r.name)){
let e = `RuleList "${r.name}" already defined`;
log.warn('L7mp.addRuleList', e);
throw new Error(`Cannot add RuleList: ${e}`);
}
// rules are added in a separate run
let rules = Array.from(r.rules);
r.rules = [];
r.autogenerated = options.autogenerated;
let rl = RuleList.create(r);
this.rulelists.push(rl);
for(let i = 0; i < rules.length; i++)
await this.addRuleToRuleList(rl, rules[i], i, {autogenerated: true});
return r;
}
getRuleList(n){
log.silly('L7mp.getRuleList:', n);
return this.rulelists.find( ({name}) => name === n );
}
dumpRuleList(n, options){
options = {recursive: false, ...options};
log.silly('L7mp.dumpRuleList:', n, 'options:', dumper(options, 4));
let r = this.getRuleList(n);
if(!r){
let error = `Unknown RuleList "${n}"`;
log.warn('L7mp.dumpRuleList:', error);
throw new NotFoundError(`Cannot dump RuleList: ${error}`);
}
let rules = r.rules;
if(options.recursive){
rules = [];
for(let ru of r.rules){
let rule = this.getRule(ru);
rules.push(rule ? this.dumpRule(rule.name, options) :
`${ru}:<STALE>`);
}
}
return {
name: r.name,
rules: rules,
};
}
deleteRuleList(n, options){
options = {recursive: false, ...options};
log.info('L7mp.deleteRuleList:', n, 'options:', dumper(options, 4));
let i = this.rulelists.findIndex( ({name}) => name === n);
if(i >= 0){
let rl = this.rulelists[i];
if(options.recursive){
let rules = [...rl.rules];
for(let r of rules){
let rule = this.getRule(r);
if(rule && rule.autogenerated){
let i = rl.rules.findIndex( (name) => name === r);
this.deleteRuleFromRuleList(rl, i);
}
this.deleteRule(r, options);
}
}
this.rulelists.splice(i, 1);
} else {
let e = `Unknown RuleList "${n}"`;
log.warn(`L7mp.deleteRuleList:`, e );
throw new Error(`Cannot delete RuleList: ${e}`);
}
}
// must be called with a ref to rulelist!
async addRuleToRuleList(rl, rule, pos, options) {
options = {autogenerated: false, ...options};
log.info(`L7mp.addRuleToRuleList: position ${pos}:`, dumper(rule, 8),
'options:', dumper(options, 4));
if(!(rl instanceof RuleList)){
let e = `Invalid rulelist`;
log.warn(`L7mp.addRuleToRuleList:`, e );
throw new Error(`Cannot add rule to rulelist: ${e}`);
}
if(pos < 0 || pos > rl.rules.length){
let e = `Cannot insert rule at position ${pos} into rulelist`;
log.warn(`L7mp.addRuleToRuleList:`, e);
throw new Error(e);
}
let name = rule;
if(typeof rule === 'object'){
// inline rule: create
name = rule.name = rule.name ||
this.newName(`${rl.name}-Rule-${Rule.index++}`, this.getRule);
await this.addRule(rule, {autogenerated: true});
} else if(typeof rule !== 'string'){
let e = `Invalid rule`;
log.warn(`L7mp.addRule:`, e );
throw new Error(`Cannot add rule to rulelist: ${e}`);
}
rl.rules.splice(pos, 0, name);
}
// must be called with a ref to rulelist!
// cannot be recursive
deleteRuleFromRuleList(rl, pos, options){
options = {recursive: false, ...options};
log.info(`L7mp.deleteRuleFromRuleList: rulelist ${rl.name}, position ${pos}`,
'options:', dumper(options, 4));
if(!(rl instanceof RuleList)){
let e = `Invalid rulelist`;
log.warn(`L7mp.deleteRuleFromRuleList:`, e );
throw new Error(`Cannot delete rule from rulelist: ${e}`);
}
if(pos < 0 || pos > rl.rules.length){
let e = `Cannot delete rule at position ${pos} from rulelist`;
log.warn(`L7mp.deleteRuleFromRuleList:`, e);
throw new Error(e);
}
let r = rl.rules[pos];
if(options.recursive)
this.deleteRule(r, options);
rl.rules.splice(pos, 1);
}
////////////////////////////////////////////////////
//
// Rule API
//
////////////////////////////////////////////////////
async addRule(r, options) {
options = {autogenerated: false, ...options};
log.info('L7mp.addRule:', dumper(r, 8), 'options:', dumper(options,3));
let schema = {
name: {
validate: (value) => /^\S+?$/.test(value),
required: true,
},
action: {
validate: (value) => value instanceof Object,
required: true,
},
};
let e = validate(r, schema);
if(e){
log.warn('L7mp.addRule:', e);
throw new Error(`Cannot add rule: ${e}`);
}
if(this.getRule(r.name)){
let e = `Rule "${r.name}" already defined`;
log.warn('L7mp.addRule', e);
throw new Error(`Cannot add rule: ${e}`);
}
r.autogenerated = options.autogenerated;
r = Rule.create(r);
this.rules.push(r);
let route = r.action.route;
if(route){
if(typeof route === 'object'){
// inline route: create
route.name = route.name ||
this.newName(`${r.name}-Route-${Route.index++}`, this.getRoute);
await this.addRoute(route, {autogenerated: true});
r.action.route = route.name;
}
if(typeof r.action.route !== 'string'){
let e = `Invalid route`;
log.warn(`L7mp.addRule:`, e );
throw new Error(`Cannot add rule: ${e}`);
}
}
return r;
}
getRule(n){
log.silly('L7mp.getRule:', n);
return this.rules.find( ({name}) => name === n );
}
dumpRule(n, options){
options = {recursive: false, ...options};
log.silly('L7mp.dumpRule:', n, 'options:', dumper(options, 4));
let r = this.getRule(n);
if(!r){
let error = `Unknown Rule "${n}"`;
log.warn('L7mp.dumpRule:', error);
throw new NotFoundError(`Cannot dump Rule: ${error}`);
}
let res = { name: r.name, match: r.match.toJSON(), action: {} };
if(r.action.rewrite)
res.action.rewrite = r.action.rewrite;
if(r.action.apply){
res.action.apply = r.action.apply;
}
if(r.action.route){
let route = r.action.route;
if(options.recursive){
let rou = this.getRoute(route);
res.action.route = rou ? this.dumpRoute(rou.name, options) : `${route}:<STALE>`;
} else {
res.action.route = route;
}
}
return res;
}
deleteRule(n, options){
options = {recursive: false, ...options};
log.info('L7mp.deleteRule:', n, 'options:', dumper(options, 4));
let i = this.rules.findIndex( ({name}) => name === n);
if(i >= 0){
let rule = this.rules[i];
if(options.recursive && rule.action.route){
let route = this.getRoute(rule.action.route);
if(route && route.autogenerated)
this.deleteRoute(rule.action.route, options);
}
this.rules.splice(i, 1);
} else {
let e = `Unknown rule "${n}"`;
log.warn(`L7mp.deleteRule:`, e );
throw new Error(`Cannot delete rule: ${e}`);
}
}
////////////////////////////////////////////////////
//
// Route API
//
////////////////////////////////////////////////////
async addRoute(r, options) {
options = {autogenerated: false, ...options};
log.silly('L7mp.addRoute:', dumper(r, 6), 'options:', dumper(options,3));
// accept ols-style API
r.destination = r.destination || r.cluster;
let schema = {
name: {
validate: (value) => /^\S+?$/.test(value),
required: true,
},
destination: {
required: true,
},
ingress: {
validate: (value) => value instanceof Array,
},
egress: {
validate: (value) => value instanceof Array,
},
retry: {
validate: (value) => value instanceof Object,
},
};
let e = validate(r, schema);
if(e){
log.warn('L7mp.addRoute:', e);
throw new Error(`Cannot add route: ${e}`);
}
if(this.getRoute(r.name)){
let e = `Route "${r.name}" already defined`;
log.warn('L7mp.addRoute', e);
throw new Error(`Cannot add route: ${e}`);
}
if(typeof r.destination === 'object'){
// inline cluster: create
r.destination.name = r.destination.name ||
this.newName(`${r.name}-Cluster-${Cluster.index++}`, this.getCluster);
await this.addCluster(r.destination, {autogenerated: true});
r.destination = r.destination.name;
}
if(typeof r.destination !== 'string'){
let e = `Invalid destination`;
log.warn(`L7mp.addRoute:`, e );
throw new Error(`Cannot add route: ${e}`);
}
for(let dir of ['ingress', 'egress']){
if(!r[dir]) continue;
for(let i = 0; i < r[dir].length; i++){
let c = r[dir][i];
if(typeof c === 'object'){
// inline cluster: create
c.name = c.name ||
this.newName(`${r.name}-${dir}-Cluster-${Cluster.index++}`, this.getCluster);
await this.addCluster(c, {autogenerated: true});
r[dir][i] = c.name;
}
if(typeof r[dir][i] !== 'string'){
let e = `Invalid cluster`;
log.warn(`L7mp.addRoute:`, e );
throw new Error(`Cannot add route: ${e}`);
}
}
}
r.autogenerated = options.autogenerated;
r = Route.create(r);
this.routes.push(r);
return r;
}
getRoute(n){
log.silly('L7mp.getRoute:', n);
return this.routes.find( ({name}) => name === n );
}
dumpRoute(n, options){
options = {recursive: false, ...options};
log.silly('L7mp.dumpRoute:', n, 'options:', dumper(options, 4));
let r = this.getRoute(n);
if(!r){
let error = `Unknown Route "${n}"`;
log.warn('L7mp.dumpRoute:', error);
throw new NotFoundError(`Cannot dump Route: ${error}`);
}
let res = {
name: r.name,
destination: r.destination,
ingress: r.ingress,
egress: r.egress,
retry: r.retry,
};
if(options.recursive){
let dest = this.getCluster(r.destination);
res.destination = dest ? this.dumpCluster(dest.name, options) :
`${r.destination}:<STALE>`;
for(let dir of ['ingress', 'egress']){
res[dir] = [];
for(let i = 0; i < r[dir].length; i++){
let c = this.getCluster(r[dir][i]);
res[dir].push(c ? this.dumpCluster(r[dir][i], options) :
`${r[dir][i]}:<STALE>`);
}
}
}
return res;
}
deleteRoute(n, options){
options = {recursive: false, ...options};
log.silly('L7mp.deleteRoute:', n, 'options:', dumper(options, 4));
let i = this.routes.findIndex(({name}) => name === n);
if(i >= 0){
if(options.recursive){
let route = this.routes[i];
let dest = this.getCluster(route.destination);
if(dest && dest.autogenerated)
this.deleteCluster(route.destination, options);
for(let dir of ['ingress', 'egress']){
if(!route[dir]) continue;
let cs = [...route[dir]];
for(let i = 0; i < cs.length; i++){
let c = this.getCluster(cs[i]);
if(c && c.autogenerated)
this.deleteCluster(cs[i], options);
}
}
}
this.routes.splice(i, 1);
} else {
let e = `Unknown Route "${n}"`;
log.warn(`L7mp.deleteRoute:`, e );
throw new Error(`Cannot delete Route: ${e}`);
}
}
////////////////////////////////////////////////////
//
// EndPoint API
//
////////////////////////////////////////////////////
async addEndPoint(c, ep) {
log.silly('L7mp.addEndPoint:', dumper(ep, 6));
let schema = {
name: {
validate: (value) => /^\S+?$/.test(value),
},
spec: {
validate: (value) => value instanceof Object,
required: true,
},
};
let e = validate(ep, schema);
if(e){
log.warn('L7mp.addEndPoint:', e);
throw new Error(`Cannot add endpoint: ${e}`);
}
if(this.getEndPoint(ep.name)){
let e = `Endpoint "${ep.name}" already defined`;
log.warn('L7mp.addEndpoint', e);
throw new Error(`Cannot add endpoint: ${e}`);
}
let cl = this.getCluster(c);
if(!cl){
let e = `Unknown cluster "${c}"`;
log.warn('L7mp.addEndPoint', e);
throw new NotFoundError(`Cannot add endpoint: ${e}`);
}
ep = cl.addEndPoint(ep);
this.endpoints.push(ep);
return ep;
}
getEndPoint(n){
log.silly('L7mp.getEndPoint:', n);
return this.endpoints.find( ({name}) => name === n );
}
dumpEndPoint(n){
log.silly('L7mp.dumpEndPoint:', n);
let e = this.getEndPoint(n);
if(!e){
let error = `Unknown endpoint "${n}"`;
log.warn('L7mp.dumpEndPoint:', error);
throw new NotFoundError(`Cannot dump EndPoint: ${error}`);
}
return {
name: e.name,
spec: e.spec,
weight: e.weight,