Cascade different refactorings to eliminate global shared state from app paths

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne SavolainenandMikko Aspiala committed 2022-01-05 13:26:49 +02:00
1 parent 908a40975e
commit 9d33fff906
336 files changed
+9105 -3307

No files matched your search

@@ -0,0 +1,37 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import { getBundledKubectlVersion } from "../../common/utils";
import createKubectlInjectable from "./create-kubectl.injectable";
const bundledKubectlInjectable = getInjectable({
instantiate: (di) => {
const createKubectl = di.inject(createKubectlInjectable);
const bundledKubectlVersion = getBundledKubectlVersion();
return createKubectl(bundledKubectlVersion);
},
lifecycle: lifecycleEnum.singleton,
});
export default bundledKubectlInjectable;
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import { Kubectl } from "./kubectl";
import directoryForKubectlBinariesInjectable from "./directory-for-kubectl-binaries/directory-for-kubectl-binaries.injectable";
import userStoreInjectable from "../../common/user-store/user-store.injectable";
const createKubectlInjectable = getInjectable({
instantiate: (di) => {
const dependencies = {
userStore: di.inject(userStoreInjectable),
directoryForKubectlBinaries: di.inject(
directoryForKubectlBinariesInjectable,
),
};
return (clusterVersion: string) =>
new Kubectl(dependencies, clusterVersion);
},
lifecycle: lifecycleEnum.singleton,
});
export default createKubectlInjectable;
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import directoryForBinariesInjectable from "../../../common/app-paths/directory-for-binaries/directory-for-binaries.injectable";
import path from "path";
const directoryForKubectlBinariesInjectable = getInjectable({
instantiate: (di) =>
path.join(di.inject(directoryForBinariesInjectable), "kubectl"),
lifecycle: lifecycleEnum.singleton,
});
export default directoryForKubectlBinariesInjectable;
+406
View File
@@ -0,0 +1,406 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import path from "path";
import fs from "fs";
import { promiseExecFile } from "../../common/utils/promise-exec";
import logger from "../logger";
import { ensureDir, pathExists } from "fs-extra";
import * as lockFile from "proper-lockfile";
import { helmCli } from "../helm/helm-cli";
import { customRequest } from "../../common/request";
import { getBundledKubectlVersion } from "../../common/utils/app-version";
import { isDevelopment, isWindows, isTestEnv } from "../../common/vars";
import { SemVer } from "semver";
import { defaultPackageMirror, packageMirrors } from "../../common/user-store/preferences-helpers";
const bundledVersion = getBundledKubectlVersion();
const kubectlMap: Map<string, string> = new Map([
["1.7", "1.8.15"],
["1.8", "1.9.10"],
["1.9", "1.10.13"],
["1.10", "1.11.10"],
["1.11", "1.12.10"],
["1.12", "1.13.12"],
["1.13", "1.13.12"],
["1.14", "1.14.10"],
["1.15", "1.15.11"],
["1.16", "1.16.15"],
["1.17", "1.17.17"],
["1.18", "1.18.20"],
["1.19", "1.19.12"],
["1.20", "1.20.8"],
["1.21", bundledVersion],
]);
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");
} else {
bundledPath = path.join(process.resourcesPath, process.arch, "kubectl");
}
if (isWindows) {
bundledPath = `${bundledPath}.exe`;
}
return bundledPath;
}
interface Dependencies {
directoryForKubectlBinaries: string;
userStore: {
kubectlBinariesPath?: string
downloadBinariesPath?: string
downloadKubectlBinaries: boolean
downloadMirror: string
};
}
export class Kubectl {
public kubectlVersion: string;
protected directory: string;
protected url: string;
protected path: string;
protected dirname: string;
public static readonly bundledKubectlVersion: string = bundledVersion;
public static invalidBundle = false;
constructor(private dependencies: Dependencies, clusterVersion: string) {
let version: SemVer;
try {
version = new SemVer(clusterVersion, { includePrerelease: false });
} catch {
version = new SemVer(Kubectl.bundledKubectlVersion);
}
const minorVersion = `${version.major}.${version.minor}`;
/* minorVersion is the first two digits of kube server version
if the version map includes that, use that version, if not, fallback to the exact x.y.z of kube version */
if (kubectlMap.has(minorVersion)) {
this.kubectlVersion = kubectlMap.get(minorVersion);
logger.debug(`Set kubectl version ${this.kubectlVersion} for cluster version ${clusterVersion} using version map`);
} else {
this.kubectlVersion = version.format();
logger.debug(`Set kubectl version ${this.kubectlVersion} for cluster version ${clusterVersion} using fallback`);
}
let arch = null;
if (process.arch == "x64") {
arch = "amd64";
} else if (process.arch == "x86" || process.arch == "ia32") {
arch = "386";
} else {
arch = process.arch;
}
const platformName = isWindows ? "windows" : process.platform;
const binaryName = isWindows ? "kubectl.exe" : "kubectl";
this.url = `${this.getDownloadMirror()}/v${this.kubectlVersion}/bin/${platformName}/${arch}/${binaryName}`;
this.dirname = path.normalize(path.join(this.getDownloadDir(), this.kubectlVersion));
this.path = path.join(this.dirname, binaryName);
}
public getBundledPath() {
return bundledKubectlPath();
}
public getPathFromPreferences() {
return this.dependencies.userStore.kubectlBinariesPath || this.getBundledPath();
}
protected getDownloadDir() {
if (this.dependencies.userStore.downloadBinariesPath) {
return path.join(this.dependencies.userStore.downloadBinariesPath, "kubectl");
}
return this.dependencies.directoryForKubectlBinaries;
}
public getPath = async (bundled = false): Promise<string> => {
if (bundled) {
return this.getBundledPath();
}
if (this.dependencies.userStore.downloadKubectlBinaries === false) {
return this.getPathFromPreferences();
}
// return binary name if bundled path is not functional
if (!await this.checkBinary(this.getBundledPath(), false)) {
Kubectl.invalidBundle = true;
return path.basename(this.getBundledPath());
}
try {
if (!await this.ensureKubectl()) {
logger.error("Failed to ensure kubectl, fallback to the bundled version");
return this.getBundledPath();
}
return this.path;
} catch (err) {
logger.error("Failed to ensure kubectl, fallback to the bundled version");
logger.error(err);
return this.getBundledPath();
}
};
public async binDir() {
try {
await this.ensureKubectl();
await this.writeInitScripts();
return this.dirname;
} catch (err) {
logger.error(err);
return "";
}
}
public async checkBinary(path: string, checkVersion = true) {
const exists = await pathExists(path);
if (exists) {
try {
const args = [
"version",
"--client", "true",
"--output", "json",
];
const { stdout } = await promiseExecFile(path, args);
const output = JSON.parse(stdout);
if (!checkVersion) {
return true;
}
let version: string = output.clientVersion.gitVersion;
if (version[0] === "v") {
version = version.slice(1);
}
if (version === this.kubectlVersion) {
logger.debug(`Local kubectl is version ${this.kubectlVersion}`);
return true;
}
logger.error(`Local kubectl is version ${version}, expected ${this.kubectlVersion}, unlinking`);
} catch (err) {
logger.error(`Local kubectl failed to run properly (${err.message}), unlinking`);
}
await fs.promises.unlink(this.path);
}
return false;
}
protected async checkBundled(): Promise<boolean> {
if (this.kubectlVersion === Kubectl.bundledKubectlVersion) {
try {
const exist = await pathExists(this.path);
if (!exist) {
await fs.promises.copyFile(this.getBundledPath(), this.path);
await fs.promises.chmod(this.path, 0o755);
}
return true;
} catch (err) {
logger.error(`Could not copy the bundled kubectl to app-data: ${err}`);
return false;
}
} else {
return false;
}
}
public async ensureKubectl(): Promise<boolean> {
if (this.dependencies.userStore.downloadKubectlBinaries === false) {
return true;
}
if (Kubectl.invalidBundle) {
logger.error(`Detected invalid bundle binary, returning ...`);
return false;
}
await ensureDir(this.dirname, 0o755);
try {
const release = await lockFile.lock(this.dirname);
logger.debug(`Acquired a lock for ${this.kubectlVersion}`);
const bundled = await this.checkBundled();
let isValid = await this.checkBinary(this.path, !bundled);
if (!isValid && !bundled) {
try {
await this.downloadKubectl();
} catch (error) {
logger.error(`[KUBECTL]: failed to download kubectl`, error);
logger.debug(`[KUBECTL]: Releasing lock for ${this.kubectlVersion}`);
await release();
return false;
}
isValid = await this.checkBinary(this.path, false);
}
if (!isValid) {
logger.debug(`[KUBECTL]: Releasing lock for ${this.kubectlVersion}`);
await release();
return false;
}
logger.debug(`[KUBECTL]: Releasing lock for ${this.kubectlVersion}`);
await release();
return true;
} catch (error) {
logger.error(`[KUBECTL]: Failed to get a lock for ${this.kubectlVersion}`, error);
return false;
}
}
public async downloadKubectl() {
await ensureDir(path.dirname(this.path), 0o755);
logger.info(`Downloading kubectl ${this.kubectlVersion} from ${this.url} to ${this.path}`);
return new Promise<void>((resolve, reject) => {
const stream = customRequest({
url: this.url,
gzip: true,
});
const file = fs.createWriteStream(this.path);
stream.on("complete", () => {
logger.debug("kubectl binary download finished");
file.end();
});
stream.on("error", (error) => {
logger.error(error);
fs.unlink(this.path, () => {
// do nothing
});
reject(error);
});
file.on("close", () => {
logger.debug("kubectl binary download closed");
fs.chmod(this.path, 0o755, (err) => {
if (err) reject(err);
});
resolve();
});
stream.pipe(file);
});
}
protected async writeInitScripts() {
const kubectlPath = this.dependencies.userStore.downloadKubectlBinaries ? this.dirname : path.dirname(this.getPathFromPreferences());
const helmPath = helmCli.getBinaryDir();
const fsPromises = fs.promises;
const bashScriptPath = path.join(this.dirname, ".bash_set_path");
let bashScript = `${initScriptVersionString}`;
bashScript += "tempkubeconfig=\"$KUBECONFIG\"\n";
bashScript += "test -f \"/etc/profile\" && . \"/etc/profile\"\n";
bashScript += "if test -f \"$HOME/.bash_profile\"; then\n";
bashScript += " . \"$HOME/.bash_profile\"\n";
bashScript += "elif test -f \"$HOME/.bash_login\"; then\n";
bashScript += " . \"$HOME/.bash_login\"\n";
bashScript += "elif test -f \"$HOME/.profile\"; then\n";
bashScript += " . \"$HOME/.profile\"\n";
bashScript += "fi\n";
bashScript += `export PATH="${helmPath}:${kubectlPath}:$PATH"\n`;
bashScript += "export KUBECONFIG=\"$tempkubeconfig\"\n";
bashScript += `NO_PROXY=\",\${NO_PROXY:-localhost},\"\n`;
bashScript += `NO_PROXY=\"\${NO_PROXY//,localhost,/,}\"\n`;
bashScript += `NO_PROXY=\"\${NO_PROXY//,127.0.0.1,/,}\"\n`;
bashScript += `NO_PROXY=\"localhost,127.0.0.1\${NO_PROXY%,}\"\n`;
bashScript += "export NO_PROXY\n";
bashScript += "unset tempkubeconfig\n";
await fsPromises.writeFile(bashScriptPath, bashScript.toString(), { mode: 0o644 });
const zshScriptPath = path.join(this.dirname, ".zlogin");
let zshScript = `${initScriptVersionString}`;
zshScript += "tempkubeconfig=\"$KUBECONFIG\"\n";
// restore previous ZDOTDIR
zshScript += "export ZDOTDIR=\"$OLD_ZDOTDIR\"\n";
// source all the files
zshScript += "test -f \"$OLD_ZDOTDIR/.zshenv\" && . \"$OLD_ZDOTDIR/.zshenv\"\n";
zshScript += "test -f \"$OLD_ZDOTDIR/.zprofile\" && . \"$OLD_ZDOTDIR/.zprofile\"\n";
zshScript += "test -f \"$OLD_ZDOTDIR/.zlogin\" && . \"$OLD_ZDOTDIR/.zlogin\"\n";
zshScript += "test -f \"$OLD_ZDOTDIR/.zshrc\" && . \"$OLD_ZDOTDIR/.zshrc\"\n";
// voodoo to replace any previous occurrences of kubectl path in the PATH
zshScript += `kubectlpath=\"${kubectlPath}"\n`;
zshScript += `helmpath=\"${helmPath}"\n`;
zshScript += "p=\":$kubectlpath:\"\n";
zshScript += "d=\":$PATH:\"\n";
zshScript += `d=\${d//$p/:}\n`;
zshScript += `d=\${d/#:/}\n`;
zshScript += `export PATH=\"$helmpath:$kubectlpath:\${d/%:/}\"\n`;
zshScript += "export KUBECONFIG=\"$tempkubeconfig\"\n";
zshScript += `NO_PROXY=\",\${NO_PROXY:-localhost},\"\n`;
zshScript += `NO_PROXY=\"\${NO_PROXY//,localhost,/,}\"\n`;
zshScript += `NO_PROXY=\"\${NO_PROXY//,127.0.0.1,/,}\"\n`;
zshScript += `NO_PROXY=\"localhost,127.0.0.1\${NO_PROXY%,}\"\n`;
zshScript += "export NO_PROXY\n";
zshScript += "unset tempkubeconfig\n";
zshScript += "unset OLD_ZDOTDIR\n";
await fsPromises.writeFile(zshScriptPath, zshScript.toString(), { mode: 0o644 });
}
protected getDownloadMirror(): string {
// MacOS packages are only available from default
const mirror = packageMirrors.get(this.dependencies.userStore.downloadMirror)
?? packageMirrors.get(defaultPackageMirror);
return mirror.url;
}
}