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

replace kubectl-proxy with lens-k8s-proxy

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2022-02-23 09:53:11 +02:00
parent 12a6bd8512
commit c8ac776d25
5 changed files with 140 additions and 25 deletions

View File

@ -0,0 +1,98 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import packageInfo from "../package.json";
import fs from "fs";
import request from "request";
import { ensureDir, pathExists } from "fs-extra";
import path from "path";
import { noop } from "lodash";
import { isLinux, isMac } from "../src/common/vars";
class K8sProxyDownloader {
public version: string;
protected url: string;
protected path: string;
protected dirname: string;
constructor(version: string, platform: string, arch: string, target: string) {
this.version = version;
this.url = `https://github.com/lensapp/lens-k8s-proxy/releases/download/v${this.version}/lens-k8s-proxy-${platform}-${arch}`;
this.dirname = path.dirname(target);
this.path = target;
}
public async checkBinary() {
const exists = await pathExists(this.path);
if (exists) {
return true;
}
return false;
}
public async download() {
if (await this.checkBinary()) {
return console.log("Already exists");
}
await ensureDir(path.dirname(this.path), 0o755);
const file = fs.createWriteStream(this.path);
console.log(`Downloading lens-k8s-version ${this.version} from ${this.url} to ${this.path}`);
const requestOpts: request.UriOptions & request.CoreOptions = {
uri: this.url,
gzip: true,
followAllRedirects: true,
};
const stream = request(requestOpts);
stream.on("complete", () => {
console.log("lens-k8s-version binary download finished");
file.end(noop);
});
stream.on("error", (error) => {
console.log(error);
fs.unlink(this.path, noop);
throw error;
});
return new Promise<void>((resolve, reject) => {
file.on("close", () => {
console.log("lens-k8s-version binary download closed");
fs.chmod(this.path, 0o755, (err) => {
if (err) reject(err);
});
resolve();
});
stream.pipe(file);
});
}
}
const downloadVersion = packageInfo.config.k8sProxyVersion;
const baseDir = path.join(__dirname, "..", "binaries", "client");
const downloads = [];
if (isMac) {
downloads.push({ platform: "darwin", arch: "amd64", target: path.join(baseDir, "darwin", "x64", "lens-k8s-proxy") });
downloads.push({ platform: "darwin", arch: "arm64", target: path.join(baseDir, "darwin", "arm64", "lens-k8s-proxy") });
} else if (isLinux) {
downloads.push({ platform: "linux", arch: "amd64", target: path.join(baseDir, "linux", "x64", "lens-k8s-proxy") });
downloads.push({ platform: "linux", arch: "arm64", target: path.join(baseDir, "linux", "arm64", "lens-k8s-proxy") });
} else {
downloads.push({ platform: "windows", arch: "amd64", target: path.join(baseDir, "windows", "x64", "lens-k8s-proxy.exe") });
downloads.push({ platform: "windows", arch: "386", target: path.join(baseDir, "windows", "ia32", "lens-k8s-proxy.exe") });
}
downloads.forEach((dlOpts) => {
console.log(dlOpts);
const downloader = new K8sProxyDownloader(downloadVersion, dlOpts.platform, dlOpts.arch, dlOpts.target);
console.log(`Downloading: ${JSON.stringify(dlOpts)}`);
downloader.download().then(() => downloader.checkBinary().then(() => console.log("Download complete")));
});

View File

@ -34,6 +34,7 @@
"download-bins": "concurrently yarn:download:*",
"download:kubectl": "yarn run ts-node build/download_kubectl.ts",
"download:helm": "yarn run ts-node build/download_helm.ts",
"download:k8s-proxy": "yarn run ts-node build/download_k8s_proxy.ts",
"build:tray-icons": "yarn run ts-node build/build_tray_icon.ts",
"build:theme-vars": "yarn run ts-node build/build_theme_vars.ts",
"lint": "PROD=true yarn run eslint --ext js,ts,tsx --max-warnings=0 .",
@ -47,6 +48,7 @@
"postversion": "git push --set-upstream ${GIT_REMOTE:-origin} release/v$npm_package_version"
},
"config": {
"k8sProxyVersion": "0.1.1",
"bundledKubectlVersion": "1.23.3",
"bundledHelmVersion": "3.7.2",
"sentryDsn": ""
@ -129,6 +131,10 @@
"from": "binaries/client/linux/${arch}/kubectl",
"to": "./${arch}/kubectl"
},
{
"from": "binaries/client/linux/${arch}/lens-k8s-proxy",
"to": "./${arch}/lens-k8s-proxy"
},
{
"from": "binaries/client/${arch}/helm3/helm3",
"to": "./helm3/helm3"
@ -150,6 +156,10 @@
"from": "binaries/client/darwin/${arch}/kubectl",
"to": "./${arch}/kubectl"
},
{
"from": "binaries/client/darwin/${arch}/lens-k8s-proxy",
"to": "./${arch}/lens-k8s-proxy"
},
{
"from": "binaries/client/${arch}/helm3/helm3",
"to": "./helm3/helm3"
@ -169,6 +179,14 @@
"from": "binaries/client/windows/ia32/kubectl.exe",
"to": "./ia32/kubectl.exe"
},
{
"from": "binaries/client/windows/x64/lens-k8s-proxy",
"to": "./x64/lens-k8s-proxy.exe"
},
{
"from": "binaries/client/windows/ia32/lens-k8s-proxy",
"to": "./ia32/lens-k8s-proxy.exe"
},
{
"from": "binaries/client/x64/helm3/helm3.exe",
"to": "./helm3/helm3.exe"

View File

@ -4,12 +4,17 @@
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import path from "path";
import { isDevelopment, contextDir } from "../../vars";
import directoryForUserDataInjectable from "../directory-for-user-data/directory-for-user-data.injectable";
const directoryForBinariesInjectable = getInjectable({
instantiate: (di) =>
path.join(di.inject(directoryForUserDataInjectable), "binaries"),
instantiate: (di) => {
if (isDevelopment) {
return path.join(contextDir, "binaries");
}
return path.join(di.inject(directoryForUserDataInjectable), "binaries");
},
lifecycle: lifecycleEnum.singleton,
});

View File

@ -5,14 +5,16 @@
import { getInjectable, lifecycleEnum } 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 directoryForBinariesInjectable from "../../common/app-paths/directory-for-binaries/directory-for-binaries.injectable";
import path from "path";
import { isDevelopment, isWindows } from "../../common/vars";
const createKubeAuthProxyInjectable = getInjectable({
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(directoryForBinariesInjectable), proxyPath, binaryName),
};
return (cluster: Cluster, environmentVariables: NodeJS.ProcessEnv) =>

View File

@ -15,11 +15,11 @@ import { makeObservable, observable, when } from "mobx";
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;
@ -45,23 +45,15 @@ 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,
API_PREFIX: this.apiPrefix,
},
});
this.proxyProcess.on("error", (error) => {
this.cluster.broadcastConnectUpdate(error.message, true);
this.exit();