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

Switching current-context from dialog

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2021-08-20 16:56:05 +03:00 committed by Sebastian Malton
parent 8e85beee86
commit 89e7cb79ac
2 changed files with 78 additions and 12 deletions

View File

@ -21,7 +21,7 @@
.dialog { .dialog {
> div { > div {
max-width: 50vw; max-width: 600px;
min-width: calc(45 * var(--unit)); min-width: calc(45 * var(--unit));
background-color: white; background-color: white;
border-radius: 3px; border-radius: 3px;

View File

@ -20,12 +20,12 @@
*/ */
import styles from "./delete-cluster-dialog.module.css"; import styles from "./delete-cluster-dialog.module.css";
import { observable } from "mobx"; import { makeObservable, observable } from "mobx";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import React from "react"; import React from "react";
import { Button } from "../button"; import { Button } from "../button";
import type { KubeConfig } from "@kubernetes/client-node"; import type { Context, KubeConfig } from "@kubernetes/client-node";
import type { Cluster } from "../../../main/cluster"; import type { Cluster } from "../../../main/cluster";
import { saveKubeconfig } from "./save-config"; import { saveKubeconfig } from "./save-config";
import { requestMain } from "../../../common/ipc"; import { requestMain } from "../../../common/ipc";
@ -35,6 +35,7 @@ import { HotbarStore } from "../../../common/hotbar-store";
import { boundMethod } from "autobind-decorator"; import { boundMethod } from "autobind-decorator";
import { Dialog } from "../dialog"; import { Dialog } from "../dialog";
import { Icon } from "../icon"; import { Icon } from "../icon";
import { Select } from "../select";
type DialogState = { type DialogState = {
isOpen: boolean, isOpen: boolean,
@ -46,8 +47,21 @@ const dialogState: DialogState = observable({
isOpen: false isOpen: false
}); });
type Props = {};
@observer @observer
export class DeleteClusterDialog extends React.Component { export class DeleteClusterDialog extends React.Component {
showContextSwitch = false;
newCurrentContext = "";
constructor(props: Props) {
super(props);
makeObservable(this, {
showContextSwitch: observable,
newCurrentContext: observable
});
}
static open({ config, cluster }: Partial<DialogState>) { static open({ config, cluster }: Partial<DialogState>) {
dialogState.isOpen = true; dialogState.isOpen = true;
dialogState.config = config; dialogState.config = config;
@ -58,40 +72,90 @@ export class DeleteClusterDialog extends React.Component {
dialogState.isOpen = false; dialogState.isOpen = false;
} }
@boundMethod
onOpen() {
this.showContextSwitch = false;
this.newCurrentContext = "";
}
onClose() { onClose() {
DeleteClusterDialog.close(); DeleteClusterDialog.close();
} }
removeContext() {
dialogState.config.contexts = dialogState.config.contexts.filter(item =>
item.name !== dialogState.cluster.contextName
);
}
changeCurrentContext() {
if (this.newCurrentContext) {
dialogState.config.currentContext = this.newCurrentContext;
}
}
@boundMethod @boundMethod
async onDelete() { async onDelete() {
await requestMain(clusterSetDeletingHandler, dialogState.cluster.id); const { cluster, config } = dialogState;
dialogState.config.contexts = dialogState.config.contexts.filter(item => item.name !== dialogState.cluster.contextName);
await requestMain(clusterSetDeletingHandler, cluster.id);
this.removeContext();
this.changeCurrentContext();
try { try {
await saveKubeconfig(dialogState.config, dialogState.cluster.kubeConfigPath); await saveKubeconfig(config, cluster.kubeConfigPath);
HotbarStore.getInstance().removeAllHotbarItems(dialogState.cluster.id); HotbarStore.getInstance().removeAllHotbarItems(cluster.id);
await requestMain(clusterDeleteHandler, dialogState.cluster.id); await requestMain(clusterDeleteHandler, cluster.id);
} catch(error) { } catch(error) {
Notifications.error(`Cannot remove cluster, failed to process config file. ${error}`); Notifications.error(`Cannot remove cluster, failed to process config file. ${error}`);
await requestMain(clusterClearDeletingHandler, dialogState.cluster.id); await requestMain(clusterClearDeletingHandler, cluster.id);
} }
this.onClose(); this.onClose();
} }
renderCurrentContextSwitch(contexts: Context[]) {
if (!this.showContextSwitch) return null;
const options = [
...contexts.map(context => ({
label: context.name,
value: context.name,
})),
];
return (
<div className="mt-4">
<p className="mb-4 font-semibold">Choose new current-context</p>
<Select
options={options}
onChange={({ value }) => this.newCurrentContext = value}
themeName="light"
className="ml-[1px] mr-[1px]"
/>
</div>
)
}
render() { render() {
const { cluster, config, isOpen } = dialogState; const { cluster, config, isOpen } = dialogState;
if (!cluster || !config) return null; if (!cluster || !config) return null;
const isCurrentContext = config.currentContext == cluster.contextName; const isCurrentContext = config.currentContext == cluster.contextName;
// const contexts = config.contexts.filter(context => context.name !== cluster.contextName); const contexts = config.contexts.filter(context => context.name !== cluster.contextName);
const warning = isCurrentContext ? ( const warning = isCurrentContext ? (
<> <>
<p>The <b>current-context</b> field from the kubeconfig file indicates a minikube context <p>The <b>current-context</b> field from the kubeconfig file indicates a minikube context
that will cease to exist after the change. This will affect the operation of kubectl. that will cease to exist after the change. This will affect the operation of kubectl.{" "}
{" "}<b>Replace current context</b></p> {contexts.length > 0 && (
<b
className="cursor-pointer underline"
onClick={() => this.showContextSwitch = !this.showContextSwitch}
>Replace current context</b>
)}
</p>
</> </>
) : ( ) : (
<p>The contents of your kubeconfig file will be changed!</p> <p>The contents of your kubeconfig file will be changed!</p>
@ -102,6 +166,7 @@ export class DeleteClusterDialog extends React.Component {
className={styles.dialog} className={styles.dialog}
isOpen={isOpen} isOpen={isOpen}
close={this.onClose} close={this.onClose}
open={this.onOpen}
> >
<div className={styles.dialogContent}> <div className={styles.dialogContent}>
<div> <div>
@ -111,6 +176,7 @@ export class DeleteClusterDialog extends React.Component {
<Icon material="warning_amber" className={styles.warningIcon}/> <Icon material="warning_amber" className={styles.warningIcon}/>
{warning} {warning}
</div> </div>
{this.renderCurrentContextSwitch(contexts)}
</div> </div>
<div className={styles.dialogButtons}> <div className={styles.dialogButtons}>
<Button <Button