Fix downloading cluster specific kubectl (#5399)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-06-02 18:40:30 -04:00
1 parent ac27077ef9
commit e1c1e00a2b
22 files changed
+251 -63

No files matched your search

@@ -0,0 +1,19 @@
/**
* 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 normalizedPlatformInjectable from "../../common/vars/normalized-platform.injectable";
const kubectlBinaryNameInjectable = getInjectable({
id: "kubectl-binary-name",
instantiate: (di) => {
const platform = di.inject(normalizedPlatformInjectable);
return platform === "windows"
? "kubectl.exe"
: "kubectl";
},
});
export default kubectlBinaryNameInjectable;
@@ -0,0 +1,18 @@
/**
* 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 path from "path";
import baseBundeledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.injectable";
import kubectlBinaryNameInjectable from "./binary-name.injectable";
const bundledKubectlBinaryPathInjectable = getInjectable({
id: "bundled-kubectl-binary-path",
instantiate: (di) => path.join(
di.inject(baseBundeledBinariesDirectoryInjectable),
di.inject(kubectlBinaryNameInjectable),
),
});
export default bundledKubectlBinaryPathInjectable;
+14 -7
View File
@@ -3,24 +3,31 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { KubectlDependencies } from "./kubectl";
import { Kubectl } from "./kubectl";
import directoryForKubectlBinariesInjectable from "../../common/app-paths/directory-for-kubectl-binaries/directory-for-kubectl-binaries.injectable";
import userStoreInjectable from "../../common/user-store/user-store.injectable";
import kubectlDownloadingNormalizedArchInjectable from "./normalized-arch.injectable";
import normalizedPlatformInjectable from "../../common/vars/normalized-platform.injectable";
import kubectlBinaryNameInjectable from "./binary-name.injectable";
import bundledKubectlBinaryPathInjectable from "./bundled-binary-path.injectable";
import baseBundeledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.injectable";
const createKubectlInjectable = getInjectable({
id: "create-kubectl",
instantiate: (di) => {
const dependencies = {
const dependencies: KubectlDependencies = {
userStore: di.inject(userStoreInjectable),
directoryForKubectlBinaries: di.inject(
directoryForKubectlBinariesInjectable,
),
directoryForKubectlBinaries: di.inject(directoryForKubectlBinariesInjectable),
normalizedDownloadArch: di.inject(kubectlDownloadingNormalizedArchInjectable),
normalizedDownloadPlatform: di.inject(normalizedPlatformInjectable),
kubectlBinaryName: di.inject(kubectlBinaryNameInjectable),
bundledKubectlBinaryPath: di.inject(bundledKubectlBinaryPathInjectable),
baseBundeledBinariesDirectory: di.inject(baseBundeledBinariesDirectoryInjectable),
};
return (clusterVersion: string) =>
new Kubectl(dependencies, clusterVersion);
return (clusterVersion: string) => new Kubectl(dependencies, clusterVersion);
},
});
+23 -21
View File
@@ -10,7 +10,6 @@ import logger from "../logger";
import { ensureDir, pathExists } from "fs-extra";
import * as lockFile from "proper-lockfile";
import { getBundledKubectlVersion } from "../../common/utils/app-version";
import { normalizedPlatform, normalizedArch, kubectlBinaryName, kubectlBinaryPath, baseBinariesDir } from "../../common/vars";
import { SemVer } from "semver";
import { defaultPackageMirror, packageMirrors } from "../../common/user-store/preferences-helpers";
import got from "got/dist/source";
@@ -40,27 +39,31 @@ const kubectlMap: Map<string, string> = new Map([
]);
const initScriptVersionString = "# lens-initscript v3";
interface Dependencies {
directoryForKubectlBinaries: string;
userStore: {
kubectlBinariesPath?: string;
downloadBinariesPath?: string;
downloadKubectlBinaries: boolean;
downloadMirror: string;
export interface KubectlDependencies {
readonly directoryForKubectlBinaries: string;
readonly normalizedDownloadPlatform: "darwin" | "linux" | "windows";
readonly normalizedDownloadArch: "amd64" | "arm64" | "386";
readonly kubectlBinaryName: string;
readonly bundledKubectlBinaryPath: string;
readonly baseBundeledBinariesDirectory: string;
readonly userStore: {
readonly kubectlBinariesPath?: string;
readonly downloadBinariesPath?: string;
readonly downloadKubectlBinaries: boolean;
readonly downloadMirror: string;
};
}
export class Kubectl {
public kubectlVersion: string;
protected url: string;
protected path: string;
protected dirname: string;
public readonly kubectlVersion: string;
protected readonly url: string;
protected readonly path: string;
protected readonly dirname: string;
public static readonly bundledKubectlVersion = bundledVersion;
public static invalidBundle = false;
constructor(private dependencies: Dependencies, clusterVersion: string) {
constructor(protected readonly dependencies: KubectlDependencies, clusterVersion: string) {
let version: SemVer;
try {
@@ -83,13 +86,13 @@ export class Kubectl {
logger.debug(`Set kubectl version ${this.kubectlVersion} for cluster version ${clusterVersion} using fallback`);
}
this.url = `${this.getDownloadMirror()}/v${this.kubectlVersion}/bin/${normalizedPlatform}/${normalizedArch}/${kubectlBinaryName}`;
this.url = `${this.getDownloadMirror()}/v${this.kubectlVersion}/bin/${this.dependencies.normalizedDownloadPlatform}/${this.dependencies.normalizedDownloadArch}/${this.dependencies.kubectlBinaryName}`;
this.dirname = path.normalize(path.join(this.getDownloadDir(), this.kubectlVersion));
this.path = path.join(this.dirname, kubectlBinaryName);
this.path = path.join(this.dirname, this.dependencies.kubectlBinaryName);
}
public getBundledPath() {
return kubectlBinaryPath.get();
return this.dependencies.bundledKubectlBinaryPath;
}
public getPathFromPreferences() {
@@ -279,12 +282,11 @@ export class Kubectl {
}
protected async writeInitScripts() {
const binariesDir = this.dependencies.baseBundeledBinariesDirectory;
const kubectlPath = this.dependencies.userStore.downloadKubectlBinaries
? this.dirname
: path.dirname(this.getPathFromPreferences());
const binariesDir = baseBinariesDir.get();
const bashScriptPath = path.join(this.dirname, ".bash_set_path");
const bashScript = [
initScriptVersionString,
@@ -297,7 +299,7 @@ export class Kubectl {
"elif test -f \"$HOME/.profile\"; then",
" . \"$HOME/.profile\"",
"fi",
`export PATH="${binariesDir}:${kubectlPath}:$PATH"`,
`export PATH="${kubectlPath}:${binariesDir}:$PATH"`,
'export KUBECONFIG="$tempkubeconfig"',
`NO_PROXY=",\${NO_PROXY:-localhost},"`,
`NO_PROXY="\${NO_PROXY//,localhost,/,}"`,
@@ -328,7 +330,7 @@ export class Kubectl {
"d=\":$PATH:\"",
`d=\${d//$p/:}`,
`d=\${d/#:/}`,
`export PATH="$binariesDir:$kubectlpath:\${d/%:/}"`,
`export PATH="$kubectlpath:$binariesDir:\${d/%:/}"`,
"export KUBECONFIG=\"$tempkubeconfig\"",
`NO_PROXY=",\${NO_PROXY:-localhost},"`,
`NO_PROXY="\${NO_PROXY//,localhost,/,}"`,
@@ -0,0 +1,27 @@
/**
* 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";
const kubectlDownloadingNormalizedArchInjectable = getInjectable({
id: "kubectl-downloading-normalized-arch",
instantiate: () => {
switch (process.arch) {
case "arm64":
return "arm64";
case "x64":
case "amd64":
return "amd64";
case "386":
case "x32":
case "ia32":
return "386";
default:
throw new Error(`arch=${process.arch} is unsupported`);
}
},
causesSideEffects: true,
});
export default kubectlDownloadingNormalizedArchInjectable;