Make apiBase not an InjectionToken and make openNodeShellSession not special (#6774)

* Replace apiBaseInjectionToken with tokens for configuration instead

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Use new ordering to make openNodeShellSession non-special

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-12-16 16:13:57 -05:00
1 parent de789ff955
commit 5c6402b60a
33 files changed
+175 -133

No files matched your search

@@ -0,0 +1,19 @@
/**
* 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 { apiBaseHostHeaderInjectionToken } from "../../common/k8s-api/api-base-configs";
import lensProxyPortInjectable from "../lens-proxy/lens-proxy-port.injectable";
const apiBaseHostHeaderInjectable = getInjectable({
id: "api-base-host-header",
instantiate: (di) => {
const lensProxyPort = di.inject(lensProxyPortInjectable);
return `localhost:${lensProxyPort.get()}`;
},
injectionToken: apiBaseHostHeaderInjectionToken,
});
export default apiBaseHostHeaderInjectable;
@@ -0,0 +1,19 @@
/**
* 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 { apiBaseServerAddressInjectionToken } from "../../common/k8s-api/api-base-configs";
import lensProxyPortInjectable from "../lens-proxy/lens-proxy-port.injectable";
const apiBaseServerAddressInjectable = getInjectable({
id: "api-base-server-address",
instantiate: (di) => {
const lensProxyPort = di.inject(lensProxyPortInjectable);
return `http://127.0.0.1:${lensProxyPort.get()}`;
},
injectionToken: apiBaseServerAddressInjectionToken,
});
export default apiBaseServerAddressInjectable;
-33
View File
@@ -1,33 +0,0 @@
/**
* 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 { apiBaseInjectionToken } from "../../common/k8s-api/api-base";
import createJsonApiInjectable from "../../common/k8s-api/create-json-api.injectable";
import { apiPrefix } from "../../common/vars";
import isDebuggingInjectable from "../../common/vars/is-debugging.injectable";
import isDevelopmentInjectable from "../../common/vars/is-development.injectable";
import lensProxyPortInjectable from "../lens-proxy/lens-proxy-port.injectable";
const apiBaseInjectable = getInjectable({
id: "api-base",
instantiate: (di) => {
const proxyPort = di.inject(lensProxyPortInjectable);
const createJsonApi = di.inject(createJsonApiInjectable);
const isDebugging = di.inject(isDebuggingInjectable);
return createJsonApi({
serverAddress: `http://127.0.0.1:${proxyPort.get()}`,
apiBase: apiPrefix,
debug: di.inject(isDevelopmentInjectable) || isDebugging,
}, {
headers: {
"Host": `localhost:${proxyPort.get()}`,
},
});
},
injectionToken: apiBaseInjectionToken,
});
export default apiBaseInjectable;
@@ -22,10 +22,11 @@ const openShellSessionInjectable = getInjectable({
instantiate: (di): OpenShellSession => {
const openLocalShellSession = di.inject(openLocalShellSessionInjectable);
const openNodeShellSession = di.inject(openNodeShellSessionInjectable);
return ({ nodeName, ...args }) => (
nodeName
? di.inject(openNodeShellSessionInjectable, { nodeName, ...args })
? openNodeShellSession({ nodeName, ...args })
: openLocalShellSession(args)
);
},
@@ -2,7 +2,7 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
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";
@@ -27,9 +27,11 @@ export interface NodeShellSessionArgs {
nodeName: string;
}
export type OpenNodeShellSession = (args: NodeShellSessionArgs) => Promise<void>;
const openNodeShellSessionInjectable = getInjectable({
id: "open-node-shell-session",
instantiate: (di, params: NodeShellSessionArgs) => {
instantiate: (di): OpenNodeShellSession => {
const createKubectl = di.inject(createKubectlInjectable);
const dependencies: NodeShellSessionDependencies = {
isMac: di.inject(isMacInjectable),
@@ -44,13 +46,14 @@ const openNodeShellSessionInjectable = getInjectable({
emitAppEvent: di.inject(emitAppEventInjectable),
stat: di.inject(statInjectable),
};
const kubectl = createKubectl(params.cluster.version);
const session = new NodeShellSession(dependencies, { kubectl, ...params });
return session.open();
return async (args) => {
const kubectl = createKubectl(args.cluster.version);
const session = new NodeShellSession(dependencies, { kubectl, ...args });
return session.open();
};
},
// NOTE: this must be transient to bypass the circular dependency of `createKubeJsonApiForClusterInjectable` on the lens proxy port
lifecycle: lifecycleEnum.transient,
});
export default openNodeShellSessionInjectable;