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

Disable kubectl download mirror options when not available (#3956)

This commit is contained in:
Sebastian Malton 2021-10-19 09:22:06 -04:00 committed by GitHub
parent 125a073007
commit cbffb85650
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 17 deletions

View File

@ -136,12 +136,32 @@ const allowErrorReporting: PreferenceDescription<boolean> = {
}, },
}; };
export interface DownloadMirror {
url: string;
label: string;
platforms: Set<NodeJS.Platform>;
}
export const defaultPackageMirror = "default";
export const packageMirrors = new Map<string, DownloadMirror>([
[defaultPackageMirror, {
url: "https://storage.googleapis.com/kubernetes-release/release",
label: "Default (Google)",
platforms: new Set(["darwin", "win32", "linux"]),
}],
["china", {
url: "https://mirror.azure.cn/kubernetes/kubectl",
label: "China (Azure)",
platforms: new Set(["win32", "linux"]),
}],
]);
const downloadMirror: PreferenceDescription<string> = { const downloadMirror: PreferenceDescription<string> = {
fromStore(val) { fromStore(val) {
return val ?? "default"; return packageMirrors.has(val) ? val : defaultPackageMirror;
}, },
toStore(val) { toStore(val) {
if (!val || val === "default") { if (!val || val === defaultPackageMirror) {
return undefined; return undefined;
} }

View File

@ -32,6 +32,7 @@ import { getBundledKubectlVersion } from "../common/utils/app-version";
import { isDevelopment, isWindows, isTestEnv } from "../common/vars"; import { isDevelopment, isWindows, isTestEnv } from "../common/vars";
import { SemVer } from "semver"; import { SemVer } from "semver";
import { getPath } from "../common/utils/getPath"; import { getPath } from "../common/utils/getPath";
import { defaultPackageMirror, packageMirrors } from "../common/user-store/preferences-helpers";
const bundledVersion = getBundledKubectlVersion(); const bundledVersion = getBundledKubectlVersion();
const kubectlMap: Map<string, string> = new Map([ const kubectlMap: Map<string, string> = new Map([
@ -51,10 +52,6 @@ const kubectlMap: Map<string, string> = new Map([
["1.20", "1.20.8"], ["1.20", "1.20.8"],
["1.21", bundledVersion] ["1.21", bundledVersion]
]); ]);
const packageMirrors: Map<string, string> = new Map([
["default", "https://storage.googleapis.com/kubernetes-release/release"],
["china", "https://mirror.azure.cn/kubernetes/kubectl"]
]);
let bundledPath: string; let bundledPath: string;
const initScriptVersionString = "# lens-initscript v3\n"; const initScriptVersionString = "# lens-initscript v3\n";
@ -389,12 +386,9 @@ export class Kubectl {
} }
protected getDownloadMirror() { protected getDownloadMirror() {
const mirror = packageMirrors.get(UserStore.getInstance().downloadMirror); // MacOS packages are only available from default
if (mirror) { return packageMirrors.get(UserStore.getInstance().downloadMirror)
return mirror; ?? packageMirrors.get(defaultPackageMirror);
}
return packageMirrors.get("default"); // MacOS packages are only available from default
} }
} }

View File

@ -27,17 +27,17 @@ import { observer } from "mobx-react";
import { bundledKubectlPath } from "../../../main/kubectl"; import { bundledKubectlPath } from "../../../main/kubectl";
import { SelectOption, Select } from "../select"; import { SelectOption, Select } from "../select";
import { FormSwitch, Switcher } from "../switch"; import { FormSwitch, Switcher } from "../switch";
import { packageMirrors } from "../../../common/user-store/preferences-helpers";
export const KubectlBinaries = observer(() => { export const KubectlBinaries = observer(() => {
const userStore = UserStore.getInstance(); const userStore = UserStore.getInstance();
const [downloadPath, setDownloadPath] = useState(userStore.downloadBinariesPath || ""); const [downloadPath, setDownloadPath] = useState(userStore.downloadBinariesPath || "");
const [binariesPath, setBinariesPath] = useState(userStore.kubectlBinariesPath || ""); const [binariesPath, setBinariesPath] = useState(userStore.kubectlBinariesPath || "");
const pathValidator = downloadPath ? InputValidators.isPath : undefined; const pathValidator = downloadPath ? InputValidators.isPath : undefined;
const downloadMirrorOptions: SelectOption<string>[] = Array.from(
const downloadMirrorOptions: SelectOption<string>[] = [ packageMirrors.entries(),
{ value: "default", label: "Default (Google)" }, ([value, { label, platforms }]) => ({ value, label, platforms })
{ value: "china", label: "China (Azure)" }, );
];
const save = () => { const save = () => {
userStore.downloadBinariesPath = downloadPath; userStore.downloadBinariesPath = downloadPath;
@ -68,6 +68,7 @@ export const KubectlBinaries = observer(() => {
value={userStore.downloadMirror} value={userStore.downloadMirror}
onChange={({ value }: SelectOption) => userStore.downloadMirror = value} onChange={({ value }: SelectOption) => userStore.downloadMirror = value}
disabled={!userStore.downloadKubectlBinaries} disabled={!userStore.downloadKubectlBinaries}
isOptionDisabled={({ platforms }) => !platforms.has(process.platform)}
themeName="lens" themeName="lens"
/> />
</section> </section>