mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Move Cluster.apiUrl to seperate injectable (#7354)
* Refactor out Cluster.apiUrl to direct reading - Should help prevent using stale data - Removes some uses of synchronous FS operations Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Rename helper function to better communicate intent Signed-off-by: Sebastian Malton <sebastian@malton.name> * Improve prometheus handler tests to override less things Signed-off-by: Sebastian Malton <sebastian@malton.name> --------- Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
18d660ea77
commit
fe2fd4c1fd
53 files changed
+472
-640
No files matched your search
@@ -2,18 +2,22 @@
|
||||
* 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 { KubeAuthProxyDependencies } from "./kube-auth-proxy";
|
||||
import { KubeAuthProxyImpl } from "./kube-auth-proxy";
|
||||
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||
import type { Cluster } from "../../common/cluster/cluster";
|
||||
import spawnInjectable from "../child-process/spawn.injectable";
|
||||
import kubeAuthProxyCertificateInjectable from "./kube-auth-proxy-certificate.injectable";
|
||||
import loggerInjectable from "../../common/logger.injectable";
|
||||
import waitUntilPortIsUsedInjectable from "./wait-until-port-is-used/wait-until-port-is-used.injectable";
|
||||
import lensK8sProxyPathInjectable from "./lens-k8s-proxy-path.injectable";
|
||||
import getPortFromStreamInjectable from "../utils/get-port-from-stream.injectable";
|
||||
import getDirnameOfPathInjectable from "../../common/path/get-dirname.injectable";
|
||||
import randomBytesInjectable from "../../common/utils/random-bytes.injectable";
|
||||
import type { ChildProcess } from "child_process";
|
||||
import { observable, when } from "mobx";
|
||||
import assert from "assert";
|
||||
import clusterApiUrlInjectable from "../../features/cluster/connections/main/api-url.injectable";
|
||||
import kubeAuthProxyCertificateInjectable from "./kube-auth-proxy-certificate.injectable";
|
||||
import broadcastConnectionUpdateInjectable from "../cluster/broadcast-connection-update.injectable";
|
||||
import { TypedRegEx } from "typed-regex";
|
||||
|
||||
export interface KubeAuthProxy {
|
||||
readonly apiPrefix: string;
|
||||
@@ -22,31 +26,159 @@ export interface KubeAuthProxy {
|
||||
exit: () => void;
|
||||
}
|
||||
|
||||
export type CreateKubeAuthProxy = (cluster: Cluster, env: NodeJS.ProcessEnv) => KubeAuthProxy;
|
||||
export type CreateKubeAuthProxy = (env: NodeJS.ProcessEnv) => KubeAuthProxy;
|
||||
|
||||
const startingServeMatcher = "starting to serve on (?<address>.+)";
|
||||
const startingServeRegex = Object.assign(TypedRegEx(startingServeMatcher, "i"), {
|
||||
rawMatcher: startingServeMatcher,
|
||||
});
|
||||
|
||||
const createKubeAuthProxyInjectable = getInjectable({
|
||||
id: "create-kube-auth-proxy",
|
||||
|
||||
instantiate: (di): CreateKubeAuthProxy => {
|
||||
const dependencies: Omit<KubeAuthProxyDependencies, "proxyCert" | "broadcastConnectionUpdate"> = {
|
||||
proxyBinPath: di.inject(lensK8sProxyPathInjectable),
|
||||
spawn: di.inject(spawnInjectable),
|
||||
logger: di.inject(loggerInjectable),
|
||||
waitUntilPortIsUsed: di.inject(waitUntilPortIsUsedInjectable),
|
||||
getPortFromStream: di.inject(getPortFromStreamInjectable),
|
||||
dirname: di.inject(getDirnameOfPathInjectable),
|
||||
};
|
||||
instantiate: (di, cluster): CreateKubeAuthProxy => {
|
||||
const lensK8sProxyPath = di.inject(lensK8sProxyPathInjectable);
|
||||
const spawn = di.inject(spawnInjectable);
|
||||
const logger = di.inject(loggerInjectable);
|
||||
const waitUntilPortIsUsed = di.inject(waitUntilPortIsUsedInjectable);
|
||||
const getPortFromStream = di.inject(getPortFromStreamInjectable);
|
||||
const getDirnameOfPath = di.inject(getDirnameOfPathInjectable);
|
||||
const randomBytes = di.inject(randomBytesInjectable);
|
||||
const clusterApiUrl = di.inject(clusterApiUrlInjectable, cluster);
|
||||
const broadcastConnectionUpdate = di.inject(broadcastConnectionUpdateInjectable, cluster);
|
||||
|
||||
return (cluster, env) => {
|
||||
const clusterUrl = new URL(cluster.apiUrl.get());
|
||||
return (env) => {
|
||||
let port: number | undefined;
|
||||
let proxyProcess: ChildProcess | undefined;
|
||||
const ready = observable.box(false);
|
||||
const apiPrefix = `/${randomBytes(8).toString("hex")}`;
|
||||
|
||||
return new KubeAuthProxyImpl({
|
||||
...dependencies,
|
||||
proxyCert: di.inject(kubeAuthProxyCertificateInjectable, clusterUrl.hostname),
|
||||
broadcastConnectionUpdate: di.inject(broadcastConnectionUpdateInjectable, cluster),
|
||||
}, cluster, env);
|
||||
const exit = () => {
|
||||
ready.set(false);
|
||||
|
||||
if (proxyProcess) {
|
||||
logger.debug("[KUBE-AUTH]: stopping local proxy", cluster.getMeta());
|
||||
proxyProcess.removeAllListeners();
|
||||
proxyProcess.stderr?.removeAllListeners();
|
||||
proxyProcess.stdout?.removeAllListeners();
|
||||
proxyProcess.kill();
|
||||
proxyProcess = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const run = async (): Promise<void> => {
|
||||
if (proxyProcess) {
|
||||
return when(() => ready.get());
|
||||
}
|
||||
|
||||
const apiUrl = await clusterApiUrl();
|
||||
const certificate = di.inject(kubeAuthProxyCertificateInjectable, apiUrl.hostname);
|
||||
|
||||
proxyProcess = spawn(lensK8sProxyPath, [], {
|
||||
env: {
|
||||
...env,
|
||||
KUBECONFIG: cluster.kubeConfigPath.get(),
|
||||
KUBECONFIG_CONTEXT: cluster.contextName.get(),
|
||||
API_PREFIX: apiPrefix,
|
||||
PROXY_KEY: certificate.private,
|
||||
PROXY_CERT: certificate.cert,
|
||||
},
|
||||
cwd: getDirnameOfPath(cluster.kubeConfigPath.get()),
|
||||
});
|
||||
proxyProcess.on("error", (error) => {
|
||||
broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: error.message,
|
||||
});
|
||||
exit();
|
||||
});
|
||||
|
||||
proxyProcess.on("exit", (code) => {
|
||||
if (code) {
|
||||
broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: `proxy exited with code: ${code}`,
|
||||
});
|
||||
} else {
|
||||
broadcastConnectionUpdate({
|
||||
level: "info",
|
||||
message: "proxy exited successfully",
|
||||
});
|
||||
}
|
||||
exit();
|
||||
});
|
||||
|
||||
proxyProcess.on("disconnect", () => {
|
||||
broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: "Proxy disconnected communications",
|
||||
});
|
||||
exit();
|
||||
});
|
||||
|
||||
assert(proxyProcess.stderr);
|
||||
assert(proxyProcess.stdout);
|
||||
|
||||
proxyProcess.stderr.on("data", (data: Buffer) => {
|
||||
if (data.includes("http: TLS handshake error")) {
|
||||
return;
|
||||
}
|
||||
|
||||
broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: data.toString(),
|
||||
});
|
||||
});
|
||||
|
||||
proxyProcess.stdout.on("data", (data: Buffer) => {
|
||||
if (typeof port === "number") {
|
||||
broadcastConnectionUpdate({
|
||||
level: "info",
|
||||
message: data.toString(),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
port = await getPortFromStream(proxyProcess.stdout, {
|
||||
lineRegex: startingServeRegex,
|
||||
onFind: () => broadcastConnectionUpdate({
|
||||
level: "info",
|
||||
message: "Authentication proxy started",
|
||||
}),
|
||||
});
|
||||
|
||||
logger.info(`[KUBE-AUTH-PROXY]: found port=${port}`);
|
||||
|
||||
try {
|
||||
await waitUntilPortIsUsed(port, 500, 10000);
|
||||
ready.set(true);
|
||||
} catch (error) {
|
||||
logger.warn("[KUBE-AUTH-PROXY]: waitUntilUsed failed", error);
|
||||
broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: "Proxy port failed to be used within time limit, restarting...",
|
||||
});
|
||||
exit();
|
||||
|
||||
return run();
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
apiPrefix,
|
||||
exit,
|
||||
run,
|
||||
get port() {
|
||||
assert(port, "port has not yet been initialized");
|
||||
|
||||
return port;
|
||||
},
|
||||
};
|
||||
};
|
||||
},
|
||||
lifecycle: lifecycleEnum.keyedSingleton({
|
||||
getInstanceKey: (di, cluster: Cluster) => cluster.id,
|
||||
}),
|
||||
});
|
||||
|
||||
export default createKubeAuthProxyInjectable;
|
||||
@@ -1,168 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import type { ChildProcess } from "child_process";
|
||||
import { randomBytes } from "crypto";
|
||||
import type { Cluster } from "../../common/cluster/cluster";
|
||||
import type { GetPortFromStream } from "../utils/get-port-from-stream.injectable";
|
||||
import { observable, when } from "mobx";
|
||||
import type { SelfSignedCert } from "selfsigned";
|
||||
import assert from "assert";
|
||||
import { TypedRegEx } from "typed-regex";
|
||||
import type { Spawn } from "../child-process/spawn.injectable";
|
||||
import type { Logger } from "../../common/logger";
|
||||
import type { WaitUntilPortIsUsed } from "./wait-until-port-is-used/wait-until-port-is-used.injectable";
|
||||
import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable";
|
||||
import type { BroadcastConnectionUpdate } from "../cluster/broadcast-connection-update.injectable";
|
||||
import type { KubeAuthProxy } from "./create-kube-auth-proxy.injectable";
|
||||
|
||||
const startingServeMatcher = "starting to serve on (?<address>.+)";
|
||||
const startingServeRegex = Object.assign(TypedRegEx(startingServeMatcher, "i"), {
|
||||
rawMatcher: startingServeMatcher,
|
||||
});
|
||||
|
||||
export interface KubeAuthProxyDependencies {
|
||||
readonly proxyBinPath: string;
|
||||
readonly proxyCert: SelfSignedCert;
|
||||
readonly logger: Logger;
|
||||
spawn: Spawn;
|
||||
waitUntilPortIsUsed: WaitUntilPortIsUsed;
|
||||
getPortFromStream: GetPortFromStream;
|
||||
dirname: GetDirnameOfPath;
|
||||
broadcastConnectionUpdate: BroadcastConnectionUpdate;
|
||||
}
|
||||
|
||||
export class KubeAuthProxyImpl implements KubeAuthProxy {
|
||||
public readonly apiPrefix = `/${randomBytes(8).toString("hex")}`;
|
||||
|
||||
public get port(): number {
|
||||
const port = this._port;
|
||||
|
||||
assert(port, "port has not yet been initialized");
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
protected _port?: number;
|
||||
protected proxyProcess?: ChildProcess;
|
||||
protected readonly ready = observable.box(false);
|
||||
|
||||
constructor(
|
||||
private readonly dependencies: KubeAuthProxyDependencies,
|
||||
protected readonly cluster: Cluster,
|
||||
protected readonly env: NodeJS.ProcessEnv,
|
||||
) {}
|
||||
|
||||
public async run(): Promise<void> {
|
||||
if (this.proxyProcess) {
|
||||
return when(() => this.ready.get());
|
||||
}
|
||||
|
||||
const proxyBin = this.dependencies.proxyBinPath;
|
||||
const cert = this.dependencies.proxyCert;
|
||||
|
||||
this.proxyProcess = this.dependencies.spawn(proxyBin, [], {
|
||||
env: {
|
||||
...this.env,
|
||||
KUBECONFIG: this.cluster.kubeConfigPath.get(),
|
||||
KUBECONFIG_CONTEXT: this.cluster.contextName.get(),
|
||||
API_PREFIX: this.apiPrefix,
|
||||
PROXY_KEY: cert.private,
|
||||
PROXY_CERT: cert.cert,
|
||||
},
|
||||
cwd: this.dependencies.dirname(this.cluster.kubeConfigPath.get()),
|
||||
});
|
||||
this.proxyProcess.on("error", (error) => {
|
||||
this.dependencies.broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: error.message,
|
||||
});
|
||||
this.exit();
|
||||
});
|
||||
|
||||
this.proxyProcess.on("exit", (code) => {
|
||||
if (code) {
|
||||
this.dependencies.broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: `proxy exited with code: ${code}`,
|
||||
});
|
||||
} else {
|
||||
this.dependencies.broadcastConnectionUpdate({
|
||||
level: "info",
|
||||
message: "proxy exited successfully",
|
||||
});
|
||||
}
|
||||
this.exit();
|
||||
});
|
||||
|
||||
this.proxyProcess.on("disconnect", () => {
|
||||
this.dependencies.broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: "Proxy disconnected communications",
|
||||
});
|
||||
this.exit();
|
||||
});
|
||||
|
||||
assert(this.proxyProcess.stderr);
|
||||
assert(this.proxyProcess.stdout);
|
||||
|
||||
this.proxyProcess.stderr.on("data", (data: Buffer) => {
|
||||
if (data.includes("http: TLS handshake error")) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.dependencies.broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: data.toString(),
|
||||
});
|
||||
});
|
||||
|
||||
this.proxyProcess.stdout.on("data", (data: Buffer) => {
|
||||
if (typeof this._port === "number") {
|
||||
this.dependencies.broadcastConnectionUpdate({
|
||||
level: "info",
|
||||
message: data.toString(),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
this._port = await this.dependencies.getPortFromStream(this.proxyProcess.stdout, {
|
||||
lineRegex: startingServeRegex,
|
||||
onFind: () => this.dependencies.broadcastConnectionUpdate({
|
||||
level: "info",
|
||||
message: "Authentication proxy started",
|
||||
}),
|
||||
});
|
||||
|
||||
this.dependencies.logger.info(`[KUBE-AUTH-PROXY]: found port=${this._port}`);
|
||||
|
||||
try {
|
||||
await this.dependencies.waitUntilPortIsUsed(this.port, 500, 10000);
|
||||
this.ready.set(true);
|
||||
} catch (error) {
|
||||
this.dependencies.logger.warn("[KUBE-AUTH-PROXY]: waitUntilUsed failed", error);
|
||||
this.dependencies.broadcastConnectionUpdate({
|
||||
level: "error",
|
||||
message: "Proxy port failed to be used within time limit, restarting...",
|
||||
});
|
||||
this.exit();
|
||||
|
||||
return this.run();
|
||||
}
|
||||
}
|
||||
|
||||
public exit() {
|
||||
this.ready.set(false);
|
||||
|
||||
if (this.proxyProcess) {
|
||||
this.dependencies.logger.debug("[KUBE-AUTH]: stopping local proxy", this.cluster.getMeta());
|
||||
this.proxyProcess.removeAllListeners();
|
||||
this.proxyProcess.stderr?.removeAllListeners();
|
||||
this.proxyProcess.stdout?.removeAllListeners();
|
||||
this.proxyProcess.kill();
|
||||
this.proxyProcess = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user