Skip to content

Commit b8ffb6d

Browse files
authored
Merge pull request openiap#35 from skadefro/master
Improvements
2 parents 06e4bb3 + 9691449 commit b8ffb6d

26 files changed

Lines changed: 765 additions & 67 deletions

OpenFlow/src/Config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export class Config {
1212
public static db: DatabaseConnection = null;
1313
public static version: string = fs.readFileSync("VERSION", "utf8");
1414

15+
public static NODE_ENV: string = Config.getEnv("NODE_ENV", "development");
16+
1517
public static auto_create_users: boolean = Config.parseBoolean(Config.getEnv("auto_create_users", "false"));
1618
public static auto_create_domains: string[] = Config.parseArray(Config.getEnv("auto_create_domains", ""));
1719
public static allow_user_registration: boolean = Config.parseBoolean(Config.getEnv("allow_user_registration", "false"));

OpenFlow/src/KubeUtil.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@ export class KubeUtil {
1616
}
1717
constructor() {
1818
const kc = new k8s.KubeConfig();
19-
kc.loadFromCluster();
19+
var success: boolean = false;
20+
try {
21+
kc.loadFromDefault();
22+
success = true;
23+
} catch (error) {
24+
console.log(error);
25+
}
26+
if (success == false) {
27+
try {
28+
kc.loadFromCluster();
29+
success = true;
30+
} catch (error) {
31+
console.log(error);
32+
}
33+
}
2034
this.CoreV1Api = kc.makeApiClient(k8s.CoreV1Api);
2135
this.AppsV1Api = kc.makeApiClient(k8s.AppsV1Api);
2236
this.ExtensionsV1beta1Api = kc.makeApiClient(k8s.ExtensionsV1beta1Api);

OpenFlow/src/Messages/Message.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { WebSocketClient, QueuedMessage } from "../WebSocketClient";
55
import { QueryMessage } from "./QueryMessage";
66
import { Base, Rights, WellknownIds } from "../base";
77
import { SigninMessage } from "./SigninMessage";
8-
import { User } from "../User";
8+
import { User, NoderedUser } from "../User";
99
import { Auth } from "../Auth";
1010
import { Crypt } from "../Crypt";
1111
import { TokenUser } from "../TokenUser";
@@ -753,23 +753,30 @@ export class Message {
753753
private async EnsureNoderedInstance(cli: WebSocketClient): Promise<void> {
754754
this.Reply();
755755
var msg: EnsureNoderedInstanceMessage;
756-
var user: User;
756+
var user: NoderedUser;
757757
try {
758758
cli._logger.debug("[" + cli.user.username + "] EnsureNoderedInstance");
759759
msg = EnsureNoderedInstanceMessage.assign(this.data);
760760
var name = await this.GetInstanceName(cli, msg._id, msg.name);
761761
var _id = msg._id;
762762
if (_id === null || _id === undefined || _id === "") _id = cli.user._id;
763+
764+
var users = await Config.db.query<NoderedUser>({ _id: _id }, null, 1, 0, null, "users", cli.jwt);
765+
if (users.length == 0) {
766+
throw new Error("Unknown userid " + _id);
767+
}
768+
user = NoderedUser.assign(users[0]);
769+
770+
763771
var namespace = Config.namespace;
764772
var hostname = Config.nodered_domain_schema.replace("$nodered_id$", name);
765773

766774
var nodereduser = await User.FindById(_id, cli.jwt);
767775
var tuser: TokenUser = new TokenUser(nodereduser);
768776
var nodered_jwt: string = Crypt.createToken(tuser, Config.personalnoderedtoken_expires_in);
769777

770-
var queue_prefix: string = "";
771778
if (Config.force_queue_prefix) {
772-
queue_prefix = nodereduser.username;
779+
user.nodered.queue_prefix = nodereduser.username;
773780
}
774781

775782
cli._logger.debug("[" + cli.user.username + "] ensure nodered role " + name + "noderedadmins");
@@ -799,14 +806,15 @@ export class Message {
799806
image: Config.nodered_image,
800807
imagePullPolicy: "Always",
801808
ports: [{ containerPort: 80 }, { containerPort: 5858 }],
809+
resources: user.nodered.resources,
802810
env: [
803811
{ name: "saml_federation_metadata", value: Config.saml_federation_metadata },
804812
{ name: "saml_issuer", value: Config.saml_issuer },
805813
{ name: "saml_baseurl", value: Config.protocol + "://" + hostname + "/" },
806814
{ name: "nodered_id", value: name },
807815
{ name: "nodered_sa", value: nodereduser.username },
808816
{ name: "jwt", value: nodered_jwt },
809-
{ name: "queue_prefix", value: queue_prefix },
817+
{ name: "queue_prefix", value: user.nodered.queue_prefix },
810818
{ name: "api_ws_url", value: Config.api_ws_url },
811819
{ name: "amqp_url", value: Config.amqp_url },
812820
{ name: "nodered_domain_schema", value: hostname },
@@ -815,6 +823,8 @@ export class Message {
815823
{ name: "port", value: Config.port.toString() },
816824
{ name: "noderedusers", value: (name + "noderedusers") },
817825
{ name: "noderedadmins", value: (name + "noderedadmins") },
826+
{ name: "api_allow_anonymous", value: user.nodered.api_allow_anonymous.toString() },
827+
{ name: "NODE_ENV", value: Config.NODE_ENV },
818828
],
819829
livenessProbe: {
820830
httpGet: {
@@ -889,6 +899,7 @@ export class Message {
889899
} catch (error) {
890900
this.data = "";
891901
cli._logger.error(error);
902+
console.log(JSON.stringify(error, null, 2));
892903
//msg.error = JSON.stringify(error, null, 2);
893904
if (msg !== null && msg !== undefined) msg.error = "Request failed!"
894905
}

OpenFlow/src/User.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,36 @@ export class User extends Base {
201201
// user = User.assign(user);
202202
// return user;
203203
// }
204-
205204
}
205+
export class resourcevalues {
206+
public cpu: string;
207+
public memory: string;
208+
}
209+
export class resources {
210+
public limits: resourcevalues;
211+
public requests: resourcevalues;
212+
}
213+
export class noderedconfig {
214+
constructor() {
215+
// this.resources = new resources();
216+
}
217+
public resources: resources;
218+
public api_allow_anonymous: boolean = false;
219+
public queue_prefix: string = "";
220+
}
221+
222+
export class NoderedUser extends User {
223+
public nodered: noderedconfig;
224+
constructor() {
225+
super();
226+
this._type = "user";
227+
}
228+
static assign<NoderedUser>(o: any): NoderedUser {
229+
var res = Object.assign(new NoderedUser(), o);
230+
if (res.nodered === null || res.nodered === undefined) {
231+
res.nodered = new noderedconfig();
232+
}
233+
return res;
234+
}
206235

236+
}

OpenFlow/src/WebServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ export class WebServer {
136136
server = http.createServer(this.app);
137137
}
138138

139-
140-
server.listen(Config.port);
139+
var port = Config.port;
140+
server.listen(port);
141141
return server;
142142
}
143143
}

OpenFlow/src/public/Controllers.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2645,6 +2645,9 @@ module openflow {
26452645
public instancelog: string = "";
26462646
public name: string = "";
26472647
public userid: string = "";
2648+
public user: NoderedUser = null;
2649+
public limitsmemory: string = "";
2650+
public loading: boolean = false;
26482651
constructor(
26492652
public $scope: ng.IScope,
26502653
public $location: ng.ILocationService,
@@ -2654,26 +2657,58 @@ module openflow {
26542657
) {
26552658
console.debug("NoderedCtrl");
26562659
WebSocketClient.onSignedin(async (user: TokenUser) => {
2660+
this.loading = true;
26572661
await api.RegisterQueue();
26582662
this.userid = $routeParams.id;
26592663
if (this.userid == null || this.userid == undefined || this.userid == "") {
26602664
this.name = WebSocketClient.user.username;
26612665
this.userid = WebSocketClient.user._id;
2666+
var users: NoderedUser[] = await this.api.Query("users", { _id: this.userid }, null, null, 1);
2667+
this.user = NoderedUser.assign(users[0]);
2668+
this.name = users[0].username;
26622669
} else {
2663-
var users = await this.api.Query("users", { _id: this.userid }, null, null, 1);
2670+
var users: NoderedUser[] = await this.api.Query("users", { _id: this.userid }, null, null, 1);
26642671
if (users.length == 0) {
26652672
this.instancestatus = "Unknown id!";
26662673
return;
26672674
}
2675+
this.user = NoderedUser.assign(users[0]);
26682676
this.name = users[0].username;
26692677
}
2678+
if (this.user.nodered != null && this.user.nodered.resources != null && this.user.nodered.resources.limits != null) {
2679+
this.limitsmemory = this.user.nodered.resources.limits.memory;
2680+
}
2681+
console.log(this.limitsmemory);
26702682
this.name = this.name.split("@").join("").split(".").join("");
26712683
this.name = this.name.toLowerCase();
26722684
this.noderedurl = "https://" + WebSocketClient.nodered_domain_schema.replace("$nodered_id$", this.name);
26732685
// // this.GetNoderedInstance();
26742686
this.GetNoderedInstance();
26752687
});
26762688
}
2689+
async save() {
2690+
if (this.limitsmemory != "") {
2691+
if (this.user.nodered == null) this.user.nodered = new noderedconfig();
2692+
if (this.user.nodered.resources == null) this.user.nodered.resources = new resources();
2693+
if (this.user.nodered.resources.limits == null) this.user.nodered.resources.limits = new resourcevalues();
2694+
if (this.user.nodered.resources.limits.memory != this.limitsmemory) {
2695+
this.user.nodered.resources.limits.memory = this.limitsmemory;
2696+
}
2697+
} else {
2698+
if (this.user.nodered != null && this.user.nodered.resources != null && this.user.nodered.resources.limits != null) {
2699+
if (this.limitsmemory != this.user.nodered.resources.limits.memory) {
2700+
this.user.nodered.resources.limits.memory = this.limitsmemory;
2701+
}
2702+
}
2703+
}
2704+
this.loading = true;
2705+
this.messages += 'Updating ' + this.user.name + "\n";
2706+
if (!this.$scope.$$phase) { this.$scope.$apply(); }
2707+
await this.api.Update("users", this.user);
2708+
this.loading = false;
2709+
this.messages += 'update complete\n';
2710+
if (!this.$scope.$$phase) { this.$scope.$apply(); }
2711+
}
26772712
async GetNoderedInstance() {
26782713
try {
26792714
this.instancestatus = "fetching status";
@@ -2696,6 +2731,7 @@ module openflow {
26962731
this.instancestatus = "";
26972732
console.error(error);
26982733
}
2734+
this.loading = false;
26992735
if (!this.$scope.$$phase) { this.$scope.$apply(); }
27002736
}
27012737
async GetNoderedInstanceLog() {

OpenFlow/src/public/Entities.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,33 @@ module openflow {
178178
this._type = "user";
179179
}
180180
}
181+
export class resourcevalues {
182+
public cpu: string;
183+
public memory: string;
184+
}
185+
export class resources {
186+
public limits: resourcevalues;
187+
public requests: resourcevalues;
188+
}
189+
export class noderedconfig {
190+
public resources: resources;
191+
public api_allow_anonymous: boolean = false;
192+
public queue_prefix: string = "";
193+
}
194+
export class NoderedUser extends TokenUser {
195+
public nodered: noderedconfig;
196+
constructor(name: string, public username: string) {
197+
super(name, username);
198+
}
199+
static assign<NoderedUser>(o: any): NoderedUser {
200+
var res = Object.assign(new NoderedUser(o.name, o.username), o);
201+
if (res.nodered === null || res.nodered === undefined) {
202+
res.nodered = new noderedconfig();
203+
}
204+
return res;
205+
}
206+
207+
}
181208
export class Rolemember {
182209
constructor(name: string, _id: string) {
183210
this.name = name;

OpenFlow/src/public/Nodered.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,36 @@ <h1 translate lib="web">sockets</h1>
4949
</div>
5050
</div>
5151
</section>
52+
<section ng-show="menuctrl.hasrole('admins')">
53+
<div class="form-group">
54+
<label class="col-sm-3 control-label"><span translate lib="web">api_allow_anonymous</span>: </label>
55+
<div class="col-sm-9">
56+
<input type="checkbox" class='form-control' ng-model="ctrl.user.nodered.api_allow_anonymous"></input>
57+
</div>
58+
</div>
59+
<div class="form-group">
60+
<label class="col-sm-3 control-label"><span translate lib="web">memory</span>: </label>
61+
<div class="col-sm-9">
62+
<select class="form-control" ng-model="ctrl.limitsmemory">
63+
<option value="">Fair usage</option>
64+
<option value="256Mi">512Mi</option>
65+
<option value="512Mi">512Mi</option>
66+
<option value="1Gi">1Gi</option>
67+
<option value="1536Mi">1,5Gi</option>
68+
<option value="2Gi">2Gi</option>
69+
<option value="2560Mi">5,5Gi</option>
70+
<option value="3Gi">3Gi</option>
71+
</select>
72+
</div>
73+
</div>
74+
<div class="form-group">
75+
<div class="col-sm-offset-3 col-sm-9">
76+
<button type="button" ng-disabled="ctrl.loading==true" class="btn btn-success" translate lib="web"
77+
ng-click="ctrl.save()">save</button>
78+
</div>
79+
</div>
80+
</section>
81+
5282

5383
<pre>{{ ctrl.messages }}</pre>
5484
<p>If this is your first time creating a NodeRed instance, please signout and signin again, to refresh group

OpenFlowNodeRED/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"cookie-parser": "^1.4.4",
2222
"cookie-session": "^1.3.3",
2323
"express": "^4.17.1",
24+
"google-auth-library": "^6.0.0",
2425
"gulp-shell": "^0.7.1",
2526
"jsonwebtoken": "^8.5.1",
2627
"morgan": "^1.9.1",
@@ -34,4 +35,4 @@
3435
"smtp-server": "^3.5.0",
3536
"winston": "^3.2.1"
3637
}
37-
}
38+
}

OpenFlowNodeRED/src/Config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export class Config {
99
public static nodered_sa: string = Config.getEnv("nodered_sa", "");
1010
public static queue_prefix: string = Config.getEnv("queue_prefix", "");
1111

12+
public static NODE_ENV: string = Config.getEnv("NODE_ENV", "development");
13+
1214
public static saml_federation_metadata: string = Config.getEnv("saml_federation_metadata", "");
1315
public static saml_issuer: string = Config.getEnv("saml_issuer", "");
1416
public static saml_entrypoint: string = Config.getEnv("saml_entrypoint", "");
@@ -23,9 +25,6 @@ export class Config {
2325
public static noderedusers: string = Config.getEnv("noderedusers", "");
2426
public static noderedadmins: string = Config.getEnv("noderedadmins", "");
2527

26-
27-
28-
2928
public static api_ws_url: string = Config.getEnv("api_ws_url", "ws://localhost:3000");
3029
public static amqp_url: string = Config.getEnv("amqp_url", "amqp://localhost");
3130

0 commit comments

Comments
 (0)