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

View File

@ -36,15 +36,21 @@ const packageMirrors: Map<string, string> = new Map([
let bundledPath: string let bundledPath: string
const initScriptVersionString = "# lens-initscript v3\n" const initScriptVersionString = "# lens-initscript v3\n"
if (isDevelopment || isTestEnv) { export function bundledKubectlPath(): string {
if (bundledPath) { return bundledPath }
if (isDevelopment || isTestEnv) {
const platformName = isWindows ? "windows" : process.platform const platformName = isWindows ? "windows" : process.platform
bundledPath = path.join(process.cwd(), "binaries", "client", platformName, process.arch, "kubectl") bundledPath = path.join(process.cwd(), "binaries", "client", platformName, process.arch, "kubectl")
} else { } else {
bundledPath = path.join(process.resourcesPath, process.arch, "kubectl") bundledPath = path.join(process.resourcesPath, process.arch, "kubectl")
} }
if (isWindows) { if (isWindows) {
bundledPath = `${bundledPath}.exe` bundledPath = `${bundledPath}.exe`
}
return bundledPath
} }
export class Kubectl { export class Kubectl {
@ -58,7 +64,6 @@ export class Kubectl {
return path.join((app || remote.app).getPath("userData"), "binaries", "kubectl") return path.join((app || remote.app).getPath("userData"), "binaries", "kubectl")
} }
public static readonly bundledKubectlPath = bundledPath
public static readonly bundledKubectlVersion: string = bundledVersion public static readonly bundledKubectlVersion: string = bundledVersion
public static invalidBundle = false public static invalidBundle = false
private static bundledInstance: Kubectl; private static bundledInstance: Kubectl;
@ -102,7 +107,7 @@ export class Kubectl {
} }
public getBundledPath() { public getBundledPath() {
return Kubectl.bundledKubectlPath return bundledKubectlPath()
} }
public getPathFromPreferences() { public getPathFromPreferences() {
@ -125,19 +130,19 @@ export class Kubectl {
// return binary name if bundled path is not functional // return binary name if bundled path is not functional
if (!await this.checkBinary(this.getBundledPath(), false)) { if (!await this.checkBinary(this.getBundledPath(), false)) {
Kubectl.invalidBundle = true Kubectl.invalidBundle = true
return path.basename(bundledPath) return path.basename(this.getBundledPath())
} }
try { try {
if (!await this.ensureKubectl()) { if (!await this.ensureKubectl()) {
logger.error("Failed to ensure kubectl, fallback to the bundled version") logger.error("Failed to ensure kubectl, fallback to the bundled version")
return Kubectl.bundledKubectlPath return this.getBundledPath()
} }
return this.path return this.path
} catch (err) { } catch (err) {
logger.error("Failed to ensure kubectl, fallback to the bundled version") logger.error("Failed to ensure kubectl, fallback to the bundled version")
logger.error(err) logger.error(err)
return Kubectl.bundledKubectlPath return this.getBundledPath()
} }
} }
@ -183,7 +188,7 @@ export class Kubectl {
try { try {
const exist = await pathExists(this.path) const exist = await pathExists(this.path)
if (!exist) { 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) await fs.promises.chmod(this.path, 0o755)
} }
return true return true
@ -332,6 +337,3 @@ export class Kubectl {
return packageMirrors.get("default") // MacOS packages are only available from default 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 packageInfo from "../../package.json"
import path from "path" import path from "path"
import { bundledKubectl, Kubectl } from "../../src/main/kubectl"; import { Kubectl } from "../../src/main/kubectl";
import { isWindows } from "../common/vars"; import { isWindows } from "../common/vars";
jest.mock("../common/user-store"); jest.mock("../common/user-store");
describe("kubectlVersion", () => { describe("kubectlVersion", () => {
it("returns bundled version if exactly same version used", async () => { it("returns bundled version if exactly same version used", async () => {
const kubectl = new Kubectl(bundledKubectl.kubectlVersion) const kubectl = new Kubectl(Kubectl.bundled().kubectlVersion)
expect(kubectl.kubectlVersion).toBe(bundledKubectl.kubectlVersion) expect(kubectl.kubectlVersion).toBe(Kubectl.bundled().kubectlVersion)
}) })
it("returns bundled version if same major.minor version is used", async () => { it("returns bundled version if same major.minor version is used", async () => {
const { bundledKubectlVersion } = packageInfo.config; const { bundledKubectlVersion } = packageInfo.config;
const kubectl = new Kubectl(bundledKubectlVersion); 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 { LensApiRequest } from "../router"
import { LensApi } from "../lens-api" import { LensApi } from "../lens-api"
import { spawn, ChildProcessWithoutNullStreams } from "child_process" import { spawn, ChildProcessWithoutNullStreams } from "child_process"
import { bundledKubectl } from "../kubectl" import { Kubectl } from "../kubectl"
import { getFreePort } from "../port" import { getFreePort } from "../port"
import { shell } from "electron" import { shell } from "electron"
import * as tcpPortUsed from "tcp-port-used" import * as tcpPortUsed from "tcp-port-used"
@ -37,7 +37,7 @@ class PortForward {
public async start() { public async start() {
this.localPort = await getFreePort() this.localPort = await getFreePort()
const kubectlBin = await bundledKubectl.getPath() const kubectlBin = await Kubectl.bundled().getPath()
const args = [ const args = [
"--kubeconfig", this.kubeConfig, "--kubeconfig", this.kubeConfig,
"port-forward", "port-forward",

View File

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

View File

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