1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

modified shell env api to work as a transformer (WIP)

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann 2022-02-02 22:46:51 -05:00
parent 1ac3d0a36b
commit 4b51c22610
9 changed files with 38 additions and 23 deletions

View File

@ -11,3 +11,4 @@ export type { PageRegistration, RegisteredPage, PageParams, PageComponentProps,
export type { ClusterPageMenuRegistration, ClusterPageMenuComponents } from "../registries/page-menu-registry";
export type { ProtocolHandlerRegistration, RouteParams as ProtocolRouteParams, RouteHandler as ProtocolRouteHandler } from "../registries/protocol-handler";
export type { CustomCategoryViewProps, CustomCategoryViewComponents, CustomCategoryViewRegistration } from "../../renderer/components/+catalog/custom-views";
export type { ShellEnvModifier, ShellEnvClusterData } from "../../main/shell-session/shell-env-modifier/shell-env-modifier-registration";

View File

@ -10,11 +10,14 @@ import type { CatalogEntity } from "../common/catalog";
import type { IObservableArray } from "mobx";
import type { MenuRegistration } from "../main/menu/menu-registration";
import type { TrayMenuRegistration } from "../main/tray/tray-menu-registration";
import type { ShellEnvVarRegistration } from "../main/shell-session/env-var/env-var-registration";
import type { ShellEnvModifier } from "../main/shell-session/shell-env-modifier/shell-env-modifier-registration";
import { noop } from "../common/utils";
export class LensMainExtension extends LensExtension {
appMenus: MenuRegistration[] = [];
trayMenus: TrayMenuRegistration[] = [];
shellEnvVars: ShellEnvVarRegistration[] = [];
shellEnvModifier: ShellEnvModifier = noop;
async navigate(pageId?: string, params?: Record<string, any>, frameId?: number) {
return WindowManager.getInstance().navigateExtension(this.id, pageId, params, frameId);

View File

@ -1,10 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
export interface ShellEnvVarRegistration {
title: string;
env: Record<string, string>;
}

View File

@ -7,6 +7,7 @@ import { LocalShellSession } from "./local-shell-session";
import type { Cluster } from "../../../common/cluster/cluster";
import type WebSocket from "ws";
import createKubectlInjectable from "../../kubectl/create-kubectl.injectable";
import shellEnvModifiersInjectable from "../shell-env-modifier/shell-env-modifier.injectable";
interface InstantiationParameter {
webSocket: WebSocket;
@ -17,10 +18,11 @@ interface InstantiationParameter {
const localShellSessionInjectable = getInjectable({
instantiate: (di, { cluster, tabId, webSocket }: InstantiationParameter) => {
const createKubectl = di.inject(createKubectlInjectable);
const shellEnvModifiers = di.inject(shellEnvModifiersInjectable);
const kubectl = createKubectl(cluster.version);
return new LocalShellSession(kubectl, webSocket, cluster, tabId);
return new LocalShellSession(kubectl, shellEnvModifiers.get(), webSocket, cluster, tabId);
},
lifecycle: lifecycleEnum.transient,

View File

@ -25,7 +25,7 @@ export class NodeShellSession extends ShellSession {
protected readonly cwd: string | undefined = undefined;
constructor(protected nodeName: string, kubectl: Kubectl, socket: WebSocket, cluster: Cluster, terminalId: string) {
super(kubectl, socket, cluster, terminalId);
super(kubectl, [], socket, cluster, terminalId);
}
public async open() {

View File

@ -0,0 +1,14 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { noop } from "../../../common/utils";
export interface ShellEnvClusterData {
context: string;
kubeconfigPath: string;
id: string;
}
export type ShellEnvModifier = ((cluster: ShellEnvClusterData, env: Record<string, string | undefined>) => Record<string, string | undefined>) | typeof noop;

View File

@ -5,15 +5,15 @@
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import mainExtensionsInjectable from "../../../extensions/main-extensions.injectable";
import { getShellEnvVars } from "./get-shell-env-vars";
import { shellEnvModifiers } from "./shell-env-modifiers";
const shellEnvVarInjectable = getInjectable({
const shellEnvModifiersInjectable = getInjectable({
instantiate: (di) =>
getShellEnvVars({
shellEnvModifiers({
extensions: di.inject(mainExtensionsInjectable),
}),
lifecycle: lifecycleEnum.singleton,
});
export default shellEnvVarInjectable;
export default shellEnvModifiersInjectable;

View File

@ -10,9 +10,9 @@ interface Dependencies {
extensions: IComputedValue<LensMainExtension[]>;
}
export const getShellEnvVars = ({ extensions }: Dependencies) => {
export const shellEnvModifiers = ({ extensions }: Dependencies) => {
return computed(() => (
extensions.get()
.flatMap((extension) => extension.shellEnvVars)
.flatMap((extension) => extension.shellEnvModifier)
));
};

View File

@ -19,6 +19,7 @@ import logger from "../logger";
import { TerminalChannels, TerminalMessage } from "../../renderer/api/terminal-api";
import { deserialize, serialize } from "v8";
import { stat } from "fs/promises";
import type { ShellEnvModifier } from "./shell-env-modifier/shell-env-modifier-registration";
export class ShellOpenError extends Error {
constructor(message: string, public cause: Error) {
@ -155,7 +156,7 @@ export abstract class ShellSession {
return { shellProcess, resume };
}
constructor(protected kubectl: Kubectl, protected websocket: WebSocket, protected cluster: Cluster, terminalId: string) {
constructor(protected kubectl: Kubectl, protected shellEnvModifiers: ShellEnvModifier[], protected websocket: WebSocket, protected cluster: Cluster, terminalId: string) {
this.kubeconfigPathP = this.cluster.getProxyKubeconfigPath();
this.kubectlBinDirP = this.kubectl.binDir();
this.terminalId = `${cluster.id}:${terminalId}`;
@ -311,7 +312,7 @@ export abstract class ShellSession {
}
protected async getShellEnv() {
const env = clearKubeconfigEnvVars(JSON.parse(JSON.stringify(await shellEnv())));
let env = clearKubeconfigEnvVars(JSON.parse(JSON.stringify(await shellEnv())));
const pathStr = [...this.getPathEntries(), await this.kubectlBinDirP, process.env.PATH].join(path.delimiter);
const shell = UserStore.getInstance().resolvedShell;
@ -341,8 +342,10 @@ export abstract class ShellSession {
env.DISABLE_AUTO_UPDATE = "true";
}
const kubeconfigPath = await this.kubeconfigPathP;
env.PTYPID = process.pid.toString();
env.KUBECONFIG = await this.kubeconfigPathP;
env.KUBECONFIG = kubeconfigPath;
env.TERM_PROGRAM = app.getName();
env.TERM_PROGRAM_VERSION = app.getVersion();
@ -358,6 +361,8 @@ export abstract class ShellSession {
.filter(Boolean)
.join();
this.shellEnvModifiers.map(modifier => env = { ...env, ...modifier({ context: this.cluster.contextName, kubeconfigPath, id: this.cluster.id }, env) });
return env;
}