mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Get tests passing
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
2de306810e
commit
d293554c42
37 files changed
+612
-225
No files matched your search
@@ -3,7 +3,7 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getDiForUnitTesting } from "../getDiForUnitTesting";
|
||||
import { KubeconfigManager } from "../kubeconfig-manager/kubeconfig-manager";
|
||||
import type { KubeconfigManager } from "../kubeconfig-manager/kubeconfig-manager";
|
||||
import type { Cluster } from "../../common/cluster/cluster";
|
||||
import createKubeconfigManagerInjectable from "../kubeconfig-manager/create-kubeconfig-manager.injectable";
|
||||
import { createClusterInjectionToken } from "../../common/cluster/create-cluster-injection-token";
|
||||
@@ -30,6 +30,7 @@ import removePathInjectable from "../../common/fs/remove.injectable";
|
||||
import pathExistsSyncInjectable from "../../common/fs/path-exists-sync.injectable";
|
||||
import readJsonSyncInjectable from "../../common/fs/read-json-sync.injectable";
|
||||
import writeJsonSyncInjectable from "../../common/fs/write-json-sync.injectable";
|
||||
import lensProxyPortInjectable from "../lens-proxy/lens-proxy-port.injectable";
|
||||
|
||||
const clusterServerUrl = "https://192.168.64.3:8443";
|
||||
|
||||
@@ -90,6 +91,8 @@ describe("kubeconfig manager tests", () => {
|
||||
ensureServer: ensureServerMock,
|
||||
}));
|
||||
|
||||
di.inject(lensProxyPortInjectable).set(9191);
|
||||
|
||||
const createCluster = di.inject(createClusterInjectionToken);
|
||||
|
||||
createKubeconfigManager = di.inject(createKubeconfigManagerInjectable);
|
||||
@@ -102,8 +105,6 @@ describe("kubeconfig manager tests", () => {
|
||||
clusterServerUrl,
|
||||
});
|
||||
|
||||
jest.spyOn(KubeconfigManager.prototype, "resolveProxyUrl", "get").mockReturnValue("https://127.0.0.1:9191/foo");
|
||||
|
||||
kubeConfManager = createKubeconfigManager(clusterFake);
|
||||
});
|
||||
|
||||
@@ -174,7 +175,7 @@ describe("kubeconfig manager tests", () => {
|
||||
beforeEach(async () => {
|
||||
await writeFileMock.resolveSpecific(
|
||||
[
|
||||
"/some-directory-for-temp/kubeconfig-foo",
|
||||
"/some-directory-for-temp/kubeconfig-foo",
|
||||
"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: minikube\nclusters:\n - name: minikube\n cluster:\n certificate-authority-data: PGNhLWRhdGE+\n server: https://127.0.0.1:9191/foo\n insecure-skip-tls-verify: false\ncontexts:\n - name: minikube\n context:\n cluster: minikube\n user: proxy\nusers:\n - name: proxy\n user:\n username: lens\n password: fake\n",
|
||||
],
|
||||
);
|
||||
@@ -303,7 +304,7 @@ describe("kubeconfig manager tests", () => {
|
||||
beforeEach(async () => {
|
||||
await writeFileMock.resolveSpecific(
|
||||
[
|
||||
"/some-directory-for-temp/kubeconfig-foo",
|
||||
"/some-directory-for-temp/kubeconfig-foo",
|
||||
"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: minikube\nclusters:\n - name: minikube\n cluster:\n certificate-authority-data: PGNhLWRhdGE+\n server: https://127.0.0.1:9191/foo\n insecure-skip-tls-verify: false\ncontexts:\n - name: minikube\n context:\n cluster: minikube\n user: proxy\nusers:\n - name: proxy\n user:\n username: lens\n password: fake\n",
|
||||
],
|
||||
);
|
||||
|
||||
@@ -29,6 +29,11 @@ export interface KubeconfigManagerDependencies {
|
||||
certificate: SelfSignedCert;
|
||||
}
|
||||
|
||||
export interface KubeconfigManager {
|
||||
getPath(): Promise<string>;
|
||||
clear(): Promise<void>;
|
||||
}
|
||||
|
||||
export class KubeconfigManager {
|
||||
/**
|
||||
* The path to the temp config file
|
||||
@@ -40,7 +45,7 @@ export class KubeconfigManager {
|
||||
|
||||
protected readonly contextHandler: ClusterContextHandler;
|
||||
|
||||
constructor(private readonly dependencies: KubeconfigManagerDependencies, protected cluster: Cluster) {
|
||||
constructor(private readonly dependencies: KubeconfigManagerDependencies, protected readonly cluster: Cluster) {
|
||||
this.contextHandler = cluster.contextHandler;
|
||||
}
|
||||
|
||||
@@ -87,10 +92,6 @@ export class KubeconfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
get resolveProxyUrl() {
|
||||
return `https://127.0.0.1:${this.dependencies.lensProxyPort.get()}/${this.cluster.id}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new "temporary" kubeconfig that point to the kubectl-proxy.
|
||||
* This way any user of the config does not need to know anything about the auth etc. details.
|
||||
@@ -109,7 +110,7 @@ export class KubeconfigManager {
|
||||
clusters: [
|
||||
{
|
||||
name: contextName,
|
||||
server: this.resolveProxyUrl,
|
||||
server: `http://127.0.0.1:${this.dependencies.lensProxyPort.get()}/${this.cluster.id}`,
|
||||
skipTLSVerify: false,
|
||||
caData: Buffer.from(certificate.cert).toString("base64"),
|
||||
},
|
||||
|
||||
@@ -18,6 +18,15 @@ import getDirnameOfPathInjectable from "../../common/path/get-dirname.injectable
|
||||
import joinPathsInjectable from "../../common/path/join-paths.injectable";
|
||||
import getBasenameOfPathInjectable from "../../common/path/get-basename.injectable";
|
||||
import loggerInjectable from "../../common/logger.injectable";
|
||||
import changePathModeInjectable from "../../common/fs/change-path-mode.injectable";
|
||||
import copyFileInjectable from "../../common/fs/copy-file.injectable";
|
||||
import createWriteFileStreamInjectable from "../../common/fs/create-write-file-stream.injectable";
|
||||
import execFileInjectable from "../../common/fs/exec-file.injectable";
|
||||
import pathExistsInjectable from "../../common/fs/path-exists.injectable";
|
||||
import removePathInjectable from "../../common/fs/remove.injectable";
|
||||
import writeFileInjectable from "../../common/fs/write-file.injectable";
|
||||
import ensureDirectoryInjectable from "../../common/fs/ensure-directory.injectable";
|
||||
import fetchInjectable from "../../common/fetch/fetch.injectable";
|
||||
|
||||
const createKubectlInjectable = getInjectable({
|
||||
id: "create-kubectl",
|
||||
@@ -37,6 +46,15 @@ const createKubectlInjectable = getInjectable({
|
||||
getDirnameOfPath: di.inject(getDirnameOfPathInjectable),
|
||||
joinPaths: di.inject(joinPathsInjectable),
|
||||
getBasenameOfPath: di.inject(getBasenameOfPathInjectable),
|
||||
changePathMode: di.inject(changePathModeInjectable),
|
||||
copyFile: di.inject(copyFileInjectable),
|
||||
createWriteFileStream: di.inject(createWriteFileStreamInjectable),
|
||||
ensureDirectory: di.inject(ensureDirectoryInjectable),
|
||||
execFile: di.inject(execFileInjectable),
|
||||
pathExists: di.inject(pathExistsInjectable),
|
||||
removePath: di.inject(removePathInjectable),
|
||||
writeFile: di.inject(writeFileInjectable),
|
||||
fetch: di.inject(fetchInjectable),
|
||||
};
|
||||
|
||||
return (clusterVersion: string) => new Kubectl(dependencies, clusterVersion);
|
||||
|
||||
+67
-45
@@ -3,23 +3,28 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import fs from "fs";
|
||||
import { promiseExecFile } from "../../common/utils/promise-exec";
|
||||
import { ensureDir, pathExists } from "fs-extra";
|
||||
import * as lockFile from "proper-lockfile";
|
||||
import { SemVer, coerce } from "semver";
|
||||
import { defaultPackageMirror, packageMirrors } from "../../common/user-store/preferences-helpers";
|
||||
import got from "got/dist/source";
|
||||
import { promisify } from "util";
|
||||
import stream from "stream";
|
||||
import { noop } from "lodash/fp";
|
||||
import type { JoinPaths } from "../../common/path/join-paths.injectable";
|
||||
import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable";
|
||||
import type { GetBasenameOfPath } from "../../common/path/get-basename.injectable";
|
||||
import type { NormalizedPlatform } from "../../common/vars/normalized-platform.injectable";
|
||||
import type { Logger } from "../../common/logger";
|
||||
import type { PathExists } from "../../common/fs/path-exists.injectable";
|
||||
import type { ExecFile } from "../../common/fs/exec-file.injectable";
|
||||
import type { EnsureDirectory } from "../../common/fs/ensure-directory.injectable";
|
||||
import { promisify } from "util";
|
||||
import type { ChangePathMode } from "../../common/fs/change-path-mode.injectable";
|
||||
import type { WriteFile } from "../../common/fs/write-file.injectable";
|
||||
import type { CopyFile } from "../../common/fs/copy-file.injectable";
|
||||
import type { CreateWriteFileStream } from "../../common/fs/create-write-file-stream.injectable";
|
||||
import type { Fetch } from "../../common/fetch/fetch.injectable";
|
||||
import type { RemovePath } from "../../common/fs/remove.injectable";
|
||||
|
||||
const initScriptVersionString = "# lens-initscript v3";
|
||||
const pipeline = promisify(stream.pipeline);
|
||||
|
||||
export interface KubectlDependencies {
|
||||
readonly directoryForKubectlBinaries: string;
|
||||
@@ -40,6 +45,15 @@ export interface KubectlDependencies {
|
||||
joinPaths: JoinPaths;
|
||||
getDirnameOfPath: GetDirnameOfPath;
|
||||
getBasenameOfPath: GetBasenameOfPath;
|
||||
removePath: RemovePath;
|
||||
pathExists: PathExists;
|
||||
execFile: ExecFile;
|
||||
ensureDirectory: EnsureDirectory;
|
||||
changePathMode: ChangePathMode;
|
||||
writeFile: WriteFile;
|
||||
copyFile: CopyFile;
|
||||
createWriteFileStream: CreateWriteFileStream;
|
||||
fetch: Fetch;
|
||||
}
|
||||
|
||||
export class Kubectl {
|
||||
@@ -143,37 +157,41 @@ export class Kubectl {
|
||||
}
|
||||
|
||||
public async checkBinary(path: string, checkVersion = true) {
|
||||
const exists = await pathExists(path);
|
||||
const exists = await this.dependencies.pathExists(path);
|
||||
|
||||
if (exists) {
|
||||
try {
|
||||
const args = [
|
||||
"version",
|
||||
"--client",
|
||||
"--output", "json",
|
||||
];
|
||||
const { stdout } = await promiseExecFile(path, args);
|
||||
const output = JSON.parse(stdout);
|
||||
const result = await this.dependencies.execFile(path, [
|
||||
"version",
|
||||
"--client",
|
||||
"--output", "json",
|
||||
]);
|
||||
|
||||
if (!checkVersion) {
|
||||
return true;
|
||||
}
|
||||
let version: string = output.clientVersion.gitVersion;
|
||||
if (!result.callWasSuccessful) {
|
||||
this.dependencies.logger.error(`Local kubectl failed to run properly (${result.error}), removing`);
|
||||
await this.dependencies.removePath(this.path);
|
||||
|
||||
if (version[0] === "v") {
|
||||
version = version.slice(1);
|
||||
}
|
||||
|
||||
if (version === this.kubectlVersion) {
|
||||
this.dependencies.logger.debug(`Local kubectl is version ${this.kubectlVersion}`);
|
||||
|
||||
return true;
|
||||
}
|
||||
this.dependencies.logger.error(`Local kubectl is version ${version}, expected ${this.kubectlVersion}, unlinking`);
|
||||
} catch (error) {
|
||||
this.dependencies.logger.error(`Local kubectl failed to run properly (${error}), unlinking`);
|
||||
return false;
|
||||
}
|
||||
await fs.promises.unlink(this.path);
|
||||
|
||||
const output = JSON.parse(result.response);
|
||||
|
||||
if (!checkVersion) {
|
||||
return true;
|
||||
}
|
||||
let version: string = output.clientVersion.gitVersion;
|
||||
|
||||
if (version[0] === "v") {
|
||||
version = version.slice(1);
|
||||
}
|
||||
|
||||
if (version === this.kubectlVersion) {
|
||||
this.dependencies.logger.debug(`Local kubectl is version ${this.kubectlVersion}`);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
this.dependencies.logger.error(`Local kubectl is version ${version}, expected ${this.kubectlVersion}, removing`);
|
||||
await this.dependencies.removePath(this.path);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -182,11 +200,11 @@ export class Kubectl {
|
||||
protected async checkBundled(): Promise<boolean> {
|
||||
if (this.kubectlVersion === this.dependencies.bundledKubectlVersion) {
|
||||
try {
|
||||
const exist = await pathExists(this.path);
|
||||
const exist = await this.dependencies.pathExists(this.path);
|
||||
|
||||
if (!exist) {
|
||||
await fs.promises.copyFile(this.getBundledPath(), this.path);
|
||||
await fs.promises.chmod(this.path, 0o755);
|
||||
await this.dependencies.copyFile(this.getBundledPath(), this.path);
|
||||
await this.dependencies.changePathMode(this.path, 0o755);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -211,7 +229,7 @@ export class Kubectl {
|
||||
return false;
|
||||
}
|
||||
|
||||
await ensureDir(this.dirname, 0o755);
|
||||
await this.dependencies.ensureDirectory(this.dirname, 0o755);
|
||||
|
||||
try {
|
||||
const release = await lockFile.lock(this.dirname);
|
||||
@@ -253,20 +271,24 @@ export class Kubectl {
|
||||
}
|
||||
|
||||
public async downloadKubectl() {
|
||||
await ensureDir(this.dependencies.getDirnameOfPath(this.path), 0o755);
|
||||
await this.dependencies.ensureDirectory(this.dependencies.getDirnameOfPath(this.path), 0o755);
|
||||
|
||||
this.dependencies.logger.info(`Downloading kubectl ${this.kubectlVersion} from ${this.url} to ${this.path}`);
|
||||
|
||||
const downloadStream = got.stream({ url: this.url, decompress: true });
|
||||
const fileWriteStream = fs.createWriteStream(this.path, { mode: 0o755 });
|
||||
const pipeline = promisify(stream.pipeline);
|
||||
const response = await this.dependencies.fetch(this.url, { compress: true });
|
||||
|
||||
if (!response.body || !response.body.readable) {
|
||||
throw new Error("Body missing or not readable");
|
||||
}
|
||||
|
||||
const fileWriteStream = this.dependencies.createWriteFileStream(this.path, { mode: 0o755 });
|
||||
|
||||
try {
|
||||
await pipeline(downloadStream, fileWriteStream);
|
||||
await fs.promises.chmod(this.path, 0o755);
|
||||
await pipeline(response.body, fileWriteStream);
|
||||
await this.dependencies.changePathMode(this.path, 0o755);
|
||||
this.dependencies.logger.debug("kubectl binary download finished");
|
||||
} catch (error) {
|
||||
await fs.promises.unlink(this.path).catch(noop);
|
||||
await this.dependencies.removePath(this.path);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -332,8 +354,8 @@ export class Kubectl {
|
||||
].join("\n");
|
||||
|
||||
await Promise.all([
|
||||
fs.promises.writeFile(bashScriptPath, bashScript, { mode: 0o644 }),
|
||||
fs.promises.writeFile(zshScriptPath, zshScript, { mode: 0o644 }),
|
||||
this.dependencies.writeFile(bashScriptPath, bashScript, { mode: 0o644 }),
|
||||
this.dependencies.writeFile(zshScriptPath, zshScript, { mode: 0o644 }),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,30 +17,43 @@ const getClusterForRequestInjectable = getInjectable({
|
||||
const getClusterById = di.inject(getClusterByIdInjectable);
|
||||
|
||||
return (req) => {
|
||||
if (!req.headers.host) {
|
||||
const { host } = req.headers;
|
||||
|
||||
if (!host || !req.url) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// lens-server is connecting to 127.0.0.1:<port>/<uid>
|
||||
if (req.url && req.headers.host.startsWith("127.0.0.1")) {
|
||||
const clusterId = req.url.split("/")[1];
|
||||
const cluster = getClusterById(clusterId);
|
||||
if (host.startsWith("127.0.0.1") || host.startsWith("localhost")) {
|
||||
{
|
||||
const clusterId = req.url.split("/")[1];
|
||||
const cluster = getClusterById(clusterId);
|
||||
|
||||
if (cluster) {
|
||||
// we need to swap path prefix so that request is proxied to kube api
|
||||
req.url = req.url.replace(`/${clusterId}`, apiKubePrefix);
|
||||
if (cluster) {
|
||||
// we need to swap path prefix so that request is proxied to kube api
|
||||
req.url = req.url.replace(`/${clusterId}`, apiKubePrefix);
|
||||
|
||||
return cluster;
|
||||
}
|
||||
}
|
||||
|
||||
return cluster;
|
||||
{
|
||||
const searchParams = new URLSearchParams(req.url);
|
||||
const clusterId = searchParams.get("clusterId");
|
||||
|
||||
if (clusterId) {
|
||||
return getClusterById(clusterId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const clusterId = getClusterIdFromHost(req.headers.host);
|
||||
const clusterId = getClusterIdFromHost(host);
|
||||
|
||||
if (!clusterId) {
|
||||
return undefined;
|
||||
if (clusterId) {
|
||||
return getClusterById(clusterId);
|
||||
}
|
||||
|
||||
return getClusterById(clusterId);
|
||||
return undefined;
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -68,7 +68,7 @@ export class LensProxy {
|
||||
protected retryCounters = new Map<string, number>();
|
||||
|
||||
constructor(private readonly dependencies: Dependencies) {
|
||||
this.configureProxy(dependencies.proxy);
|
||||
this.configureProxy(this.dependencies.proxy);
|
||||
|
||||
this.proxyServer = https.createServer(
|
||||
{
|
||||
@@ -91,8 +91,13 @@ export class LensProxy {
|
||||
const isInternal = req.url.startsWith(`${apiPrefix}?`);
|
||||
const reqHandler = isInternal ? this.dependencies.shellApiRequest : this.dependencies.kubeApiUpgradeRequest;
|
||||
|
||||
(async () => reqHandler({ req, socket, head, cluster }))()
|
||||
.catch(error => this.dependencies.logger.error("[LENS-PROXY]: failed to handle proxy upgrade", error));
|
||||
(async () => {
|
||||
try {
|
||||
await reqHandler({ req, socket, head, cluster });
|
||||
} catch (error) {
|
||||
this.dependencies.logger.error("[LENS-PROXY]: failed to handle proxy upgrade", error);
|
||||
}
|
||||
})();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -27,14 +27,21 @@ const shellApiRequestInjectable = getInjectable({
|
||||
const nodeName = searchParams.get("node");
|
||||
const shellToken = searchParams.get("shellToken");
|
||||
|
||||
console.log("got shell api request", { tabId, clusterId: cluster?.id, nodeName });
|
||||
|
||||
if (!tabId || !cluster || !shellApiAuthenticator.authenticate(cluster.id, tabId, shellToken)) {
|
||||
socket.write("Invalid shell request");
|
||||
socket.end();
|
||||
} else {
|
||||
new WebSocketServer({ noServer: true })
|
||||
.handleUpgrade(req, socket, head, (websocket) => {
|
||||
openShellSession({ websocket, cluster, tabId, nodeName })
|
||||
.catch(error => logger.error(`[SHELL-SESSION]: failed to open a ${nodeName ? "node" : "local"} shell`, error));
|
||||
(async () => {
|
||||
try {
|
||||
await openShellSession({ websocket, cluster, tabId, nodeName });
|
||||
} catch (error) {
|
||||
logger.error(`[SHELL-SESSION]: failed to open a ${nodeName ? "node" : "local"} shell`, error);
|
||||
}
|
||||
})();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -37,23 +37,21 @@ export class LocalShellSession extends ShellSession {
|
||||
}
|
||||
|
||||
public async open() {
|
||||
// extensions can modify the env
|
||||
const env = this.dependencies.modifyTerminalShellEnv(this.cluster.id, await this.getCachedShellEnv());
|
||||
const cachedShellEnv = await this.getCachedShellEnv();
|
||||
const env = this.dependencies.modifyTerminalShellEnv(this.cluster.id, cachedShellEnv);
|
||||
const shell = env.PTYSHELL;
|
||||
|
||||
if (!shell) {
|
||||
if (shell) {
|
||||
const args = await this.getShellArgs(shell);
|
||||
|
||||
await this.openShellProcess(shell, args, env);
|
||||
} else {
|
||||
this.send({
|
||||
type: TerminalChannels.ERROR,
|
||||
data: "PTYSHELL is not defined with the environment",
|
||||
});
|
||||
this.dependencies.logger.warn(`[LOCAL-SHELL-SESSION]: PTYSHELL env var is not defined for ${this.terminalId}`);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const args = await this.getShellArgs(shell);
|
||||
|
||||
await this.openShellProcess(shell, args, env);
|
||||
}
|
||||
|
||||
protected async getShellArgs(shell: string): Promise<string[]> {
|
||||
|
||||
@@ -18,12 +18,16 @@ import getDirnameOfPathInjectable from "../../../common/path/get-dirname.injecta
|
||||
import joinPathsInjectable from "../../../common/path/join-paths.injectable";
|
||||
import getBasenameOfPathInjectable from "../../../common/path/get-basename.injectable";
|
||||
import computeShellEnvironmentInjectable from "../../../features/shell-sync/main/compute-shell-environment.injectable";
|
||||
import spawnPtyInjectable from "../spawn-pty.injectable";
|
||||
import userShellSettingInjectable from "../../../common/user-store/shell-setting.injectable";
|
||||
import appNameInjectable from "../../../common/vars/app-name.injectable";
|
||||
import buildVersionInjectable from "../../vars/build-version/build-version.injectable";
|
||||
import emitAppEventInjectable from "../../../common/app-event-bus/emit-event.injectable";
|
||||
import statInjectable from "../../../common/fs/stat.injectable";
|
||||
import shellEnvironmentCacheInjectable from "../shell-environment-cache.injectable";
|
||||
import shellProcessesInjectable from "../shell-processes.injectable";
|
||||
import homeDirectoryPathInjectable from "../../../common/os/home-directory-path.injectable";
|
||||
import pathDelimiterInjectable from "../../../common/path/delimiter.injectable";
|
||||
import spawnPtyInjectable from "../spawn-pty.injectable";
|
||||
|
||||
export interface OpenLocalShellSessionArgs {
|
||||
websocket: WebSocket;
|
||||
@@ -55,6 +59,10 @@ const openLocalShellSessionInjectable = getInjectable({
|
||||
computeShellEnvironment: di.inject(computeShellEnvironmentInjectable),
|
||||
spawnPty: di.inject(spawnPtyInjectable),
|
||||
stat: di.inject(statInjectable),
|
||||
shellEnvironmentCache: di.inject(shellEnvironmentCacheInjectable),
|
||||
shellProcesses: di.inject(shellProcessesInjectable),
|
||||
homeDirectory: di.inject(homeDirectoryPathInjectable),
|
||||
pathDelimiter: di.inject(pathDelimiterInjectable),
|
||||
};
|
||||
|
||||
return (args) => {
|
||||
|
||||
@@ -13,13 +13,18 @@ import isWindowsInjectable from "../../../common/vars/is-windows.injectable";
|
||||
import loggerInjectable from "../../../common/logger.injectable";
|
||||
import createKubeJsonApiForClusterInjectable from "../../../common/k8s-api/create-kube-json-api-for-cluster.injectable";
|
||||
import computeShellEnvironmentInjectable from "../../../features/shell-sync/main/compute-shell-environment.injectable";
|
||||
import spawnPtyInjectable from "../spawn-pty.injectable";
|
||||
import userShellSettingInjectable from "../../../common/user-store/shell-setting.injectable";
|
||||
import appNameInjectable from "../../../common/vars/app-name.injectable";
|
||||
import buildVersionInjectable from "../../vars/build-version/build-version.injectable";
|
||||
import emitAppEventInjectable from "../../../common/app-event-bus/emit-event.injectable";
|
||||
import statInjectable from "../../../common/fs/stat.injectable";
|
||||
import createKubeApiInjectable from "../../../common/k8s-api/create-kube-api.injectable";
|
||||
import getBasenameOfPathInjectable from "../../../common/path/get-basename.injectable";
|
||||
import homeDirectoryPathInjectable from "../../../common/os/home-directory-path.injectable";
|
||||
import pathDelimiterInjectable from "../../../common/path/delimiter.injectable";
|
||||
import shellEnvironmentCacheInjectable from "../shell-environment-cache.injectable";
|
||||
import shellProcessesInjectable from "../shell-processes.injectable";
|
||||
import spawnPtyInjectable from "../spawn-pty.injectable";
|
||||
|
||||
export interface NodeShellSessionArgs {
|
||||
websocket: WebSocket;
|
||||
@@ -47,6 +52,11 @@ const openNodeShellSessionInjectable = getInjectable({
|
||||
emitAppEvent: di.inject(emitAppEventInjectable),
|
||||
stat: di.inject(statInjectable),
|
||||
createKubeApi: di.inject(createKubeApiInjectable),
|
||||
getBasenameOfPath: di.inject(getBasenameOfPathInjectable),
|
||||
homeDirectory: di.inject(homeDirectoryPathInjectable),
|
||||
pathDelimiter: di.inject(pathDelimiterInjectable),
|
||||
shellEnvironmentCache: di.inject(shellEnvironmentCacheInjectable),
|
||||
shellProcesses: di.inject(shellProcessesInjectable),
|
||||
};
|
||||
|
||||
return async (args) => {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { IAsyncComputed } from "@ogre-tools/injectable-react";
|
||||
import { asyncComputed } from "@ogre-tools/injectable-react";
|
||||
import { when } from "mobx";
|
||||
import { now } from "mobx-utils";
|
||||
import type { ClusterId } from "../../common/cluster-types";
|
||||
import { getOrInsert } from "../../common/utils";
|
||||
|
||||
export type ShellEnvironmentCache = (clusterId: string, builder: () => Promise<Partial<Record<string, string>>>) => Promise<Partial<Record<string, string>>>;
|
||||
|
||||
const shellEnvironmentCacheInjectable = getInjectable({
|
||||
id: "shell-environment-cache",
|
||||
instantiate: (): ShellEnvironmentCache => {
|
||||
const cache = new Map<ClusterId, IAsyncComputed<Partial<Record<string, string>>>>();
|
||||
|
||||
return async (clusterId, builder) => {
|
||||
const cacheLine = getOrInsert(cache, clusterId, asyncComputed(() => {
|
||||
now(1000 * 60 * 10); // update every 10 minutes
|
||||
|
||||
return builder();
|
||||
}));
|
||||
|
||||
await when(() => !cacheLine.pending.get());
|
||||
|
||||
return cacheLine.value.get();
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default shellEnvironmentCacheInjectable;
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { IPty } from "node-pty";
|
||||
import loggerInjectable from "../../common/logger.injectable";
|
||||
import { getOrInsertWith } from "../../common/utils";
|
||||
import type { AsyncResult } from "../../common/utils/async-result";
|
||||
import spawnPtyInjectable from "./spawn-pty.injectable";
|
||||
|
||||
export interface StartOrResuemArgs {
|
||||
terminalId: string;
|
||||
shell: string;
|
||||
args: string[];
|
||||
env: Partial<Record<string, string>>;
|
||||
cwd: string;
|
||||
}
|
||||
|
||||
export interface ShellProcesses {
|
||||
startOrResume: (args: StartOrResuemArgs) => AsyncResult<{ shellProcess: IPty; resume: boolean }>;
|
||||
cleanup: () => void;
|
||||
clear: (terminalId: string) => void;
|
||||
}
|
||||
|
||||
const shellProcessesInjectable = getInjectable({
|
||||
id: "shell-processes",
|
||||
instantiate: (di): ShellProcesses => {
|
||||
const spawnPty = di.inject(spawnPtyInjectable);
|
||||
const logger = di.inject(loggerInjectable);
|
||||
|
||||
const processes = new Map<string, IPty>();
|
||||
|
||||
return {
|
||||
startOrResume: ({ terminalId, shell, args, env, cwd }) => {
|
||||
try {
|
||||
const resume = processes.has(terminalId);
|
||||
|
||||
const shellProcess = getOrInsertWith(processes, terminalId, () => (
|
||||
spawnPty(shell, args, {
|
||||
rows: 30,
|
||||
cols: 80,
|
||||
cwd,
|
||||
env,
|
||||
name: "xterm-256color",
|
||||
// TODO: Something else is broken here so we need to force the use of winPty on windows
|
||||
useConpty: false,
|
||||
})
|
||||
));
|
||||
|
||||
logger.info(`[SHELL-SESSION]: PTY for ${terminalId} is ${resume ? "resumed" : "started"} with PID=${shellProcess.pid}`);
|
||||
|
||||
return {
|
||||
callWasSuccessful: true,
|
||||
response: { shellProcess, resume },
|
||||
};
|
||||
} catch (error) {
|
||||
logger.warn(`[SHELL-SESSION]: Failed to start PTY for ${terminalId}: ${error}`, { shell });
|
||||
|
||||
return {
|
||||
callWasSuccessful: false,
|
||||
error: String(error),
|
||||
};
|
||||
}
|
||||
},
|
||||
cleanup: () => {
|
||||
for (const shellProcess of processes.values()) {
|
||||
try {
|
||||
process.kill(shellProcess.pid);
|
||||
} catch {
|
||||
// ignore error
|
||||
}
|
||||
}
|
||||
|
||||
processes.clear();
|
||||
},
|
||||
clear: (terminalId) => {
|
||||
processes.delete(terminalId);
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default shellProcessesInjectable;
|
||||
@@ -7,18 +7,17 @@ import type { Cluster } from "../../common/cluster/cluster";
|
||||
import type { Kubectl } from "../kubectl/kubectl";
|
||||
import type WebSocket from "ws";
|
||||
import { clearKubeconfigEnvVars } from "../utils/clear-kube-env-vars";
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
import type * as pty from "node-pty";
|
||||
import { getOrInsertWith } from "../../common/utils";
|
||||
import { type TerminalMessage, TerminalChannels } from "../../common/terminal/channels";
|
||||
import type { Logger } from "../../common/logger";
|
||||
import type { ComputeShellEnvironment } from "../../features/shell-sync/main/compute-shell-environment.injectable";
|
||||
import type { SpawnPty } from "./spawn-pty.injectable";
|
||||
import type { InitializableState } from "../../common/initializable-state/create";
|
||||
import type { EmitAppEvent } from "../../common/app-event-bus/emit-event.injectable";
|
||||
import type { Stat } from "../../common/fs/stat.injectable";
|
||||
import type { IComputedValue } from "mobx";
|
||||
import type { ShellProcesses } from "./shell-processes.injectable";
|
||||
import type { ShellEnvironmentCache } from "./shell-environment-cache.injectable";
|
||||
import type { GetBasenameOfPath } from "../../common/path/get-basename.injectable";
|
||||
import type { SpawnPty } from "./spawn-pty.injectable";
|
||||
|
||||
export class ShellOpenError extends Error {
|
||||
constructor(message: string, options?: ErrorOptions) {
|
||||
@@ -109,12 +108,17 @@ export interface ShellSessionDependencies {
|
||||
readonly isMac: boolean;
|
||||
readonly logger: Logger;
|
||||
readonly userShellSetting: IComputedValue<string>;
|
||||
readonly homeDirectory: string;
|
||||
readonly appName: string;
|
||||
readonly buildVersion: InitializableState<string>;
|
||||
readonly shellProcesses: ShellProcesses;
|
||||
readonly pathDelimiter: string;
|
||||
computeShellEnvironment: ComputeShellEnvironment;
|
||||
spawnPty: SpawnPty;
|
||||
emitAppEvent: EmitAppEvent;
|
||||
stat: Stat;
|
||||
shellEnvironmentCache: ShellEnvironmentCache;
|
||||
getBasenameOfPath: GetBasenameOfPath;
|
||||
}
|
||||
|
||||
export interface ShellSessionArgs {
|
||||
@@ -127,25 +131,6 @@ export interface ShellSessionArgs {
|
||||
export abstract class ShellSession {
|
||||
abstract readonly ShellType: string;
|
||||
|
||||
private static readonly shellEnvs = new Map<string, Record<string, string | undefined>>();
|
||||
private static readonly processes = new Map<string, pty.IPty>();
|
||||
|
||||
/**
|
||||
* Kill all remaining shell backing processes. Should be called when about to
|
||||
* quit
|
||||
*/
|
||||
public static cleanup(): void {
|
||||
for (const shellProcess of this.processes.values()) {
|
||||
try {
|
||||
process.kill(shellProcess.pid);
|
||||
} catch {
|
||||
// ignore error
|
||||
}
|
||||
}
|
||||
|
||||
this.processes.clear();
|
||||
}
|
||||
|
||||
protected running = false;
|
||||
protected readonly kubectlBinDirP: Promise<string>;
|
||||
protected readonly kubeconfigPathP: Promise<string>;
|
||||
@@ -156,36 +141,6 @@ export abstract class ShellSession {
|
||||
|
||||
protected abstract get cwd(): string | undefined;
|
||||
|
||||
protected ensureShellProcess(shell: string, args: string[], env: Partial<Record<string, string>>, cwd: string): { shellProcess: pty.IPty; resume: boolean } | null {
|
||||
try {
|
||||
const resume = ShellSession.processes.has(this.terminalId);
|
||||
|
||||
const shellProcess = getOrInsertWith(ShellSession.processes, this.terminalId, () => (
|
||||
this.dependencies.spawnPty(shell, args, {
|
||||
rows: 30,
|
||||
cols: 80,
|
||||
cwd,
|
||||
env,
|
||||
name: "xterm-256color",
|
||||
// TODO: Something else is broken here so we need to force the use of winPty on windows
|
||||
useConpty: false,
|
||||
})
|
||||
));
|
||||
|
||||
this.dependencies.logger.info(`[SHELL-SESSION]: PTY for ${this.terminalId} is ${resume ? "resumed" : "started"} with PID=${shellProcess.pid}`);
|
||||
|
||||
return { shellProcess, resume };
|
||||
} catch (error) {
|
||||
this.send({
|
||||
type: TerminalChannels.ERROR,
|
||||
data: `Failed to start shell (${shell}): ${error}`,
|
||||
});
|
||||
this.dependencies.logger.warn(`[SHELL-SESSION]: Failed to start PTY for ${this.terminalId}: ${error}`, { shell });
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
constructor(protected readonly dependencies: ShellSessionDependencies, { kubectl, websocket, cluster, tabId: terminalId }: ShellSessionArgs) {
|
||||
this.kubectl = kubectl;
|
||||
this.websocket = websocket;
|
||||
@@ -205,13 +160,13 @@ export abstract class ShellSession {
|
||||
if (this.dependencies.isWindows) {
|
||||
cwdOptions.push(
|
||||
env.USERPROFILE,
|
||||
os.homedir(),
|
||||
this.dependencies.homeDirectory,
|
||||
"C:\\",
|
||||
);
|
||||
} else {
|
||||
cwdOptions.push(
|
||||
env.HOME,
|
||||
os.homedir(),
|
||||
this.dependencies.homeDirectory,
|
||||
);
|
||||
|
||||
if (this.dependencies.isMac) {
|
||||
@@ -242,20 +197,34 @@ export abstract class ShellSession {
|
||||
|
||||
protected async openShellProcess(shell: string, args: string[], env: Record<string, string | undefined>) {
|
||||
const cwd = await this.getCwd(env);
|
||||
const ensured = this.ensureShellProcess(shell, args, env, cwd);
|
||||
const result = this.dependencies.shellProcesses.startOrResume({
|
||||
terminalId: this.terminalId,
|
||||
shell,
|
||||
args,
|
||||
env,
|
||||
cwd,
|
||||
});
|
||||
|
||||
if (!result.callWasSuccessful) {
|
||||
this.send({
|
||||
type: TerminalChannels.ERROR,
|
||||
data: `Failed to start shell (${shell}): ${result.error}`,
|
||||
});
|
||||
|
||||
if (!ensured) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { shellProcess, resume } = ensured;
|
||||
const { shellProcess, resume } = result.response;
|
||||
|
||||
if (resume) {
|
||||
this.send({ type: TerminalChannels.CONNECTED });
|
||||
}
|
||||
|
||||
this.running = true;
|
||||
shellProcess.onData(data => this.send({ type: TerminalChannels.STDOUT, data }));
|
||||
shellProcess.onData(data => {
|
||||
console.log("sending", { data });
|
||||
this.send({ type: TerminalChannels.STDOUT, data });
|
||||
});
|
||||
shellProcess.onExit(({ exitCode }) => {
|
||||
this.dependencies.logger.info(`[SHELL-SESSION]: shell has exited for ${this.terminalId} closed with exitcode=${exitCode}`);
|
||||
|
||||
@@ -322,8 +291,8 @@ export abstract class ShellSession {
|
||||
|
||||
try {
|
||||
this.dependencies.logger.info(`[SHELL-SESSION]: Killing shell process (pid=${shellProcess.pid}) for ${this.terminalId}`);
|
||||
this.dependencies.shellProcesses.clear(this.terminalId);
|
||||
shellProcess.kill();
|
||||
ShellSession.processes.delete(this.terminalId);
|
||||
} catch (error) {
|
||||
this.dependencies.logger.warn(`[SHELL-SESSION]: failed to kill shell process (pid=${shellProcess.pid}) for ${this.terminalId}`, error);
|
||||
}
|
||||
@@ -338,21 +307,7 @@ export abstract class ShellSession {
|
||||
}
|
||||
|
||||
protected async getCachedShellEnv() {
|
||||
const { id: clusterId } = this.cluster;
|
||||
|
||||
let env = ShellSession.shellEnvs.get(clusterId);
|
||||
|
||||
if (!env) {
|
||||
env = await this.getShellEnv();
|
||||
ShellSession.shellEnvs.set(clusterId, env);
|
||||
} else {
|
||||
// refresh env in the background
|
||||
this.getShellEnv().then((shellEnv: any) => {
|
||||
ShellSession.shellEnvs.set(clusterId, shellEnv);
|
||||
});
|
||||
}
|
||||
|
||||
return env;
|
||||
return this.dependencies.shellEnvironmentCache(this.cluster.id, () => this.getShellEnv());
|
||||
}
|
||||
|
||||
protected async getShellEnv() {
|
||||
@@ -367,13 +322,13 @@ export abstract class ShellSession {
|
||||
})();
|
||||
|
||||
const env = clearKubeconfigEnvVars(JSON.parse(JSON.stringify(rawEnv)));
|
||||
const pathStr = [await this.kubectlBinDirP, ...this.getPathEntries(), env.PATH].join(path.delimiter);
|
||||
const pathStr = [await this.kubectlBinDirP, ...this.getPathEntries(), env.PATH].join(this.dependencies.pathDelimiter);
|
||||
|
||||
delete env.DEBUG; // don't pass DEBUG into shells
|
||||
env.PTYSHELL = shell;
|
||||
env.PATH = pathStr;
|
||||
|
||||
if (this.dependencies.isWindows) {
|
||||
env.PTYSHELL = shell || "powershell.exe";
|
||||
env.PATH = pathStr;
|
||||
env.LENS_SESSION = "true";
|
||||
env.WSLENV = [
|
||||
env.WSLENV,
|
||||
@@ -381,14 +336,9 @@ export abstract class ShellSession {
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(":");
|
||||
} else if (shell !== undefined) {
|
||||
env.PTYSHELL = shell;
|
||||
env.PATH = pathStr;
|
||||
} else {
|
||||
env.PTYSHELL = ""; // blank runs the system default shell
|
||||
}
|
||||
|
||||
if (path.basename(env.PTYSHELL) === "zsh") {
|
||||
if (this.dependencies.getBasenameOfPath(env.PTYSHELL) === "zsh") {
|
||||
env.OLD_ZDOTDIR = env.ZDOTDIR || env.HOME;
|
||||
env.ZDOTDIR = await this.kubectlBinDirP;
|
||||
env.DISABLE_AUTO_UPDATE = "true";
|
||||
|
||||
@@ -4,15 +4,19 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { beforeQuitOfBackEndInjectionToken } from "../runnable-tokens/before-quit-of-back-end-injection-token";
|
||||
import { ShellSession } from "../../shell-session/shell-session";
|
||||
import shellProcessesInjectable from "../../shell-session/shell-processes.injectable";
|
||||
|
||||
const cleanUpShellSessionsInjectable = getInjectable({
|
||||
id: "clean-up-shell-sessions",
|
||||
|
||||
instantiate: () => ({
|
||||
id: "clean-up-shell-sessions",
|
||||
run: () => void ShellSession.cleanup(),
|
||||
}),
|
||||
instantiate: (di) => {
|
||||
const shellProcesses = di.inject(shellProcessesInjectable);
|
||||
|
||||
return {
|
||||
id: "clean-up-shell-sessions",
|
||||
run: () => void shellProcesses.cleanup(),
|
||||
};
|
||||
},
|
||||
|
||||
injectionToken: beforeQuitOfBackEndInjectionToken,
|
||||
});
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { afterApplicationIsLoadedInjectionToken } from "../../runnable-tokens/after-application-is-loaded-injection-token";
|
||||
import directoryForKubeConfigsInjectable from "../../../../common/app-paths/directory-for-kube-configs/directory-for-kube-configs.injectable";
|
||||
import ensureDirInjectable from "../../../../common/fs/ensure-dir.injectable";
|
||||
import ensureDirectoryInjectable from "../../../../common/fs/ensure-directory.injectable";
|
||||
import kubeconfigSyncManagerInjectable from "../../../catalog-sources/kubeconfig-sync/manager.injectable";
|
||||
import addKubeconfigSyncAsEntitySourceInjectable from "./add-source.injectable";
|
||||
|
||||
@@ -15,7 +15,7 @@ const startKubeConfigSyncInjectable = getInjectable({
|
||||
instantiate: (di) => {
|
||||
const directoryForKubeConfigs = di.inject(directoryForKubeConfigsInjectable);
|
||||
const kubeConfigSyncManager = di.inject(kubeconfigSyncManagerInjectable);
|
||||
const ensureDir = di.inject(ensureDirInjectable);
|
||||
const ensureDir = di.inject(ensureDirectoryInjectable);
|
||||
|
||||
return {
|
||||
id: "start-kubeconfig-sync",
|
||||
|
||||
Reference in new issue
Block a user