Use user configured kubectl path in node and pod menu items (#3506)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-11-02 09:29:45 -04:00
1 parent 6b047332c0
commit d8538eaa75
6 files changed
+86 -18

No files matched your search

+12 -5
View File
@@ -20,7 +20,7 @@
*/
import React from "react";
import { Renderer } from "@k8slens/extensions";
import { Common, Renderer } from "@k8slens/extensions";
type Node = Renderer.K8sApi.Node;
@@ -34,6 +34,9 @@ const {
},
Navigation
} = Renderer;
const {
App,
} = Common;
export interface NodeMenuProps extends Renderer.Component.KubeObjectMenuProps<Node> {
@@ -42,8 +45,12 @@ export interface NodeMenuProps extends Renderer.Component.KubeObjectMenuProps<No
export function NodeMenu(props: NodeMenuProps) {
const { object: node, toolbar } = props;
if (!node) return null;
if (!node) {
return null;
}
const nodeName = node.getName();
const kubectlPath = App.Preferences.getKubectlPath() || "kubectl";
const sendToTerminal = (command: string) => {
terminalStore.sendCommand(command, {
@@ -62,15 +69,15 @@ export function NodeMenu(props: NodeMenuProps) {
};
const cordon = () => {
sendToTerminal(`kubectl cordon ${nodeName}`);
sendToTerminal(`${kubectlPath} cordon ${nodeName}`);
};
const unCordon = () => {
sendToTerminal(`kubectl uncordon ${nodeName}`);
sendToTerminal(`${kubectlPath} uncordon ${nodeName}`);
};
const drain = () => {
const command = `kubectl drain ${nodeName} --delete-local-data --ignore-daemonsets --force`;
const command = `${kubectlPath} drain ${nodeName} --delete-local-data --ignore-daemonsets --force`;
ConfirmDialog.open({
ok: () => sendToTerminal(command),
+18 -5
View File
@@ -39,6 +39,7 @@ const {
} = Renderer;
const {
Util,
App,
} = Common;
export interface PodAttachMenuProps extends Renderer.Component.KubeObjectMenuProps<Pod> {
@@ -47,22 +48,34 @@ export interface PodAttachMenuProps extends Renderer.Component.KubeObjectMenuPro
export class PodAttachMenu extends React.Component<PodAttachMenuProps> {
async attachToPod(container?: string) {
const { object: pod } = this.props;
const containerParam = container ? `-c ${container}` : "";
let command = `kubectl attach -i -t -n ${pod.getNs()} ${pod.getName()} ${containerParam}`;
const kubectlPath = App.Preferences.getKubectlPath() || "kubectl";
const commandParts = [
kubectlPath,
"attach",
"-i",
"-t",
"-n", pod.getNs(),
pod.getName(),
];
if (window.navigator.platform !== "Win32") {
command = `exec ${command}`;
commandParts.unshift("exec");
}
if (container) {
commandParts.push("-c", container);
}
const shell = createTerminalTab({
title: `Pod: ${pod.getName()} (namespace: ${pod.getNs()}) [Attached]`
});
terminalStore.sendCommand(command, {
terminalStore.sendCommand(commandParts.join(" "), {
enter: true,
tabId: shell.id
});
Navigation.hideDetails();
}
+23 -7
View File
@@ -39,6 +39,7 @@ const {
} = Renderer;
const {
Util,
App,
} = Common;
export interface PodShellMenuProps extends Renderer.Component.KubeObjectMenuProps<Pod> {
@@ -46,29 +47,44 @@ export interface PodShellMenuProps extends Renderer.Component.KubeObjectMenuProp
export class PodShellMenu extends React.Component<PodShellMenuProps> {
async execShell(container?: string) {
Navigation.hideDetails();
const { object: pod } = this.props;
const containerParam = container ? `-c ${container}` : "";
let command = `kubectl exec -i -t -n ${pod.getNs()} ${pod.getName()} ${containerParam} "--"`;
const kubectlPath = App.Preferences.getKubectlPath() || "kubectl";
const commandParts = [
kubectlPath,
"exec",
"-i",
"-t",
"-n", pod.getNs(),
pod.getName(),
];
if (window.navigator.platform !== "Win32") {
command = `exec ${command}`;
commandParts.unshift("exec");
}
if (container) {
commandParts.push("-c", container);
}
commandParts.push("--");
if (pod.getSelectedNodeOs() === "windows") {
command = `${command} powershell`;
commandParts.push("powershell");
} else {
command = `${command} sh -c "clear; (bash || ash || sh)"`;
commandParts.push('sh -c "clear; (bash || ash || sh)"');
}
const shell = createTerminalTab({
title: `Pod: ${pod.getName()} (namespace: ${pod.getNs()})`
});
terminalStore.sendCommand(command, {
terminalStore.sendCommand(commandParts.join(" "), {
enter: true,
tabId: shell.id
});
Navigation.hideDetails();
}
render() {