@celljs/ta-gateway 4.16.0 → 4.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +5 -2
- package/README.md +5 -2
- package/esm/node/docker-engine.d.ts +5 -1
- package/esm/node/docker-engine.js +7 -1
- package/esm/node/docker-sandbox.d.ts +10 -1
- package/esm/node/docker-sandbox.js +24 -5
- package/esm/node/egress-proxy.d.ts +98 -0
- package/esm/node/egress-proxy.js +428 -0
- package/esm/node/environments.d.ts +5 -2
- package/esm/node/environments.js +4 -2
- package/esm/node/gateway-cli.js +12 -5
- package/esm/node/gateway-policy.d.ts +6 -1
- package/esm/node/gateway-policy.js +10 -1
- package/esm/node/gateway-quotas.d.ts +6 -1
- package/esm/node/gateway-quotas.js +17 -0
- package/esm/node/gateway-server.d.ts +31 -0
- package/esm/node/gateway-server.js +131 -13
- package/esm/node/gateway-service-util.d.ts +41 -0
- package/esm/node/gateway-service-util.js +178 -0
- package/esm/node/gateway-service.d.ts +5 -41
- package/esm/node/gateway-service.js +19 -173
- package/esm/node/gateway-socket.d.ts +1 -1
- package/esm/node/gateway-socket.js +1 -1
- package/esm/node/index.d.ts +4 -0
- package/esm/node/index.js +4 -0
- package/esm/node/kubernetes-api.d.ts +35 -0
- package/esm/node/kubernetes-api.js +115 -0
- package/esm/node/kubernetes-sandbox.d.ts +198 -0
- package/esm/node/kubernetes-sandbox.js +279 -0
- package/esm/node/sandbox-provider.d.ts +11 -1
- package/esm/node/sandbox-provider.js +2 -1
- package/esm/node/tenant-store.d.ts +15 -0
- package/esm/node/tenant-store.js +15 -0
- package/lib/node/docker-engine.d.ts +5 -1
- package/lib/node/docker-engine.js +7 -1
- package/lib/node/docker-sandbox.d.ts +10 -1
- package/lib/node/docker-sandbox.js +24 -5
- package/lib/node/egress-proxy.d.ts +99 -0
- package/lib/node/egress-proxy.js +433 -0
- package/lib/node/environments.d.ts +5 -2
- package/lib/node/environments.js +4 -2
- package/lib/node/gateway-cli.js +12 -5
- package/lib/node/gateway-policy.d.ts +6 -1
- package/lib/node/gateway-policy.js +10 -1
- package/lib/node/gateway-quotas.d.ts +6 -1
- package/lib/node/gateway-quotas.js +17 -0
- package/lib/node/gateway-server.d.ts +31 -0
- package/lib/node/gateway-server.js +129 -11
- package/lib/node/gateway-service-util.d.ts +42 -0
- package/lib/node/gateway-service-util.js +182 -0
- package/lib/node/gateway-service.d.ts +5 -41
- package/lib/node/gateway-service.js +41 -196
- package/lib/node/gateway-socket.d.ts +1 -1
- package/lib/node/gateway-socket.js +10 -10
- package/lib/node/index.d.ts +4 -0
- package/lib/node/index.js +4 -0
- package/lib/node/kubernetes-api.d.ts +36 -0
- package/lib/node/kubernetes-api.js +120 -0
- package/lib/node/kubernetes-sandbox.d.ts +199 -0
- package/lib/node/kubernetes-sandbox.js +283 -0
- package/lib/node/sandbox-provider.d.ts +11 -1
- package/lib/node/sandbox-provider.js +2 -1
- package/lib/node/tenant-store.d.ts +15 -0
- package/lib/node/tenant-store.js +15 -0
- package/package.json +4 -4
|
@@ -1,181 +1,12 @@
|
|
|
1
1
|
import { AccessUtil } from '@celljs/ta-host/lib/node/sign-in/access-store';
|
|
2
|
-
import {
|
|
2
|
+
import { TaErrorCode, } from '@celljs/ta-protocol';
|
|
3
3
|
import { Redactor } from '@celljs/ta-protocol/lib/common/redactor';
|
|
4
|
+
import { EgressConfigError, EgressProxyUtil } from './egress-proxy.js';
|
|
4
5
|
import { GatewayPolicyUtil, PolicyError } from './gateway-policy.js';
|
|
6
|
+
import { GatewayCallError, GatewayServiceUtil } from './gateway-service-util.js';
|
|
5
7
|
import { GatewayUsageUtil } from './gateway-usage.js';
|
|
6
8
|
import { ModelProxyUtil } from './model-proxy.js';
|
|
7
|
-
import { TaSandboxUtil } from './ta-sandbox.js';
|
|
8
|
-
export class GatewayCallError extends Error {
|
|
9
|
-
constructor(message, code, hint) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.code = code;
|
|
12
|
-
this.hint = hint;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
9
|
const DAY = 24 * 3600000;
|
|
16
|
-
export var GatewayServiceUtil;
|
|
17
|
-
(function (GatewayServiceUtil) {
|
|
18
|
-
GatewayServiceUtil.DEFAULT_LOG_LINES = 200;
|
|
19
|
-
GatewayServiceUtil.MAX_LOG_LINES = 2000;
|
|
20
|
-
/**
|
|
21
|
-
* Who may call each method: `anyone` signed in (about their own Ta, their own sign-ins), `admin` (owners too),
|
|
22
|
-
* `owner`. A method not here is no method. What an admin may do to another account is checked again by its role
|
|
23
|
-
* (GatewayService.manage).
|
|
24
|
-
*/
|
|
25
|
-
GatewayServiceUtil.RULES = {
|
|
26
|
-
TaGatewayServer: {
|
|
27
|
-
getGateway: 'anyone',
|
|
28
|
-
setEnvironment: 'anyone',
|
|
29
|
-
restartEnvironment: 'anyone',
|
|
30
|
-
listTenants: 'admin',
|
|
31
|
-
getConsole: 'admin',
|
|
32
|
-
getTenantAgents: 'admin',
|
|
33
|
-
getAgentActivity: 'admin',
|
|
34
|
-
getTenantLogs: 'admin',
|
|
35
|
-
getUsageTrend: 'admin',
|
|
36
|
-
getPolicy: 'admin',
|
|
37
|
-
getAudit: 'admin',
|
|
38
|
-
startTenant: 'admin',
|
|
39
|
-
stopTenant: 'admin',
|
|
40
|
-
restartTenant: 'admin',
|
|
41
|
-
createSignInLink: 'admin',
|
|
42
|
-
setTenantLimits: 'admin',
|
|
43
|
-
grantMinutes: 'admin',
|
|
44
|
-
setSuspended: 'admin',
|
|
45
|
-
setDefaultLimits: 'owner',
|
|
46
|
-
setRole: 'owner',
|
|
47
|
-
createAccessToken: 'anyone',
|
|
48
|
-
},
|
|
49
|
-
TaAccessServer: {
|
|
50
|
-
getAccess: 'anyone',
|
|
51
|
-
signOut: 'anyone',
|
|
52
|
-
bindUrl: 'admin',
|
|
53
|
-
allow: 'admin',
|
|
54
|
-
dismiss: 'admin',
|
|
55
|
-
disallow: 'admin',
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
/** What an administrator did or looked at of others: kept in the audit. */
|
|
59
|
-
GatewayServiceUtil.AUDITED = new Set([
|
|
60
|
-
'startTenant',
|
|
61
|
-
'stopTenant',
|
|
62
|
-
'restartTenant',
|
|
63
|
-
'getTenantLogs',
|
|
64
|
-
'getAgentActivity',
|
|
65
|
-
'createSignInLink',
|
|
66
|
-
'setTenantLimits',
|
|
67
|
-
'grantMinutes',
|
|
68
|
-
'setSuspended',
|
|
69
|
-
'setDefaultLimits',
|
|
70
|
-
'setRole',
|
|
71
|
-
'allow',
|
|
72
|
-
'dismiss',
|
|
73
|
-
'disallow',
|
|
74
|
-
]);
|
|
75
|
-
/** By name, own keys only: a method such as `toString` is no rule. */
|
|
76
|
-
const BY_SERVICE = new Map(Object.entries(GatewayServiceUtil.RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
|
|
77
|
-
function need(service, name) {
|
|
78
|
-
var _a;
|
|
79
|
-
return (_a = BY_SERVICE.get(service)) === null || _a === void 0 ? void 0 : _a.get(name);
|
|
80
|
-
}
|
|
81
|
-
GatewayServiceUtil.need = need;
|
|
82
|
-
function allowed(role, need) {
|
|
83
|
-
return need === 'anyone' || GatewayPolicyUtil.rank(role) >= GatewayPolicyUtil.rank(need);
|
|
84
|
-
}
|
|
85
|
-
GatewayServiceUtil.allowed = allowed;
|
|
86
|
-
/** As many lines as asked, within bounds. */
|
|
87
|
-
function lines(value) {
|
|
88
|
-
const count = Math.floor(Number(value !== null && value !== void 0 ? value : GatewayServiceUtil.DEFAULT_LOG_LINES));
|
|
89
|
-
return Number.isFinite(count) && count > 0 ? Math.min(count, GatewayServiceUtil.MAX_LOG_LINES) : GatewayServiceUtil.DEFAULT_LOG_LINES;
|
|
90
|
-
}
|
|
91
|
-
GatewayServiceUtil.lines = lines;
|
|
92
|
-
/** Days of a trend, 1 to 31. */
|
|
93
|
-
function days(value) {
|
|
94
|
-
const count = Math.floor(Number(value !== null && value !== void 0 ? value : 7));
|
|
95
|
-
return Number.isFinite(count) ? Math.min(31, Math.max(1, count)) : 7;
|
|
96
|
-
}
|
|
97
|
-
GatewayServiceUtil.days = days;
|
|
98
|
-
/** The services the gateway answers itself, not the Ta of the tenant. */
|
|
99
|
-
GatewayServiceUtil.SERVICES = Object.keys(GatewayServiceUtil.RULES);
|
|
100
|
-
function owns(method) {
|
|
101
|
-
return typeof method === 'string' && GatewayServiceUtil.SERVICES.includes(method.split('/')[0]);
|
|
102
|
-
}
|
|
103
|
-
GatewayServiceUtil.owns = owns;
|
|
104
|
-
/** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
|
|
105
|
-
function mayOwn(text) {
|
|
106
|
-
return GatewayServiceUtil.SERVICES.some(service => text.includes(`"${service}/`));
|
|
107
|
-
}
|
|
108
|
-
GatewayServiceUtil.mayOwn = mayOwn;
|
|
109
|
-
/** The calls that send the agents a message, which the limit of messages a minute counts. */
|
|
110
|
-
GatewayServiceUtil.PROMPTS = ['TaServer/prompt', 'TaServer/steer', 'TaServer/followUp'];
|
|
111
|
-
function mayPrompt(text) {
|
|
112
|
-
return GatewayServiceUtil.PROMPTS.some(method => text.includes(`"${method}"`));
|
|
113
|
-
}
|
|
114
|
-
GatewayServiceUtil.mayPrompt = mayPrompt;
|
|
115
|
-
/** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
|
|
116
|
-
function error(id, message, code, hint) {
|
|
117
|
-
return JSON.stringify({ jsonrpc: '2.0', id, error: { code: TA_RPC_ERROR_CODE, message, data: { kind: 'application', data: { code, hint } } } });
|
|
118
|
-
}
|
|
119
|
-
GatewayServiceUtil.error = error;
|
|
120
|
-
function result(id, result) {
|
|
121
|
-
return JSON.stringify({ jsonrpc: '2.0', id, result: result !== null && result !== void 0 ? result : null });
|
|
122
|
-
}
|
|
123
|
-
GatewayServiceUtil.result = result;
|
|
124
|
-
function invalid(message, hint) {
|
|
125
|
-
return new GatewayCallError(message, TaErrorCode.INVALID_ARGUMENT, hint);
|
|
126
|
-
}
|
|
127
|
-
function forbidden(message, hint) {
|
|
128
|
-
return new GatewayCallError(message, TaErrorCode.FORBIDDEN, hint);
|
|
129
|
-
}
|
|
130
|
-
GatewayServiceUtil.forbidden = forbidden;
|
|
131
|
-
function choice(value, own, providers) {
|
|
132
|
-
var _a;
|
|
133
|
-
const given = (value !== null && value !== void 0 ? value : {});
|
|
134
|
-
if (typeof given.provider === 'string') {
|
|
135
|
-
if (!providers.includes(given.provider)) {
|
|
136
|
-
throw invalid(`The gateway has no provider ${given.provider} for you.`, 'Pick one of the options of getGateway.');
|
|
137
|
-
}
|
|
138
|
-
return { provider: given.provider };
|
|
139
|
-
}
|
|
140
|
-
if (given.type === 'ta') {
|
|
141
|
-
if (!own) {
|
|
142
|
-
throw invalid('This gateway runs your Ta in its own sandboxes only.', 'Pick one of the options of getGateway.');
|
|
143
|
-
}
|
|
144
|
-
let url;
|
|
145
|
-
try {
|
|
146
|
-
url = TaSandboxUtil.url(String((_a = given.url) !== null && _a !== void 0 ? _a : ''));
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
throw invalid(error.message, 'Write the address its Ta service is reached at, such as https://ta.example.com.');
|
|
150
|
-
}
|
|
151
|
-
if (typeof given.token !== 'string' || !given.token.trim()) {
|
|
152
|
-
throw invalid('A Ta service of your own is reached with its access token.', 'Copy it from ~/.ta/agent/serve.json there, or the link ta serve printed.');
|
|
153
|
-
}
|
|
154
|
-
return { type: 'ta', url, token: given.token.trim(), name: typeof given.name === 'string' && given.name.trim() ? given.name.trim() : undefined };
|
|
155
|
-
}
|
|
156
|
-
throw invalid('No environment was named.', 'Pass { provider } or { type: "ta", url, token }.');
|
|
157
|
-
}
|
|
158
|
-
GatewayServiceUtil.choice = choice;
|
|
159
|
-
/** What the audit says of the arguments of a call: its numbers, flags and roles, never more. */
|
|
160
|
-
function detail(method, params) {
|
|
161
|
-
const [, second] = params;
|
|
162
|
-
switch (method) {
|
|
163
|
-
case 'setTenantLimits':
|
|
164
|
-
case 'setDefaultLimits':
|
|
165
|
-
return JSON.stringify(method === 'setDefaultLimits' ? params[0] : second);
|
|
166
|
-
case 'grantMinutes':
|
|
167
|
-
case 'getTenantLogs':
|
|
168
|
-
return String(second !== null && second !== void 0 ? second : '');
|
|
169
|
-
case 'setSuspended':
|
|
170
|
-
return second ? 'suspended' : 'let go';
|
|
171
|
-
case 'setRole':
|
|
172
|
-
return String(second);
|
|
173
|
-
default:
|
|
174
|
-
return undefined;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
GatewayServiceUtil.detail = detail;
|
|
178
|
-
})(GatewayServiceUtil || (GatewayServiceUtil = {}));
|
|
179
10
|
/**
|
|
180
11
|
* The calls the gateway answers on the WebSocket of a browser: TaGatewayServer, and TaAccessServer, whose
|
|
181
12
|
* accounts are those that may have a tenant here. Each method needs a role (GatewayServiceUtil.RULES); what an
|
|
@@ -184,6 +15,7 @@ export var GatewayServiceUtil;
|
|
|
184
15
|
export class GatewayService {
|
|
185
16
|
constructor(parts) {
|
|
186
17
|
this.models = parts.models;
|
|
18
|
+
this.egress = parts.egress;
|
|
187
19
|
this.flow = parts.flow;
|
|
188
20
|
this.tenants = parts.tenants;
|
|
189
21
|
this.environments = parts.environments;
|
|
@@ -235,6 +67,7 @@ export class GatewayService {
|
|
|
235
67
|
setSuspended: () => this.setSuspended(caller, first, second),
|
|
236
68
|
setDefaultLimits: () => this.setDefaultLimits(first),
|
|
237
69
|
setRole: () => this.setRole(caller, first, second),
|
|
70
|
+
setEgressHosts: () => this.setEgressHosts(first),
|
|
238
71
|
createAccessToken: () => this.accessToken(caller, first),
|
|
239
72
|
}
|
|
240
73
|
: {
|
|
@@ -352,7 +185,20 @@ export class GatewayService {
|
|
|
352
185
|
};
|
|
353
186
|
}
|
|
354
187
|
policyView() {
|
|
355
|
-
|
|
188
|
+
var _a;
|
|
189
|
+
return this.policy.view(this.environments.settings.idleMinutes, (_a = this.egress) === null || _a === void 0 ? void 0 : _a.settings);
|
|
190
|
+
}
|
|
191
|
+
setEgressHosts(value) {
|
|
192
|
+
try {
|
|
193
|
+
this.policy.setEgressHosts(EgressProxyUtil.hosts(value));
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (error instanceof EgressConfigError) {
|
|
197
|
+
throw new GatewayCallError(error.message, TaErrorCode.INVALID_ARGUMENT, 'Send hosts such as registry.npmjs.org, *.github.com or example.com:8443.');
|
|
198
|
+
}
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
return this.policyView();
|
|
356
202
|
}
|
|
357
203
|
tenant(id) {
|
|
358
204
|
const tenant = typeof id === 'string' ? this.tenants.get(id) : undefined;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
|
|
2
2
|
import type { Environments } from './environments.js';
|
|
3
3
|
import type { GatewayQuotas } from './gateway-quotas.js';
|
|
4
|
-
import {
|
|
4
|
+
import type { GatewayCaller, GatewayService } from './gateway-service.js';
|
|
5
5
|
/**
|
|
6
6
|
* One browser's WebSocket: the calls of the gateway's services answered here, everything else forwarded to
|
|
7
7
|
* the Ta of its tenant, whose token only the gateway has, and back. Until that Ta answers, what the browser
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TA_WEBSOCKET_PATH, TaErrorCode } from '@celljs/ta-protocol';
|
|
2
|
-
import { GatewayCallError, GatewayServiceUtil } from './gateway-service.js';
|
|
2
|
+
import { GatewayCallError, GatewayServiceUtil } from './gateway-service-util.js';
|
|
3
3
|
/** What a browser sends before the Ta of its tenant answers, at most: then it is cut off, to connect again. */
|
|
4
4
|
const MAX_QUEUED = 256;
|
|
5
5
|
/** And as many bytes at most: a message may be 32 MB, and 256 of them would hold the gateway's memory for one browser. */
|
package/esm/node/index.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export * from './command-sandbox.js';
|
|
2
2
|
export * from './docker-engine.js';
|
|
3
3
|
export * from './docker-sandbox.js';
|
|
4
|
+
export * from './egress-proxy.js';
|
|
4
5
|
export * from './environments.js';
|
|
5
6
|
export * from './gateway-cli.js';
|
|
6
7
|
export * from './gateway-server.js';
|
|
7
8
|
export * from './gateway-service.js';
|
|
9
|
+
export * from './gateway-service-util.js';
|
|
8
10
|
export * from './gateway-socket.js';
|
|
11
|
+
export * from './kubernetes-api.js';
|
|
12
|
+
export * from './kubernetes-sandbox.js';
|
|
9
13
|
export * from './local-sandbox.js';
|
|
10
14
|
export * from './sandbox-provider.js';
|
|
11
15
|
export * from './sign-in-links.js';
|
package/esm/node/index.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export * from './command-sandbox.js';
|
|
2
2
|
export * from './docker-engine.js';
|
|
3
3
|
export * from './docker-sandbox.js';
|
|
4
|
+
export * from './egress-proxy.js';
|
|
4
5
|
export * from './environments.js';
|
|
5
6
|
export * from './gateway-cli.js';
|
|
6
7
|
export * from './gateway-server.js';
|
|
7
8
|
export * from './gateway-service.js';
|
|
9
|
+
export * from './gateway-service-util.js';
|
|
8
10
|
export * from './gateway-socket.js';
|
|
11
|
+
export * from './kubernetes-api.js';
|
|
12
|
+
export * from './kubernetes-sandbox.js';
|
|
9
13
|
export * from './local-sandbox.js';
|
|
10
14
|
export * from './sandbox-provider.js';
|
|
11
15
|
export * from './sign-in-links.js';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare class KubernetesError extends Error {
|
|
2
|
+
readonly status: number;
|
|
3
|
+
constructor(status: number, message: string);
|
|
4
|
+
}
|
|
5
|
+
/** The calls a provider makes: a JSON body or text back; KubernetesError for an answer not 2xx. */
|
|
6
|
+
export interface KubernetesCalls {
|
|
7
|
+
call<T = unknown>(method: string, path: string, body?: unknown, type?: string): Promise<T>;
|
|
8
|
+
text(path: string): Promise<string>;
|
|
9
|
+
}
|
|
10
|
+
export interface KubernetesApiOptions {
|
|
11
|
+
/** https://<host>:<port> of the API server. */
|
|
12
|
+
server: string;
|
|
13
|
+
token?: string;
|
|
14
|
+
/** The CA of the server, PEM. */
|
|
15
|
+
ca?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The API of a cluster over HTTPS, without a client library: what a provider of sandboxes needs is a few
|
|
19
|
+
* resources by their paths.
|
|
20
|
+
*/
|
|
21
|
+
export declare class KubernetesApi implements KubernetesCalls {
|
|
22
|
+
protected readonly options: KubernetesApiOptions;
|
|
23
|
+
/** In a pod of the cluster: its service account, and the namespace it runs in. */
|
|
24
|
+
static inCluster(env?: Record<string, string | undefined>, read?: (path: string) => string): KubernetesApiOptions & {
|
|
25
|
+
namespace: string;
|
|
26
|
+
};
|
|
27
|
+
constructor(options: KubernetesApiOptions);
|
|
28
|
+
call<T = unknown>(method: string, path: string, body?: unknown, type?: string): Promise<T>;
|
|
29
|
+
text(path: string): Promise<string>;
|
|
30
|
+
protected send(method: string, path: string, body?: string, type?: string): Promise<string>;
|
|
31
|
+
}
|
|
32
|
+
export declare namespace KubernetesUtil {
|
|
33
|
+
/** A quantity of Kubernetes (`4Gi`, `500M`, `250m`, `123456n`) as a number of its unit (bytes, CPUs). */
|
|
34
|
+
function quantity(value: string | number | undefined): number | undefined;
|
|
35
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { request as httpRequest } from 'node:http';
|
|
3
|
+
import { request as httpsRequest } from 'node:https';
|
|
4
|
+
/** Where a pod finds the API of its cluster: its service account's token, CA and namespace. */
|
|
5
|
+
const ACCOUNT = '/var/run/secrets/kubernetes.io/serviceaccount';
|
|
6
|
+
export class KubernetesError extends Error {
|
|
7
|
+
constructor(status, message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.status = status;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The API of a cluster over HTTPS, without a client library: what a provider of sandboxes needs is a few
|
|
14
|
+
* resources by their paths.
|
|
15
|
+
*/
|
|
16
|
+
export class KubernetesApi {
|
|
17
|
+
/** In a pod of the cluster: its service account, and the namespace it runs in. */
|
|
18
|
+
static inCluster(env = process.env, read = (path) => readFileSync(path, 'utf8')) {
|
|
19
|
+
var _a;
|
|
20
|
+
const host = env.KUBERNETES_SERVICE_HOST;
|
|
21
|
+
if (!host) {
|
|
22
|
+
throw new Error('Not in a pod of Kubernetes (no KUBERNETES_SERVICE_HOST): give "server" and "token" of the cluster.');
|
|
23
|
+
}
|
|
24
|
+
const port = (_a = env.KUBERNETES_SERVICE_PORT) !== null && _a !== void 0 ? _a : '443';
|
|
25
|
+
return {
|
|
26
|
+
server: `https://${host.includes(':') ? `[${host}]` : host}:${port}`,
|
|
27
|
+
token: read(`${ACCOUNT}/token`).trim(),
|
|
28
|
+
ca: read(`${ACCOUNT}/ca.crt`),
|
|
29
|
+
namespace: read(`${ACCOUNT}/namespace`).trim(),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
constructor(options) {
|
|
33
|
+
this.options = options;
|
|
34
|
+
}
|
|
35
|
+
async call(method, path, body, type = 'application/json') {
|
|
36
|
+
const text = await this.send(method, path, body === undefined ? undefined : JSON.stringify(body), type);
|
|
37
|
+
return (text ? JSON.parse(text) : undefined);
|
|
38
|
+
}
|
|
39
|
+
text(path) {
|
|
40
|
+
return this.send('GET', path);
|
|
41
|
+
}
|
|
42
|
+
send(method, path, body, type) {
|
|
43
|
+
const url = new URL(path, this.options.server);
|
|
44
|
+
const options = {
|
|
45
|
+
method,
|
|
46
|
+
headers: {
|
|
47
|
+
accept: 'application/json, */*',
|
|
48
|
+
...(this.options.token ? { authorization: `Bearer ${this.options.token}` } : {}),
|
|
49
|
+
...(body !== undefined ? { 'content-type': type !== null && type !== void 0 ? type : 'application/json', 'content-length': Buffer.byteLength(body) } : {}),
|
|
50
|
+
},
|
|
51
|
+
...(this.options.ca ? { ca: this.options.ca } : {}),
|
|
52
|
+
timeout: 30000,
|
|
53
|
+
};
|
|
54
|
+
const send = url.protocol === 'https:' ? httpsRequest : httpRequest;
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const request = send(url, options, response => {
|
|
57
|
+
const chunks = [];
|
|
58
|
+
response.on('data', chunk => chunks.push(chunk));
|
|
59
|
+
response.on('end', () => {
|
|
60
|
+
var _a, _b;
|
|
61
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
62
|
+
const status = (_a = response.statusCode) !== null && _a !== void 0 ? _a : 0;
|
|
63
|
+
if (status >= 200 && status < 300) {
|
|
64
|
+
resolve(text);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
let message = text.slice(0, 300);
|
|
68
|
+
try {
|
|
69
|
+
message = (_b = JSON.parse(text).message) !== null && _b !== void 0 ? _b : message;
|
|
70
|
+
}
|
|
71
|
+
catch (_c) {
|
|
72
|
+
// Not JSON.
|
|
73
|
+
}
|
|
74
|
+
reject(new KubernetesError(status, `${method} ${url.pathname}: ${status} ${message}`));
|
|
75
|
+
});
|
|
76
|
+
response.on('error', reject);
|
|
77
|
+
});
|
|
78
|
+
request.on('timeout', () => request.destroy(new Error(`${method} ${url.pathname}: no answer in time`)));
|
|
79
|
+
request.on('error', reject);
|
|
80
|
+
request.end(body);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export var KubernetesUtil;
|
|
85
|
+
(function (KubernetesUtil) {
|
|
86
|
+
/** A quantity of Kubernetes (`4Gi`, `500M`, `250m`, `123456n`) as a number of its unit (bytes, CPUs). */
|
|
87
|
+
function quantity(value) {
|
|
88
|
+
if (typeof value === 'number') {
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
const match = /^([0-9.]+)([a-zA-Z]*)$/.exec(String(value !== null && value !== void 0 ? value : '').trim());
|
|
92
|
+
if (!match) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const scale = {
|
|
96
|
+
'': 1,
|
|
97
|
+
n: 1e-9,
|
|
98
|
+
u: 1e-6,
|
|
99
|
+
m: 1e-3,
|
|
100
|
+
k: 1e3,
|
|
101
|
+
K: 1e3,
|
|
102
|
+
M: 1e6,
|
|
103
|
+
G: 1e9,
|
|
104
|
+
T: 1e12,
|
|
105
|
+
Ki: 2 ** 10,
|
|
106
|
+
Mi: 2 ** 20,
|
|
107
|
+
Gi: 2 ** 30,
|
|
108
|
+
Ti: 2 ** 40,
|
|
109
|
+
};
|
|
110
|
+
const factor = scale[match[2]];
|
|
111
|
+
return factor === undefined ? undefined : Number(match[1]) * factor;
|
|
112
|
+
}
|
|
113
|
+
KubernetesUtil.quantity = quantity;
|
|
114
|
+
})(KubernetesUtil || (KubernetesUtil = {}));
|
|
115
|
+
//# sourceMappingURL=kubernetes-api.js.map
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { KubernetesApi, type KubernetesCalls } from './kubernetes-api.js';
|
|
2
|
+
import { type SandboxEndpoint, type SandboxProvider, type SandboxProviderConfig, type SandboxState, type SandboxTenant, type SandboxUsage } from './sandbox-provider.js';
|
|
3
|
+
export interface KubernetesSandboxOptions {
|
|
4
|
+
image: string;
|
|
5
|
+
imagePullPolicy?: string;
|
|
6
|
+
namespace: string;
|
|
7
|
+
/** Each sandbox at most: a quantity of memory (`4Gi`), CPUs. */
|
|
8
|
+
memory: string;
|
|
9
|
+
cpus: number;
|
|
10
|
+
/** The volume of its home: its size, and the class of storage (the cluster's default without one). */
|
|
11
|
+
storage: string;
|
|
12
|
+
storageClass?: string;
|
|
13
|
+
/** gVisor, Kata: the isolation of the cluster's choice, a RuntimeClass. */
|
|
14
|
+
runtimeClass?: string;
|
|
15
|
+
/** Names of the Sandbox and its Secret: `<prefix><tenant>`. */
|
|
16
|
+
prefix: string;
|
|
17
|
+
/** More of the environment of every sandbox. */
|
|
18
|
+
env: Record<string, string>;
|
|
19
|
+
nodeSelector?: Record<string, string>;
|
|
20
|
+
imagePullSecrets?: string[];
|
|
21
|
+
/** The domain of the cluster's services, for the address of a sandbox. */
|
|
22
|
+
clusterDomain: string;
|
|
23
|
+
}
|
|
24
|
+
/** What the API answers of a Sandbox, as far as the provider reads it. */
|
|
25
|
+
interface SandboxObject {
|
|
26
|
+
metadata: {
|
|
27
|
+
name: string;
|
|
28
|
+
uid?: string;
|
|
29
|
+
annotations?: Record<string, string>;
|
|
30
|
+
};
|
|
31
|
+
spec: {
|
|
32
|
+
operatingMode?: 'Running' | 'Suspended';
|
|
33
|
+
};
|
|
34
|
+
status?: {
|
|
35
|
+
serviceFQDN?: string;
|
|
36
|
+
conditions?: {
|
|
37
|
+
type: string;
|
|
38
|
+
status: string;
|
|
39
|
+
reason?: string;
|
|
40
|
+
message?: string;
|
|
41
|
+
}[];
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export declare namespace KubernetesSandboxUtil {
|
|
45
|
+
/** The annotation of a Sandbox and its pod that tells the environment the gateway gave it (as `cell.ta.env` of docker). */
|
|
46
|
+
const ENV_ANNOTATION = "cell.ta/env";
|
|
47
|
+
const COMPONENT = "ta-sandbox";
|
|
48
|
+
function options(config: SandboxProviderConfig, inCluster?: () => {
|
|
49
|
+
namespace: string;
|
|
50
|
+
}): KubernetesSandboxOptions;
|
|
51
|
+
/** The API: `server` and `token` (`$NAME`: of the gateway's environment) of the configuration, else the pod's own. */
|
|
52
|
+
function api(config: SandboxProviderConfig, env?: Record<string, string | undefined>): KubernetesApi;
|
|
53
|
+
function ready(sandbox: SandboxObject | undefined): boolean;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* `kubernetes`: a Sandbox of kubernetes-sigs/agent-sandbox per tenant (docs/design/ta-deploy.md 6): a pod of ta serve,
|
|
57
|
+
* its home in a volume of its own that stays while it is suspended, a headless Service for a stable address. Stopping
|
|
58
|
+
* suspends it (the pod goes, the volume stays); its token and the environment of the gateway are in a Secret of its
|
|
59
|
+
* own, not in the Sandbox, which more people may read.
|
|
60
|
+
*/
|
|
61
|
+
export declare class KubernetesSandbox implements SandboxProvider {
|
|
62
|
+
readonly id: string;
|
|
63
|
+
protected readonly options: KubernetesSandboxOptions;
|
|
64
|
+
readonly type = "kubernetes";
|
|
65
|
+
readonly name: string;
|
|
66
|
+
readonly gatewayUrl?: string;
|
|
67
|
+
/** A first pull of the image and a volume to bind take longer than a container. */
|
|
68
|
+
readonly readyTimeoutMs: number;
|
|
69
|
+
protected readonly api: KubernetesCalls;
|
|
70
|
+
constructor(id: string, config: SandboxProviderConfig, api?: KubernetesCalls, options?: KubernetesSandboxOptions);
|
|
71
|
+
start(tenant: SandboxTenant): Promise<SandboxEndpoint>;
|
|
72
|
+
stop(tenant: SandboxTenant): Promise<void>;
|
|
73
|
+
state(tenant: SandboxTenant): Promise<SandboxState>;
|
|
74
|
+
/** From metrics.k8s.io, when the cluster has metrics-server. */
|
|
75
|
+
usage(tenant: SandboxTenant): Promise<SandboxUsage | undefined>;
|
|
76
|
+
logs(tenant: SandboxTenant, lines: number): Promise<string>;
|
|
77
|
+
/** The Sandbox of a tenant, as the API is given it. */
|
|
78
|
+
manifest(tenant: SandboxTenant): {
|
|
79
|
+
apiVersion: string;
|
|
80
|
+
kind: string;
|
|
81
|
+
metadata: {
|
|
82
|
+
name: string;
|
|
83
|
+
labels: {
|
|
84
|
+
'app.kubernetes.io/name': string;
|
|
85
|
+
'app.kubernetes.io/component': string;
|
|
86
|
+
'cell.ta/tenant': string;
|
|
87
|
+
};
|
|
88
|
+
annotations: {
|
|
89
|
+
"cell.ta/env": string;
|
|
90
|
+
'cell.ta/account': string;
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
spec: {
|
|
94
|
+
operatingMode: string;
|
|
95
|
+
service: boolean;
|
|
96
|
+
failedPodPolicy: string;
|
|
97
|
+
podTemplate: {
|
|
98
|
+
metadata: {
|
|
99
|
+
labels: {
|
|
100
|
+
'app.kubernetes.io/name': string;
|
|
101
|
+
'app.kubernetes.io/component': string;
|
|
102
|
+
'cell.ta/tenant': string;
|
|
103
|
+
};
|
|
104
|
+
annotations: {
|
|
105
|
+
"cell.ta/env": string;
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
spec: {
|
|
109
|
+
automountServiceAccountToken: boolean;
|
|
110
|
+
enableServiceLinks: boolean;
|
|
111
|
+
hostname: string;
|
|
112
|
+
securityContext: {
|
|
113
|
+
runAsUser: number;
|
|
114
|
+
runAsGroup: number;
|
|
115
|
+
fsGroup: number;
|
|
116
|
+
seccompProfile: {
|
|
117
|
+
type: string;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
runtimeClassName?: string | undefined;
|
|
121
|
+
nodeSelector?: Record<string, string> | undefined;
|
|
122
|
+
imagePullSecrets?: {
|
|
123
|
+
name: string;
|
|
124
|
+
}[] | undefined;
|
|
125
|
+
containers: {
|
|
126
|
+
name: string;
|
|
127
|
+
image: string;
|
|
128
|
+
imagePullPolicy?: string | undefined;
|
|
129
|
+
workingDir: string;
|
|
130
|
+
command: string[];
|
|
131
|
+
envFrom: {
|
|
132
|
+
secretRef: {
|
|
133
|
+
name: string;
|
|
134
|
+
};
|
|
135
|
+
}[];
|
|
136
|
+
ports: {
|
|
137
|
+
name: string;
|
|
138
|
+
containerPort: number;
|
|
139
|
+
}[];
|
|
140
|
+
resources: {
|
|
141
|
+
limits: {
|
|
142
|
+
memory: string;
|
|
143
|
+
cpu: string;
|
|
144
|
+
};
|
|
145
|
+
requests: {
|
|
146
|
+
memory: string;
|
|
147
|
+
cpu: string;
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
securityContext: {
|
|
151
|
+
capabilities: {
|
|
152
|
+
drop: string[];
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
readinessProbe: {
|
|
156
|
+
httpGet: {
|
|
157
|
+
path: string;
|
|
158
|
+
port: number;
|
|
159
|
+
};
|
|
160
|
+
periodSeconds: number;
|
|
161
|
+
failureThreshold: number;
|
|
162
|
+
};
|
|
163
|
+
volumeMounts: {
|
|
164
|
+
name: string;
|
|
165
|
+
mountPath: string;
|
|
166
|
+
}[];
|
|
167
|
+
}[];
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
volumeClaimTemplates: {
|
|
171
|
+
metadata: {
|
|
172
|
+
name: string;
|
|
173
|
+
labels: {
|
|
174
|
+
'app.kubernetes.io/name': string;
|
|
175
|
+
'app.kubernetes.io/component': string;
|
|
176
|
+
'cell.ta/tenant': string;
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
spec: {
|
|
180
|
+
accessModes: string[];
|
|
181
|
+
resources: {
|
|
182
|
+
requests: {
|
|
183
|
+
storage: string;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
storageClassName?: string | undefined;
|
|
187
|
+
};
|
|
188
|
+
}[];
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
/** Its token and the environment of the gateway, owned by its Sandbox, which takes it along when it goes. */
|
|
192
|
+
protected putSecret(tenant: SandboxTenant, sandbox: SandboxObject): Promise<void>;
|
|
193
|
+
protected deletePod(name: string): Promise<void>;
|
|
194
|
+
protected read(name: string): Promise<SandboxObject | undefined>;
|
|
195
|
+
protected path(name?: string): string;
|
|
196
|
+
protected resourceName(tenant: SandboxTenant): string;
|
|
197
|
+
}
|
|
198
|
+
export {};
|