From 123b628ad7a659b751ed1131dc447688b857878c Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Tue, 4 May 2021 08:57:01 +0300 Subject: [PATCH] tweak add cluster page Signed-off-by: Jari Kolehmainen --- .../components/+add-cluster/add-cluster.scss | 22 +- .../components/+add-cluster/add-cluster.tsx | 320 ++++-------------- 2 files changed, 68 insertions(+), 274 deletions(-) diff --git a/src/renderer/components/+add-cluster/add-cluster.scss b/src/renderer/components/+add-cluster/add-cluster.scss index 9583ec17b9..1185fcd761 100644 --- a/src/renderer/components/+add-cluster/add-cluster.scss +++ b/src/renderer/components/+add-cluster/add-cluster.scss @@ -3,8 +3,8 @@ $spacing: $padding * 2; .AceEditor { - min-height: 200px; - max-height: 400px; + min-height: 600px; + max-height: 600px; border: 1px solid var(--colorVague); border-radius: $radius; @@ -17,24 +17,6 @@ } } - .Select { - .kube-context { - --flex-gap: #{$padding}; - } - - // todo: extract to component, merge with namespace-select.scss - &__placeholder { - width: 100%; - white-space: nowrap; - text-overflow: ellipsis; - overflow: hidden; - } - - &__control { - box-shadow: 0 0 0 1px $borderFaintColor; - } - } - code { color: $pink-400; } diff --git a/src/renderer/components/+add-cluster/add-cluster.tsx b/src/renderer/components/+add-cluster/add-cluster.tsx index 4c65104c7e..b6d24f46e5 100644 --- a/src/renderer/components/+add-cluster/add-cluster.tsx +++ b/src/renderer/components/+add-cluster/add-cluster.tsx @@ -1,51 +1,35 @@ import "./add-cluster.scss"; -import os from "os"; import React from "react"; import { observer } from "mobx-react"; import { action, observable, runInAction } from "mobx"; -import { remote } from "electron"; import { KubeConfig } from "@kubernetes/client-node"; -import { Select, SelectOption } from "../select"; -import { DropFileInput, Input } from "../input"; import { AceEditor } from "../ace-editor"; import { Button } from "../button"; -import { Icon } from "../icon"; -import { kubeConfigDefaultPath, loadConfig, splitConfig, validateConfig, validateKubeConfig } from "../../../common/kube-helpers"; +import { loadConfig, splitConfig, validateKubeConfig } from "../../../common/kube-helpers"; import { ClusterStore } from "../../../common/cluster-store"; import { v4 as uuid } from "uuid"; import { navigate } from "../../navigation"; import { UserStore } from "../../../common/user-store"; -import { cssNames } from "../../utils"; import { Notifications } from "../notifications"; -import { Tab, Tabs } from "../tabs"; import { ExecValidationNotFoundError } from "../../../common/custom-errors"; import { appEventBus } from "../../../common/event-bus"; import { PageLayout } from "../layout/page-layout"; import { docsUrl } from "../../../common/vars"; import { catalogURL } from "../+catalog"; - -enum KubeConfigSourceTab { - FILE = "file", - TEXT = "text" -} - +import { preferencesURL } from "../+preferences"; +import { Input } from "../input"; @observer export class AddCluster extends React.Component { @observable.ref kubeConfigLocal: KubeConfig; @observable.ref error: React.ReactNode; - - @observable kubeContexts = observable.map(); // available contexts from kubeconfig-file or user-input - @observable selectedContexts = observable.array(); - @observable sourceTab = KubeConfigSourceTab.FILE; - @observable kubeConfigPath = ""; @observable customConfig = ""; @observable proxyServer = ""; @observable isWaiting = false; @observable showSettings = false; + kubeContexts = observable.map(); + componentDidMount() { - ClusterStore.getInstance().setActive(null); - this.setKubeConfig(UserStore.getInstance().kubeConfigPath); appEventBus.emit({ name: "cluster-add", action: "start" }); } @@ -53,52 +37,19 @@ export class AddCluster extends React.Component { UserStore.getInstance().markNewContextsAsSeen(); } - @action - setKubeConfig(filePath: string, { throwError = false } = {}) { - try { - this.kubeConfigLocal = loadConfig(filePath); - validateConfig(this.kubeConfigLocal); - this.refreshContexts(); - this.kubeConfigPath = filePath; - UserStore.getInstance().kubeConfigPath = filePath; // save to store - } catch (err) { - if (!UserStore.getInstance().isDefaultKubeConfigPath) { - Notifications.error( -
Can't setup {filePath} as kubeconfig: {String(err)}
- ); - } - - if (throwError) { - throw err; - } - } - } - @action refreshContexts() { - this.selectedContexts.clear(); this.kubeContexts.clear(); - switch (this.sourceTab) { - case KubeConfigSourceTab.FILE: - const contexts = this.getContexts(this.kubeConfigLocal); + try { + this.error = ""; + const contexts = this.getContexts(loadConfig(this.customConfig || "{}")); - this.kubeContexts.replace(contexts); - break; - case KubeConfigSourceTab.TEXT: - try { - this.error = ""; - const contexts = this.getContexts(loadConfig(this.customConfig || "{}")); + console.log(contexts); - this.kubeContexts.replace(contexts); - } catch (err) { - this.error = String(err); - } - break; - } - - if (this.kubeContexts.size === 1) { - this.selectedContexts.push(this.kubeContexts.keys().next().value); + this.kubeContexts.replace(contexts); + } catch (err) { + this.error = String(err); } } @@ -112,36 +63,14 @@ export class AddCluster extends React.Component { return contexts; } - selectKubeConfigDialog = async () => { - const { dialog, BrowserWindow } = remote; - const { canceled, filePaths } = await dialog.showOpenDialog(BrowserWindow.getFocusedWindow(), { - defaultPath: this.kubeConfigPath, - properties: ["openFile", "showHiddenFiles"], - message: `Select custom kubeconfig file`, - buttonLabel: `Use configuration`, - }); - - if (!canceled && filePaths.length) { - this.setKubeConfig(filePaths[0]); - } - }; - - onDropKubeConfig = (files: File[]) => { - this.sourceTab = KubeConfigSourceTab.FILE; - this.setKubeConfig(files[0].path); - }; - @action addClusters = (): void => { try { - if (!this.selectedContexts.length) { - return void (this.error = "Please select at least one cluster context"); - } this.error = ""; this.isWaiting = true; appEventBus.emit({ name: "cluster-add", action: "click" }); - const newClusters = this.selectedContexts.filter(context => { + const newClusters = Array.from(this.kubeContexts.keys()).filter(context => { const kubeConfig = this.kubeContexts.get(context); const error = validateKubeConfig(kubeConfig, context); @@ -157,9 +86,7 @@ export class AddCluster extends React.Component { }).map(context => { const clusterId = uuid(); const kubeConfig = this.kubeContexts.get(context); - const kubeConfigPath = this.sourceTab === KubeConfigSourceTab.FILE - ? this.kubeConfigPath // save link to original kubeconfig in file-system - : ClusterStore.embedCustomKubeConfig(clusterId, kubeConfig); // save in app-files folder + const kubeConfigPath = ClusterStore.embedCustomKubeConfig(clusterId, kubeConfig); // save in app-files folder return { id: clusterId, @@ -193,9 +120,8 @@ export class AddCluster extends React.Component { renderInfo() { return (

- Add clusters by clicking the Add Cluster button. - You'll need to obtain a working kubeconfig for the cluster you want to add. - You can either browse it from the file system or paste it as a text from the clipboard. + Paste kubeconfig as a text from the clipboard to the textarea below. + If you want to add clusters from kubeconfigs that exists on filesystem, please add those files (or folders) to kubeconfig sync via navigate(preferencesURL())}>Preferences. Read more about adding clusters here.

); @@ -204,180 +130,66 @@ export class AddCluster extends React.Component { renderKubeConfigSource() { return ( <> - - - + { + this.customConfig = value; + this.refreshContexts(); + }} /> - - {this.sourceTab === KubeConfigSourceTab.FILE && ( -
-
- this.kubeConfigPath = v} - onBlur={this.onKubeConfigInputBlur} - /> - {this.kubeConfigPath !== kubeConfigDefaultPath && ( - this.setKubeConfig(kubeConfigDefaultPath)} - tooltip="Reset" - /> - )} - -
- - Pro-Tip: you can also drag-n-drop kubeconfig file to this area - -
- )} - {this.sourceTab === KubeConfigSourceTab.TEXT && ( -
- { - this.customConfig = value; - this.refreshContexts(); - }} - /> - - Pro-Tip: paste kubeconfig to get available contexts - -
- )} + ); } - renderContextSelector() { - const allContexts = Array.from(this.kubeContexts.keys()); - const placeholder = this.selectedContexts.length > 0 - ? <>Selected contexts: {this.selectedContexts.length} - : "Select contexts"; - - return ( -
- this.proxyServer = value} - theme="round-black" - /> - - {"A HTTP proxy server URL (format: http://
:)."} - -
- )} - {this.error && ( -
{this.error}
- )} - -
-
+ ); } }