mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Restructure to monorepo (#6907)
* wip: restructure to monorepo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor create-release-pr to a package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * github workflow fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix typo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * add webpack-env types to core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix github workflows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor/fix integration tests Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * lint fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * yarn run dev Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * eslint settings for vscode Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * move templates to right package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * open-lens build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * integration test fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix nx task dependencies Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * use bash shell for unit tests in test workflow Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix test:unit for windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix build:app on windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove ELECTRON_BUILDER_EXTRA_ARGS Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * sync src/ from master Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove Makefile from core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
1 parent
0066ca4ea2
commit
2657df2293
3321 files changed
+28699
-11447
No files matched your search
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { v4 as uuid } from "uuid";
|
||||
import { Watch, CoreV1Api } from "@kubernetes/client-node";
|
||||
import type { KubeConfig } from "@kubernetes/client-node";
|
||||
import type { ShellSessionArgs, ShellSessionDependencies } from "../shell-session";
|
||||
import { ShellOpenError, ShellSession } from "../shell-session";
|
||||
import { get, once } from "lodash";
|
||||
import { NodeApi } from "../../../common/k8s-api/endpoints";
|
||||
import { TerminalChannels } from "../../../common/terminal/channels";
|
||||
import type { CreateKubeJsonApiForCluster } from "../../../common/k8s-api/create-kube-json-api-for-cluster.injectable";
|
||||
import type { CreateKubeApi } from "../../../common/k8s-api/create-kube-api.injectable";
|
||||
|
||||
export interface NodeShellSessionArgs extends ShellSessionArgs {
|
||||
nodeName: string;
|
||||
}
|
||||
|
||||
export interface NodeShellSessionDependencies extends ShellSessionDependencies {
|
||||
createKubeJsonApiForCluster: CreateKubeJsonApiForCluster;
|
||||
createKubeApi: CreateKubeApi;
|
||||
}
|
||||
|
||||
export class NodeShellSession extends ShellSession {
|
||||
ShellType = "node-shell";
|
||||
|
||||
protected readonly podName = `node-shell-${uuid()}`;
|
||||
protected readonly nodeName: string;
|
||||
protected readonly cwd: string | undefined = undefined;
|
||||
|
||||
constructor(protected readonly dependencies: NodeShellSessionDependencies, { nodeName, ...args }: NodeShellSessionArgs) {
|
||||
super(dependencies, args);
|
||||
this.nodeName = nodeName;
|
||||
}
|
||||
|
||||
public async open() {
|
||||
const kc = await this.cluster.getProxyKubeconfig();
|
||||
const coreApi = kc.makeApiClient(CoreV1Api);
|
||||
const shell = await this.kubectl.getPath();
|
||||
|
||||
const cleanup = once(() => {
|
||||
coreApi
|
||||
.deleteNamespacedPod(this.podName, "kube-system")
|
||||
.catch(error => this.dependencies.logger.warn(`[NODE-SHELL]: failed to remove pod shell`, error));
|
||||
});
|
||||
|
||||
this.websocket.once("close", cleanup);
|
||||
|
||||
try {
|
||||
await this.createNodeShellPod(coreApi);
|
||||
await this.waitForRunningPod(kc);
|
||||
} catch (error) {
|
||||
cleanup();
|
||||
|
||||
this.send({
|
||||
type: TerminalChannels.STDOUT,
|
||||
data: `Error occurred: ${get(error, "response.body.message", error ? String(error) : "unknown error")}`,
|
||||
});
|
||||
|
||||
throw new ShellOpenError(
|
||||
"failed to create node pod",
|
||||
error instanceof Error
|
||||
? { cause: error }
|
||||
: undefined,
|
||||
);
|
||||
}
|
||||
|
||||
const env = await this.getCachedShellEnv();
|
||||
const args = ["exec", "-i", "-t", "-n", "kube-system", this.podName, "--"];
|
||||
const nodeApi = this.dependencies.createKubeApi(NodeApi, {
|
||||
request: this.dependencies.createKubeJsonApiForCluster(this.cluster.id),
|
||||
});
|
||||
const node = await nodeApi.get({ name: this.nodeName });
|
||||
|
||||
if (!node) {
|
||||
throw new Error(`No node with name=${this.nodeName} found`);
|
||||
}
|
||||
|
||||
const nodeOs = node.getOperatingSystem();
|
||||
|
||||
switch (nodeOs) {
|
||||
default:
|
||||
this.dependencies.logger.warn(`[NODE-SHELL-SESSION]: could not determine node OS, falling back with assumption of linux`);
|
||||
// fallthrough
|
||||
case "linux":
|
||||
args.push("sh", "-c", "((clear && bash) || (clear && ash) || (clear && sh))");
|
||||
break;
|
||||
case "windows":
|
||||
args.push("powershell");
|
||||
break;
|
||||
}
|
||||
|
||||
await this.openShellProcess(shell, args, env);
|
||||
}
|
||||
|
||||
protected createNodeShellPod(coreApi: CoreV1Api) {
|
||||
const imagePullSecrets = this.cluster.imagePullSecret
|
||||
? [{
|
||||
name: this.cluster.imagePullSecret,
|
||||
}]
|
||||
: undefined;
|
||||
|
||||
return coreApi
|
||||
.createNamespacedPod("kube-system", {
|
||||
metadata: {
|
||||
name: this.podName,
|
||||
namespace: "kube-system",
|
||||
},
|
||||
spec: {
|
||||
nodeName: this.nodeName,
|
||||
restartPolicy: "Never",
|
||||
terminationGracePeriodSeconds: 0,
|
||||
hostPID: true,
|
||||
hostIPC: true,
|
||||
hostNetwork: true,
|
||||
tolerations: [{
|
||||
operator: "Exists",
|
||||
}],
|
||||
priorityClassName: "system-node-critical",
|
||||
containers: [{
|
||||
name: "shell",
|
||||
image: this.cluster.nodeShellImage,
|
||||
securityContext: {
|
||||
privileged: true,
|
||||
},
|
||||
command: ["nsenter"],
|
||||
args: ["-t", "1", "-m", "-u", "-i", "-n", "sleep", "14000"],
|
||||
}],
|
||||
imagePullSecrets,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
protected waitForRunningPod(kc: KubeConfig): Promise<void> {
|
||||
this.dependencies.logger.debug(`[NODE-SHELL]: waiting for ${this.podName} to be running`);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
new Watch(kc)
|
||||
.watch(`/api/v1/namespaces/kube-system/pods`,
|
||||
{},
|
||||
// callback is called for each received object.
|
||||
(type, { metadata: { name }, status }) => {
|
||||
if (name === this.podName) {
|
||||
switch (status.phase) {
|
||||
case "Running":
|
||||
return resolve();
|
||||
case "Failed":
|
||||
return reject(`Failed to be created: ${status.message || "unknown error"}`);
|
||||
}
|
||||
}
|
||||
},
|
||||
// done callback is called if the watch terminates normally
|
||||
(err) => {
|
||||
this.dependencies.logger.error(`[NODE-SHELL]: ${this.podName} was not created in time`);
|
||||
reject(err);
|
||||
},
|
||||
)
|
||||
.then(req => {
|
||||
setTimeout(() => {
|
||||
this.dependencies.logger.error(`[NODE-SHELL]: aborting wait for ${this.podName}, timing out`);
|
||||
req.abort();
|
||||
reject("Pod creation timed out");
|
||||
}, 2 * 60 * 1000); // 2 * 60 * 1000
|
||||
})
|
||||
.catch(error => {
|
||||
this.dependencies.logger.error(`[NODE-SHELL]: waiting for ${this.podName} failed: ${error}`);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 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 { Cluster } from "../../../common/cluster/cluster";
|
||||
import type WebSocket from "ws";
|
||||
import createKubectlInjectable from "../../kubectl/create-kubectl.injectable";
|
||||
import type { NodeShellSessionDependencies } from "./node-shell-session";
|
||||
import { NodeShellSession } from "./node-shell-session";
|
||||
import isMacInjectable from "../../../common/vars/is-mac.injectable";
|
||||
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";
|
||||
|
||||
export interface NodeShellSessionArgs {
|
||||
websocket: WebSocket;
|
||||
cluster: Cluster;
|
||||
tabId: string;
|
||||
nodeName: string;
|
||||
}
|
||||
|
||||
export type OpenNodeShellSession = (args: NodeShellSessionArgs) => Promise<void>;
|
||||
|
||||
const openNodeShellSessionInjectable = getInjectable({
|
||||
id: "open-node-shell-session",
|
||||
instantiate: (di): OpenNodeShellSession => {
|
||||
const createKubectl = di.inject(createKubectlInjectable);
|
||||
const dependencies: NodeShellSessionDependencies = {
|
||||
isMac: di.inject(isMacInjectable),
|
||||
isWindows: di.inject(isWindowsInjectable),
|
||||
logger: di.inject(loggerInjectable),
|
||||
userShellSetting: di.inject(userShellSettingInjectable),
|
||||
appName: di.inject(appNameInjectable),
|
||||
buildVersion: di.inject(buildVersionInjectable),
|
||||
createKubeJsonApiForCluster: di.inject(createKubeJsonApiForClusterInjectable),
|
||||
computeShellEnvironment: di.inject(computeShellEnvironmentInjectable),
|
||||
spawnPty: di.inject(spawnPtyInjectable),
|
||||
emitAppEvent: di.inject(emitAppEventInjectable),
|
||||
stat: di.inject(statInjectable),
|
||||
createKubeApi: di.inject(createKubeApiInjectable),
|
||||
};
|
||||
|
||||
return async (args) => {
|
||||
const kubectl = createKubectl(args.cluster.version);
|
||||
const session = new NodeShellSession(dependencies, { kubectl, ...args });
|
||||
|
||||
return session.open();
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default openNodeShellSessionInjectable;
|
||||
Reference in new issue
Block a user