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

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-10-05 13:02:52 -04:00
parent 851274d314
commit 6728e27790
3 changed files with 37 additions and 17 deletions

View File

@ -135,12 +135,32 @@ const allowErrorReporting: PreferenceDescription<boolean> = {
}, },
}; };
export interface DownloadMirror {
url: string;
label: string;
platforms: Set<NodeJS.Platform>;
}
const defaultPackageMirror = "default";
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;
} }
@ -289,3 +309,8 @@ export const DESCRIPTORS = {
editorConfiguration, editorConfiguration,
terminalCopyOnSelect, terminalCopyOnSelect,
}; };
export const CONSTANTS = {
defaultPackageMirror,
packageMirrors,
};

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 { CONSTANTS } 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 CONSTANTS.packageMirrors.get(UserStore.getInstance().downloadMirror)
return mirror; ?? CONSTANTS.packageMirrors.get(CONSTANTS.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 { CONSTANTS } 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>[] = [ CONSTANTS.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>