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

Fix bundled kubectl path on dev env (#981)

* fix bundled kubectl path on dev env

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix specs

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2020-10-14 14:35:33 +03:00 committed by GitHub
parent db9af88d91
commit 78f6f8ef54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 28 deletions

View File

@ -2,7 +2,7 @@ import { ChildProcess, spawn } from "child_process"
import { waitUntilUsed } from "tcp-port-used";
import { broadcastIpc } from "../common/ipc";
import type { Cluster } from "./cluster"
import { bundledKubectl, Kubectl } from "./kubectl"
import { Kubectl } from "./kubectl"
import logger from "./logger"
export interface KubeAuthProxyLog {
@ -23,7 +23,7 @@ export class KubeAuthProxy {
this.env = env
this.port = port
this.cluster = cluster
this.kubectl = bundledKubectl
this.kubectl = Kubectl.bundled()
}
public async run(): Promise<void> {

View File

@ -36,6 +36,9 @@ const packageMirrors: Map<string, string> = new Map([
let bundledPath: string
const initScriptVersionString = "# lens-initscript v3\n"
export function bundledKubectlPath(): string {
if (bundledPath) { return bundledPath }
if (isDevelopment || isTestEnv) {
const platformName = isWindows ? "windows" : process.platform
bundledPath = path.join(process.cwd(), "binaries", "client", platformName, process.arch, "kubectl")
@ -47,6 +50,9 @@ if (isWindows) {
bundledPath = `${bundledPath}.exe`
}
return bundledPath
}
export class Kubectl {
public kubectlVersion: string
protected directory: string
@ -58,7 +64,6 @@ export class Kubectl {
return path.join((app || remote.app).getPath("userData"), "binaries", "kubectl")
}
public static readonly bundledKubectlPath = bundledPath
public static readonly bundledKubectlVersion: string = bundledVersion
public static invalidBundle = false
private static bundledInstance: Kubectl;
@ -102,7 +107,7 @@ export class Kubectl {
}
public getBundledPath() {
return Kubectl.bundledKubectlPath
return bundledKubectlPath()
}
public getPathFromPreferences() {
@ -125,19 +130,19 @@ export class Kubectl {
// return binary name if bundled path is not functional
if (!await this.checkBinary(this.getBundledPath(), false)) {
Kubectl.invalidBundle = true
return path.basename(bundledPath)
return path.basename(this.getBundledPath())
}
try {
if (!await this.ensureKubectl()) {
logger.error("Failed to ensure kubectl, fallback to the bundled version")
return Kubectl.bundledKubectlPath
return this.getBundledPath()
}
return this.path
} catch (err) {
logger.error("Failed to ensure kubectl, fallback to the bundled version")
logger.error(err)
return Kubectl.bundledKubectlPath
return this.getBundledPath()
}
}
@ -183,7 +188,7 @@ export class Kubectl {
try {
const exist = await pathExists(this.path)
if (!exist) {
await fs.promises.copyFile(Kubectl.bundledKubectlPath, this.path)
await fs.promises.copyFile(this.getBundledPath(), this.path)
await fs.promises.chmod(this.path, 0o755)
}
return true
@ -332,6 +337,3 @@ export class Kubectl {
return packageMirrors.get("default") // MacOS packages are only available from default
}
}
const bundledKubectl = Kubectl.bundled()
export { bundledKubectl }

View File

@ -1,20 +1,20 @@
import packageInfo from "../../package.json"
import path from "path"
import { bundledKubectl, Kubectl } from "../../src/main/kubectl";
import { Kubectl } from "../../src/main/kubectl";
import { isWindows } from "../common/vars";
jest.mock("../common/user-store");
describe("kubectlVersion", () => {
it("returns bundled version if exactly same version used", async () => {
const kubectl = new Kubectl(bundledKubectl.kubectlVersion)
expect(kubectl.kubectlVersion).toBe(bundledKubectl.kubectlVersion)
const kubectl = new Kubectl(Kubectl.bundled().kubectlVersion)
expect(kubectl.kubectlVersion).toBe(Kubectl.bundled().kubectlVersion)
})
it("returns bundled version if same major.minor version is used", async () => {
const { bundledKubectlVersion } = packageInfo.config;
const kubectl = new Kubectl(bundledKubectlVersion);
expect(kubectl.kubectlVersion).toBe(bundledKubectl.kubectlVersion)
expect(kubectl.kubectlVersion).toBe(Kubectl.bundled().kubectlVersion)
})
})

View File

@ -1,7 +1,7 @@
import { LensApiRequest } from "../router"
import { LensApi } from "../lens-api"
import { spawn, ChildProcessWithoutNullStreams } from "child_process"
import { bundledKubectl } from "../kubectl"
import { Kubectl } from "../kubectl"
import { getFreePort } from "../port"
import { shell } from "electron"
import * as tcpPortUsed from "tcp-port-used"
@ -37,7 +37,7 @@ class PortForward {
public async start() {
this.localPort = await getFreePort()
const kubectlBin = await bundledKubectl.getPath()
const kubectlBin = await Kubectl.bundled().getPath()
const args = [
"--kubeconfig", this.kubeConfig,
"port-forward",

View File

@ -38,7 +38,7 @@ export class ShellSession extends EventEmitter {
public async open() {
this.kubectlBinDir = await this.kubectl.binDir()
const pathFromPreferences = userStore.preferences.kubectlBinariesPath || Kubectl.bundledKubectlPath
const pathFromPreferences = userStore.preferences.kubectlBinariesPath || this.kubectl.getBundledPath()
this.kubectlPathDir = userStore.preferences.downloadKubectlBinaries ? this.kubectlBinDir : path.dirname(pathFromPreferences)
this.helmBinDir = helmCli.getBinaryDir()
const env = await this.getCachedShellEnv()

View File

@ -6,7 +6,7 @@ import { Input } from '../input';
import { SubTitle } from '../layout/sub-title';
import { UserPreferences, userStore } from '../../../common/user-store';
import { observer } from 'mobx-react';
import { Kubectl } from '../../../main/kubectl';
import { bundledKubectlPath } from '../../../main/kubectl';
import { SelectOption, Select } from '../select';
export const KubectlBinaries = observer(({ preferences }: { preferences: UserPreferences }) => {
@ -58,7 +58,7 @@ export const KubectlBinaries = observer(({ preferences }: { preferences: UserPre
<SubTitle title="Path to Kubectl binary" />
<Input
theme="round-black"
placeholder={Kubectl.bundledKubectlPath}
placeholder={bundledKubectlPath()}
value={binariesPath}
validators={isPath}
onChange={setBinariesPath}