mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Replace kubectl-proxy with lens-k8s-proxy (#4924)
This commit is contained in:
1 parent
f5a7d64551
commit
c04a3b8d33
10 files changed
+191
-31
No files matched your search
@@ -5,16 +5,18 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { KubeAuthProxy } from "./kube-auth-proxy";
|
||||
import type { Cluster } from "../../common/cluster/cluster";
|
||||
import bundledKubectlInjectable from "../kubectl/bundled-kubectl.injectable";
|
||||
import path from "path";
|
||||
import { isDevelopment, isWindows } from "../../common/vars";
|
||||
import directoryForBundledBinariesInjectable from "../../common/app-paths/directory-for-bundled-binaries/directory-for-bundled-binaries.injectable";
|
||||
|
||||
const createKubeAuthProxyInjectable = getInjectable({
|
||||
id: "create-kube-auth-proxy",
|
||||
|
||||
instantiate: (di) => {
|
||||
const bundledKubectl = di.inject(bundledKubectlInjectable);
|
||||
|
||||
const binaryName = isWindows ? "lens-k8s-proxy.exe" : "lens-k8s-proxy";
|
||||
const proxyPath = isDevelopment ? path.join("client", process.platform, process.arch) : process.arch;
|
||||
const dependencies = {
|
||||
getProxyBinPath: bundledKubectl.getPath,
|
||||
proxyBinPath: path.join(di.inject(directoryForBundledBinariesInjectable), proxyPath, binaryName),
|
||||
};
|
||||
|
||||
return (cluster: Cluster, environmentVariables: NodeJS.ProcessEnv) =>
|
||||
|
||||
@@ -8,18 +8,17 @@ import { waitUntilUsed } from "tcp-port-used";
|
||||
import { randomBytes } from "crypto";
|
||||
import type { Cluster } from "../../common/cluster/cluster";
|
||||
import logger from "../logger";
|
||||
import * as url from "url";
|
||||
import { getPortFrom } from "../utils/get-port";
|
||||
import { makeObservable, observable, when } from "mobx";
|
||||
|
||||
const startingServeRegex = /^starting to serve on (?<address>.+)/i;
|
||||
const startingServeRegex = /starting to serve on (?<address>.+)/i;
|
||||
|
||||
interface Dependencies {
|
||||
getProxyBinPath: () => Promise<string>;
|
||||
proxyBinPath: string;
|
||||
}
|
||||
|
||||
export class KubeAuthProxy {
|
||||
public readonly apiPrefix = `/${randomBytes(8).toString("hex")}`;
|
||||
public readonly apiPrefix = `/${randomBytes(8).toString("hex")}/`;
|
||||
|
||||
public get port(): number {
|
||||
return this._port;
|
||||
@@ -27,13 +26,10 @@ export class KubeAuthProxy {
|
||||
|
||||
protected _port: number;
|
||||
protected proxyProcess?: ChildProcess;
|
||||
protected readonly acceptHosts: string;
|
||||
@observable protected ready = false;
|
||||
|
||||
constructor(private dependencies: Dependencies, protected readonly cluster: Cluster, protected readonly env: NodeJS.ProcessEnv) {
|
||||
makeObservable(this);
|
||||
|
||||
this.acceptHosts = url.parse(this.cluster.apiUrl).hostname;
|
||||
}
|
||||
|
||||
get whenReady() {
|
||||
@@ -45,23 +41,16 @@ export class KubeAuthProxy {
|
||||
return this.whenReady;
|
||||
}
|
||||
|
||||
const proxyBin = await this.dependencies.getProxyBinPath();
|
||||
const args = [
|
||||
"proxy",
|
||||
"-p", "0",
|
||||
"--kubeconfig", `${this.cluster.kubeConfigPath}`,
|
||||
"--context", `${this.cluster.contextName}`,
|
||||
"--accept-hosts", this.acceptHosts,
|
||||
"--reject-paths", "^[^/]",
|
||||
"--api-prefix", this.apiPrefix,
|
||||
];
|
||||
|
||||
if (process.env.DEBUG_PROXY === "true") {
|
||||
args.push("-v", "9");
|
||||
}
|
||||
logger.debug(`spawning kubectl proxy with args: ${args}`);
|
||||
|
||||
this.proxyProcess = spawn(proxyBin, args, { env: this.env });
|
||||
const proxyBin = this.dependencies.proxyBinPath;
|
||||
|
||||
this.proxyProcess = spawn(proxyBin, [], {
|
||||
env: {
|
||||
...this.env,
|
||||
KUBECONFIG: this.cluster.kubeConfigPath,
|
||||
KUBECONFIG_CONTEXT: this.cluster.contextName,
|
||||
API_PREFIX: this.apiPrefix,
|
||||
},
|
||||
});
|
||||
this.proxyProcess.on("error", (error) => {
|
||||
this.cluster.broadcastConnectUpdate(error.message, true);
|
||||
this.exit();
|
||||
|
||||
Reference in new issue
Block a user