mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Cleaning up
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
parent
0d9fb90d4b
commit
12e97773a7
@ -26,11 +26,6 @@ export function noop<T extends any[]>(...args: T): void {
|
|||||||
return void args;
|
return void args;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum ControlFlow {
|
|
||||||
Stop,
|
|
||||||
Continue
|
|
||||||
}
|
|
||||||
|
|
||||||
export * from "./app-version";
|
export * from "./app-version";
|
||||||
export * from "./autobind";
|
export * from "./autobind";
|
||||||
export * from "./base64";
|
export * from "./base64";
|
||||||
|
|||||||
@ -172,6 +172,7 @@ code {
|
|||||||
padding: 0.2em;
|
padding: 0.2em;
|
||||||
vertical-align: middle;
|
vertical-align: middle;
|
||||||
border-radius: $radius;
|
border-radius: $radius;
|
||||||
|
font-family: $font-monospace;
|
||||||
font-size: calc(var(--font-size) * .9);
|
font-size: calc(var(--font-size) * .9);
|
||||||
color: #b4b5b4;
|
color: #b4b5b4;
|
||||||
|
|
||||||
|
|||||||
@ -22,20 +22,17 @@
|
|||||||
import "./confirm-dialog.scss";
|
import "./confirm-dialog.scss";
|
||||||
|
|
||||||
import React, { ReactNode } from "react";
|
import React, { ReactNode } from "react";
|
||||||
import { action, observable } from "mobx";
|
import { observable, makeObservable } from "mobx";
|
||||||
import { observer } from "mobx-react";
|
import { observer } from "mobx-react";
|
||||||
import { cssNames, noop, prevDefault } from "../../utils";
|
import { cssNames, noop, prevDefault } from "../../utils";
|
||||||
import { Button, ButtonProps } from "../button";
|
import { Button, ButtonProps } from "../button";
|
||||||
import { Dialog, DialogProps } from "../dialog";
|
import { Dialog, DialogProps } from "../dialog";
|
||||||
import { Icon } from "../icon";
|
import { Icon } from "../icon";
|
||||||
|
import { Notifications } from "../notifications";
|
||||||
|
|
||||||
export interface ConfirmDialogProps extends Partial<DialogProps> {
|
export interface ConfirmDialogProps extends Partial<DialogProps> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ConfirmDialogRootProps {
|
|
||||||
className?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ConfirmDialogParams extends ConfirmDialogBooleanParams {
|
export interface ConfirmDialogParams extends ConfirmDialogBooleanParams {
|
||||||
ok?: () => any | Promise<any>;
|
ok?: () => any | Promise<any>;
|
||||||
cancel?: () => any | Promise<any>;
|
cancel?: () => any | Promise<any>;
|
||||||
@ -50,24 +47,23 @@ export interface ConfirmDialogBooleanParams {
|
|||||||
cancelButtonProps?: Partial<ButtonProps>;
|
cancelButtonProps?: Partial<ButtonProps>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const dialogState = observable.set<ConfirmDialogParams>([], { deep: false });
|
const dialogState = observable.object({
|
||||||
|
isOpen: false,
|
||||||
|
params: null as ConfirmDialogParams,
|
||||||
|
});
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class ConfirmDialog extends React.Component<ConfirmDialogRootProps> {
|
export class ConfirmDialog extends React.Component<ConfirmDialogProps> {
|
||||||
static defaultParams: Partial<ConfirmDialogParams> = {
|
@observable isSaving = false;
|
||||||
ok: noop,
|
|
||||||
cancel: noop,
|
constructor(props: ConfirmDialogProps) {
|
||||||
labelOk: "Ok",
|
super(props);
|
||||||
labelCancel: "Cancel",
|
makeObservable(this);
|
||||||
icon: <Icon big material="warning" />,
|
}
|
||||||
};
|
|
||||||
|
|
||||||
@action
|
|
||||||
static open(params: ConfirmDialogParams) {
|
static open(params: ConfirmDialogParams) {
|
||||||
dialogState.add({
|
dialogState.isOpen = true;
|
||||||
...ConfirmDialog.defaultParams,
|
dialogState.params = params;
|
||||||
...params,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static confirm(params: ConfirmDialogBooleanParams): Promise<boolean> {
|
static confirm(params: ConfirmDialogBooleanParams): Promise<boolean> {
|
||||||
@ -80,62 +76,92 @@ export class ConfirmDialog extends React.Component<ConfirmDialogRootProps> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = async (params: ConfirmDialogParams) => {
|
static defaultParams: Partial<ConfirmDialogParams> = {
|
||||||
|
ok: noop,
|
||||||
|
cancel: noop,
|
||||||
|
labelOk: "Ok",
|
||||||
|
labelCancel: "Cancel",
|
||||||
|
icon: <Icon big material="warning"/>,
|
||||||
|
};
|
||||||
|
|
||||||
|
get params(): ConfirmDialogParams {
|
||||||
|
return Object.assign({}, ConfirmDialog.defaultParams, dialogState.params);
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = async () => {
|
||||||
try {
|
try {
|
||||||
await params.ok();
|
this.isSaving = true;
|
||||||
} catch {} finally {
|
await (async () => this.params.ok())();
|
||||||
dialogState.delete(params);
|
} catch (error) {
|
||||||
|
Notifications.error(
|
||||||
|
<>
|
||||||
|
<p>Confirmation action failed:</p>
|
||||||
|
<p>{error?.message ?? error?.toString?.() ?? "Unknown error"}</p>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
this.isSaving = false;
|
||||||
|
dialogState.isOpen = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
close = async (params: ConfirmDialogParams) => {
|
onClose = () => {
|
||||||
|
this.isSaving = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
close = async () => {
|
||||||
try {
|
try {
|
||||||
await params.cancel();
|
await Promise.resolve(this.params.cancel());
|
||||||
} catch { } finally {
|
} catch (error) {
|
||||||
dialogState.delete(params);
|
Notifications.error(
|
||||||
|
<>
|
||||||
|
<p>Cancelling action failed:</p>
|
||||||
|
<p>{error?.message ?? error?.toString?.() ?? "Unknown error"}</p>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
this.isSaving = false;
|
||||||
|
dialogState.isOpen = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { className } = this.props;
|
const { className, ...dialogProps } = this.props;
|
||||||
|
const {
|
||||||
|
icon, labelOk, labelCancel, message,
|
||||||
|
okButtonProps = {},
|
||||||
|
cancelButtonProps = {},
|
||||||
|
} = this.params;
|
||||||
|
|
||||||
return [...dialogState].reduce<JSX.Element>((prev, params) => {
|
return (
|
||||||
const {
|
<Dialog
|
||||||
icon, labelOk, labelCancel, message,
|
{...dialogProps}
|
||||||
okButtonProps = {},
|
className={cssNames("ConfirmDialog", className)}
|
||||||
cancelButtonProps = {},
|
isOpen={dialogState.isOpen}
|
||||||
} = params;
|
onClose={this.onClose}
|
||||||
|
close={this.close}
|
||||||
return (
|
>
|
||||||
<>
|
<div className="confirm-content">
|
||||||
{prev}
|
{icon} {message}
|
||||||
<Dialog
|
</div>
|
||||||
className={cssNames("ConfirmDialog", className)}
|
<div className="confirm-buttons">
|
||||||
isOpen={true}
|
<Button
|
||||||
close={() => this.close(params)}
|
plain
|
||||||
>
|
className="cancel"
|
||||||
<div className="confirm-content">
|
label={labelCancel}
|
||||||
{icon} {message}
|
onClick={prevDefault(this.close)}
|
||||||
</div>
|
{...cancelButtonProps}
|
||||||
<div className="confirm-buttons">
|
/>
|
||||||
<Button
|
<Button
|
||||||
plain
|
autoFocus primary
|
||||||
className="cancel"
|
className="ok"
|
||||||
label={labelCancel}
|
label={labelOk}
|
||||||
onClick={prevDefault(() => this.close(params))}
|
onClick={prevDefault(this.ok)}
|
||||||
{...cancelButtonProps}
|
waiting={this.isSaving}
|
||||||
/>
|
{...okButtonProps}
|
||||||
<Button
|
/>
|
||||||
autoFocus primary
|
</div>
|
||||||
className="ok"
|
</Dialog>
|
||||||
label={labelOk}
|
);
|
||||||
onClick={prevDefault(() => this.ok(params))}
|
|
||||||
{...okButtonProps}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</Dialog>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}, null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -134,7 +134,7 @@ export class DeleteClusterDialog extends React.Component {
|
|||||||
className="ml-[1px] mr-[1px]"
|
className="ml-[1px] mr-[1px]"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
@ -1,77 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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 type { KubeConfig } from "@kubernetes/client-node";
|
|
||||||
import React from "react";
|
|
||||||
import type { Cluster } from "../../../main/cluster";
|
|
||||||
import { ControlFlow, iter } from "../../utils";
|
|
||||||
import { ConfirmDialog } from "../confirm-dialog";
|
|
||||||
import { Select } from "../select";
|
|
||||||
|
|
||||||
export interface DeleteClusterDialogArgs {
|
|
||||||
config: KubeConfig;
|
|
||||||
cluster: Cluster;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function deleteClusterConfirmDialog({ config, cluster }: DeleteClusterDialogArgs): Promise<[ControlFlow.Stop] | [ControlFlow.Continue, string | false]> {
|
|
||||||
const contextNames = new Set(config.getContexts().map(({ name }) => name));
|
|
||||||
|
|
||||||
contextNames.delete(cluster.contextName);
|
|
||||||
|
|
||||||
if (config.currentContext !== cluster.contextName || contextNames.size === 0) {
|
|
||||||
return [ControlFlow.Continue, false];
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = [
|
|
||||||
{
|
|
||||||
label: "--unset current-context--",
|
|
||||||
value: false,
|
|
||||||
},
|
|
||||||
...iter.map(contextNames, name => ({
|
|
||||||
label: name,
|
|
||||||
value: name,
|
|
||||||
})),
|
|
||||||
];
|
|
||||||
let selectedOption: string | false = false;
|
|
||||||
const didConfirm = await ConfirmDialog.confirm({
|
|
||||||
labelOk: "Select context",
|
|
||||||
message: (
|
|
||||||
<>
|
|
||||||
<p>
|
|
||||||
The context you are deleting is the <code>current-context</code> in the <code>{cluster.kubeConfigPath}</code> file.
|
|
||||||
Please select one of the other contexts to replace it with.
|
|
||||||
</p>
|
|
||||||
<br />
|
|
||||||
<Select
|
|
||||||
options={options}
|
|
||||||
onChange={({ value }) => selectedOption = value}
|
|
||||||
themeName="light"
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
if (didConfirm) {
|
|
||||||
return [ControlFlow.Continue, selectedOption];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [ControlFlow.Stop];
|
|
||||||
}
|
|
||||||
@ -21,8 +21,6 @@
|
|||||||
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
|
||||||
import tempy from "tempy";
|
|
||||||
import "../../common/catalog-entities/kubernetes-cluster";
|
import "../../common/catalog-entities/kubernetes-cluster";
|
||||||
import { WebLinkCategory } from "../../common/catalog-entities";
|
import { WebLinkCategory } from "../../common/catalog-entities";
|
||||||
import { ClusterStore } from "../../common/cluster-store";
|
import { ClusterStore } from "../../common/cluster-store";
|
||||||
@ -30,15 +28,7 @@ import { appEventBus } from "../../common/event-bus";
|
|||||||
import { catalogCategoryRegistry } from "../api/catalog-category-registry";
|
import { catalogCategoryRegistry } from "../api/catalog-category-registry";
|
||||||
import { WeblinkAddCommand } from "../components/catalog-entities/weblink-add-command";
|
import { WeblinkAddCommand } from "../components/catalog-entities/weblink-add-command";
|
||||||
import { CommandOverlay } from "../components/command-palette";
|
import { CommandOverlay } from "../components/command-palette";
|
||||||
import { Notifications } from "../components/notifications";
|
|
||||||
import { loadConfigFromString } from "../../common/kube-helpers";
|
import { loadConfigFromString } from "../../common/kube-helpers";
|
||||||
import { requestMain } from "../../common/ipc";
|
|
||||||
import { clusterClearDeletingHandler, clusterDeleteHandler, clusterSetDeletingHandler } from "../../common/cluster-ipc";
|
|
||||||
import { ControlFlow } from "../utils";
|
|
||||||
import { HotbarStore } from "../../common/hotbar-store";
|
|
||||||
import type { ClusterId } from "../../common/cluster-types";
|
|
||||||
import type { Cluster } from "../../main/cluster";
|
|
||||||
import { deleteClusterConfirmDialog } from "../components/dialog/delete-cluster-dialog";
|
|
||||||
import { DeleteClusterDialog } from "../components/delete-cluster-dialog";
|
import { DeleteClusterDialog } from "../components/delete-cluster-dialog";
|
||||||
|
|
||||||
function initWebLinks() {
|
function initWebLinks() {
|
||||||
@ -80,108 +70,3 @@ export function initCatalog() {
|
|||||||
initWebLinks();
|
initWebLinks();
|
||||||
initKubernetesClusters();
|
initKubernetesClusters();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setDeleting(cluster: Cluster): Promise<ControlFlow> {
|
|
||||||
await requestMain(clusterSetDeletingHandler, cluster.id);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await fs.promises.access(cluster.kubeConfigPath, fs.constants.W_OK | fs.constants.R_OK);
|
|
||||||
} catch {
|
|
||||||
await requestMain(clusterClearDeletingHandler, cluster.id);
|
|
||||||
|
|
||||||
Notifications.error(
|
|
||||||
<p>Cannot remove cluster, missing write permissions for <code>{cluster.kubeConfigPath}</code></p>
|
|
||||||
);
|
|
||||||
|
|
||||||
return ControlFlow.Stop;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ControlFlow.Continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function aquireConfigLock(cluster: Cluster, lockFilePath: string): Promise<ControlFlow> {
|
|
||||||
try {
|
|
||||||
const fd = await fs.promises.open(lockFilePath, "wx");
|
|
||||||
|
|
||||||
await fd.close(); // close immeditaly as we will want to delete the file later
|
|
||||||
} catch (error) {
|
|
||||||
await requestMain(clusterClearDeletingHandler, cluster.id);
|
|
||||||
console.warn("[KUBERNETES-CLUSTER]: failed to lock config file", error);
|
|
||||||
|
|
||||||
switch (error.code) {
|
|
||||||
case "EEXIST":
|
|
||||||
case "EISDIR":
|
|
||||||
Notifications.error("Cannot remove cluster, failed to aquire lock file. Already held.");
|
|
||||||
break;
|
|
||||||
case "EPERM":
|
|
||||||
case "EACCES":
|
|
||||||
Notifications.error("Cannot remove cluster, failed to aquire lock file. Permission denied.");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Notifications.error(`Cannot remove cluster, failed to aquire lock file. ${error}`);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ControlFlow.Stop;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ControlFlow.Continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function deleteLocalCluster(clusterId: ClusterId): Promise<void> {
|
|
||||||
appEventBus.emit({ name: "cluster", action: "remove" });
|
|
||||||
const cluster = ClusterStore.getInstance().getById(clusterId);
|
|
||||||
|
|
||||||
if (!cluster) {
|
|
||||||
return console.warn("[KUBERNETES-CLUSTER]: cannot delete cluster, does not exist in store", { clusterId });
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (await setDeleting(cluster)) {
|
|
||||||
case ControlFlow.Stop:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const lockFilePath = `${path.resolve(cluster.kubeConfigPath)}.lock`;
|
|
||||||
|
|
||||||
switch (await aquireConfigLock(cluster, lockFilePath)) {
|
|
||||||
case ControlFlow.Stop:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { config, error } = loadConfigFromString(await fs.promises.readFile(cluster.kubeConfigPath, "utf-8"));
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
const [cf, selectedOption] = await deleteClusterConfirmDialog({ config, cluster });
|
|
||||||
|
|
||||||
switch (cf) {
|
|
||||||
case ControlFlow.Stop:
|
|
||||||
return void await requestMain(clusterClearDeletingHandler, cluster.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (selectedOption === false) {
|
|
||||||
config.setCurrentContext(undefined);
|
|
||||||
} else {
|
|
||||||
config.setCurrentContext(selectedOption);
|
|
||||||
}
|
|
||||||
|
|
||||||
config.contexts = config.contexts.filter(context => context.name !== cluster.contextName);
|
|
||||||
|
|
||||||
const tmpFilePath = tempy.file();
|
|
||||||
|
|
||||||
await fs.promises.writeFile(tmpFilePath, config.exportConfig());
|
|
||||||
await fs.promises.rename(tmpFilePath, cluster.kubeConfigPath);
|
|
||||||
await requestMain(clusterDeleteHandler, clusterId);
|
|
||||||
HotbarStore.getInstance().removeAllHotbarItems(clusterId);
|
|
||||||
} catch (error) {
|
|
||||||
await requestMain(clusterClearDeletingHandler, clusterId);
|
|
||||||
console.warn("[KUBERNETES-CLUSTER]: failed to read or parse kube config file", error);
|
|
||||||
|
|
||||||
return void Notifications.error(`Cannot remove cluster, failed to process config file. ${error}`);
|
|
||||||
} finally {
|
|
||||||
await fs.promises.unlink(lockFilePath); // always unlink the file
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user