@appsemble/node-utils 0.29.5 → 0.29.6
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.md +3 -3
- package/container/helpers.js +3 -3
- package/container/index.d.ts +1 -0
- package/container/index.js +1 -0
- package/container/logs.d.ts +2 -0
- package/container/logs.js +91 -0
- package/package.json +3 -3
- package/server/controllers/action.js +2 -1
- package/server/types.d.ts +1 -0
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#  Appsemble Node Utilities
|
|
2
2
|
|
|
3
3
|
> NodeJS utilities used by Appsemble internally.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@appsemble/node-utils)
|
|
6
|
-
[](https://gitlab.com/appsemble/appsemble/-/releases/0.29.6)
|
|
7
7
|
[](https://prettier.io)
|
|
8
8
|
|
|
9
9
|
## Table of Contents
|
|
@@ -26,5 +26,5 @@ compatibility is not guaranteed.
|
|
|
26
26
|
|
|
27
27
|
## License
|
|
28
28
|
|
|
29
|
-
[LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.29.
|
|
29
|
+
[LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.29.6/LICENSE.md) ©
|
|
30
30
|
[Appsemble](https://appsemble.com)
|
package/container/helpers.js
CHANGED
|
@@ -5,7 +5,7 @@ export const maxCPU = (_a = process.env.MAX_CONTAINER_CPU) !== null && _a !== vo
|
|
|
5
5
|
// In gigabytes
|
|
6
6
|
export const maxMemoryGi = (_b = process.env.MAX_CONTAINER_MEMORY) !== null && _b !== void 0 ? _b : 3;
|
|
7
7
|
export const appIdLabel = 'appId';
|
|
8
|
-
export const resourceDefaults = { memory: '
|
|
8
|
+
export const resourceDefaults = { memory: '128Mi', cpu: '0.1' };
|
|
9
9
|
export function getKubeConfig() {
|
|
10
10
|
const kubeconfig = new KubeConfig();
|
|
11
11
|
kubeconfig.loadFromDefault();
|
|
@@ -15,7 +15,7 @@ export function getKubeConfig() {
|
|
|
15
15
|
}
|
|
16
16
|
export function getContainerNamespace() {
|
|
17
17
|
var _a;
|
|
18
|
-
return (_a = process.env.
|
|
18
|
+
return `companion-containers-${(_a = process.env.SERVICE_NAME) !== null && _a !== void 0 ? _a : 'appsemble'}`;
|
|
19
19
|
}
|
|
20
20
|
export function formatServiceName(containerName, appName, appId) {
|
|
21
21
|
const serviceName = `${containerName}-${appName}-${appId}`.replaceAll(' ', '-').toLowerCase();
|
|
@@ -92,7 +92,7 @@ export function validateContainerResources(resources) {
|
|
|
92
92
|
unit === 'Gi' && parsedValue > maxMemoryGi,
|
|
93
93
|
unit === 'Mi' && parsedValue > maxMemoryGi * 1024,
|
|
94
94
|
];
|
|
95
|
-
if (conditionsMemory.some(
|
|
95
|
+
if (conditionsMemory.some(Boolean)) {
|
|
96
96
|
result.limits.memory = resourceDefaults.memory;
|
|
97
97
|
}
|
|
98
98
|
}
|
package/container/index.d.ts
CHANGED
package/container/index.js
CHANGED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import stream from 'node:stream';
|
|
3
|
+
import { Log } from '@kubernetes/client-node';
|
|
4
|
+
import { getContainerNamespace, getKubeConfig, handleKubernetesError } from './helpers.js';
|
|
5
|
+
import { logger } from '../logger.js';
|
|
6
|
+
function fetchLogs(namespace, podName, containerName) {
|
|
7
|
+
const { kubeconfig } = getKubeConfig();
|
|
8
|
+
const log = new Log(kubeconfig);
|
|
9
|
+
const logStream = new stream.PassThrough();
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
log
|
|
12
|
+
.log(namespace, podName, containerName || '', logStream)
|
|
13
|
+
.then(() => {
|
|
14
|
+
let logs = '';
|
|
15
|
+
logStream.on('data', (chunk) => {
|
|
16
|
+
logs += String(chunk);
|
|
17
|
+
});
|
|
18
|
+
logStream.on('end', () => {
|
|
19
|
+
resolve(logs);
|
|
20
|
+
});
|
|
21
|
+
logStream.on('error', (err) => {
|
|
22
|
+
reject(err);
|
|
23
|
+
});
|
|
24
|
+
})
|
|
25
|
+
.catch((err) => {
|
|
26
|
+
reject(err);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function filterLogEntries(entries, deploymentName, fromAppsemble) {
|
|
31
|
+
let res = entries;
|
|
32
|
+
if (fromAppsemble) {
|
|
33
|
+
res = entries.filter((e) => e.includes(deploymentName));
|
|
34
|
+
}
|
|
35
|
+
return res;
|
|
36
|
+
}
|
|
37
|
+
export async function getLogs(deploymentName, fromAppsemble) {
|
|
38
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
39
|
+
const { coreApi } = getKubeConfig();
|
|
40
|
+
let pods;
|
|
41
|
+
const containerNamespace = getContainerNamespace();
|
|
42
|
+
let appsembleNamespace = 'appsemble';
|
|
43
|
+
const appsembleServiceName = (_a = process.env.SERVICE_NAME) !== null && _a !== void 0 ? _a : 'appsemble';
|
|
44
|
+
logger.silly(`Using service name ${appsembleServiceName}`);
|
|
45
|
+
try {
|
|
46
|
+
const NAMESPACE_FILE_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/namespace';
|
|
47
|
+
const namespace = await readFile(NAMESPACE_FILE_PATH, 'utf8');
|
|
48
|
+
appsembleNamespace = namespace.trim();
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
logger.error('Error fetching namespace name. Using `appsemble` as default');
|
|
52
|
+
logger.error(error);
|
|
53
|
+
}
|
|
54
|
+
logger.silly(`Using namespace ${appsembleNamespace}`);
|
|
55
|
+
try {
|
|
56
|
+
pods = (await coreApi.listNamespacedPod(fromAppsemble ? appsembleNamespace : containerNamespace)).body;
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
handleKubernetesError(error);
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
if (pods.items.length === 0) {
|
|
63
|
+
logger.warn(`No pods found for deployment ${deploymentName} in namespace ${containerNamespace}`);
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
const podLogs = [];
|
|
67
|
+
for (const pod of pods.items) {
|
|
68
|
+
if (((_c = (_b = pod.metadata) === null || _b === void 0 ? void 0 : _b.ownerReferences) === null || _c === void 0 ? void 0 : _c.find((r) => r.kind === 'ReplicaSet')) &&
|
|
69
|
+
((_d = pod.status) === null || _d === void 0 ? void 0 : _d.phase) === 'Running') {
|
|
70
|
+
if (fromAppsemble && !((_e = pod.metadata) === null || _e === void 0 ? void 0 : _e.name.includes(appsembleServiceName))) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (!fromAppsemble && !((_f = pod.metadata) === null || _f === void 0 ? void 0 : _f.name.includes(deploymentName))) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
logger.silly(`Pod: ${(_g = pod.metadata) === null || _g === void 0 ? void 0 : _g.name}, status: ${pod.status}, deployment: ${deploymentName}`);
|
|
77
|
+
try {
|
|
78
|
+
const res = await fetchLogs(fromAppsemble ? appsembleNamespace : containerNamespace, (_h = pod.metadata) === null || _h === void 0 ? void 0 : _h.name, fromAppsemble ? 'appsemble' : deploymentName);
|
|
79
|
+
let entries = res.split('\n');
|
|
80
|
+
logger.silly(`Number of entries found for pod ${(_j = pod.metadata) === null || _j === void 0 ? void 0 : _j.name}: ${entries.length}`);
|
|
81
|
+
entries = filterLogEntries([...entries], deploymentName, fromAppsemble);
|
|
82
|
+
podLogs.push({ fromAppsemble, entries });
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
handleKubernetesError(error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return podLogs;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=logs.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appsemble/node-utils",
|
|
3
|
-
"version": "0.29.
|
|
3
|
+
"version": "0.29.6",
|
|
4
4
|
"description": "NodeJS utilities used by Appsemble internally.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"app",
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
"test": "vitest"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@appsemble/types": "0.29.
|
|
42
|
-
"@appsemble/utils": "0.29.
|
|
41
|
+
"@appsemble/types": "0.29.6",
|
|
42
|
+
"@appsemble/utils": "0.29.6",
|
|
43
43
|
"@formatjs/fast-memoize": "^2.0.0",
|
|
44
44
|
"@fortawesome/fontawesome-free": "^6.0.0",
|
|
45
45
|
"@kubernetes/client-node": "0.21.0",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assertKoaError, createFormData, getRemapperContext, logger, parseServiceUrl, scaleDeployment, setLastRequestAnnotation, throwKoaError, version, waitForPodReadiness, } from '@appsemble/node-utils';
|
|
1
|
+
import { assertKoaError, createFormData, getContainerNamespace, getRemapperContext, logger, parseServiceUrl, scaleDeployment, setLastRequestAnnotation, throwKoaError, version, waitForPodReadiness, } from '@appsemble/node-utils';
|
|
2
2
|
import { defaultLocale, remap } from '@appsemble/utils';
|
|
3
3
|
import axios from 'axios';
|
|
4
4
|
import { get, mapValues, pick } from 'lodash-es';
|
|
@@ -137,6 +137,7 @@ async function handleRequestProxy(ctx, app, action, useBody, options) {
|
|
|
137
137
|
const urlPattern = /^http:\/\/(([\da-z-]+).){2}svc.cluster.local/;
|
|
138
138
|
// Restricting access to only the containers defined by the app
|
|
139
139
|
if (urlPattern.test(String(proxyUrl))) {
|
|
140
|
+
axiosConfig.url = axiosConfig.url.replace(axiosConfig.url.split('.')[1], getContainerNamespace());
|
|
140
141
|
const { appId } = parseServiceUrl(String(proxyUrl));
|
|
141
142
|
if (appId !== String(app.id)) {
|
|
142
143
|
throwKoaError(ctx, 403, 'Forbidden');
|