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

Correctly catch and notify user about NodeShell pod failures

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-11-03 09:45:23 -04:00
parent 8de3cbe5ee
commit 7e0d617697
4 changed files with 36 additions and 32 deletions

View File

@ -60,12 +60,10 @@ export class LensProxy extends Singleton {
public port: number; public port: number;
constructor(protected router: Router, functions: LensProxyFunctions) { constructor(protected router: Router, { shellApiRequest, kubeApiRequest, getClusterForRequest }: LensProxyFunctions) {
super(); super();
const { shellApiRequest, kubeApiRequest } = functions; this.getClusterForRequest = getClusterForRequest;
this.getClusterForRequest = functions.getClusterForRequest;
this.proxyServer = spdy.createServer({ this.proxyServer = spdy.createServer({
spdy: { spdy: {
@ -79,9 +77,15 @@ export class LensProxy extends Singleton {
this.proxyServer this.proxyServer
.on("upgrade", (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => { .on("upgrade", (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => {
const isInternal = req.url.startsWith(`${apiPrefix}?`); const isInternal = req.url.startsWith(`${apiPrefix}?`);
const cluster = getClusterForRequest(req);
if (!cluster) {
return void logger.error(`[LENS-PROXY]: Could not find cluster for upgrade request`);
}
const reqHandler = isInternal ? shellApiRequest : kubeApiRequest; const reqHandler = isInternal ? shellApiRequest : kubeApiRequest;
(async () => reqHandler({ req, socket, head }))() (async () => reqHandler({ req, socket, head, cluster }))()
.catch(error => logger.error("[LENS-PROXY]: failed to handle proxy upgrade", error)); .catch(error => logger.error("[LENS-PROXY]: failed to handle proxy upgrade", error));
}); });
} }

View File

@ -23,18 +23,11 @@ import { chunk } from "lodash";
import net from "net"; import net from "net";
import url from "url"; import url from "url";
import { apiKubePrefix } from "../../common/vars"; import { apiKubePrefix } from "../../common/vars";
import { ClusterManager } from "../cluster-manager";
import type { ProxyApiRequestArgs } from "./types"; import type { ProxyApiRequestArgs } from "./types";
const skipRawHeaders = new Set(["Host", "Authorization"]); const skipRawHeaders = new Set(["Host", "Authorization"]);
export async function kubeApiRequest({ req, socket, head }: ProxyApiRequestArgs) { export async function kubeApiRequest({ req, socket, head, cluster }: ProxyApiRequestArgs) {
const cluster = ClusterManager.getInstance().getClusterForRequest(req);
if (!cluster) {
return;
}
const proxyUrl = await cluster.contextHandler.resolveAuthProxyUrl() + req.url.replace(apiKubePrefix, ""); const proxyUrl = await cluster.contextHandler.resolveAuthProxyUrl() + req.url.replace(apiKubePrefix, "");
const apiUrl = url.parse(cluster.apiUrl); const apiUrl = url.parse(cluster.apiUrl);
const pUrl = url.parse(proxyUrl); const pUrl = url.parse(proxyUrl);

View File

@ -21,9 +21,11 @@
import type http from "http"; import type http from "http";
import type net from "net"; import type net from "net";
import type { Cluster } from "../cluster";
export interface ProxyApiRequestArgs { export interface ProxyApiRequestArgs {
req: http.IncomingMessage, req: http.IncomingMessage,
socket: net.Socket, socket: net.Socket,
head: Buffer, head: Buffer,
cluster: Cluster,
} }

View File

@ -33,12 +33,10 @@ import logger from "../logger";
export class NodeShellSession extends ShellSession { export class NodeShellSession extends ShellSession {
ShellType = "node-shell"; ShellType = "node-shell";
protected podId = `node-shell-${uuid()}`; protected readonly podName = `node-shell-${uuid()}`;
protected kc: KubeConfig; protected kc: KubeConfig;
protected get cwd(): string | undefined { protected readonly cwd: string | undefined = undefined;
return undefined;
}
constructor(socket: WebSocket, cluster: Cluster, protected nodeName: string) { constructor(socket: WebSocket, cluster: Cluster, protected nodeName: string) {
super(socket, cluster); super(socket, cluster);
@ -59,7 +57,7 @@ export class NodeShellSession extends ShellSession {
} }
const env = await this.getCachedShellEnv(); const env = await this.getCachedShellEnv();
const args = ["exec", "-i", "-t", "-n", "kube-system", this.podId, "--"]; const args = ["exec", "-i", "-t", "-n", "kube-system", this.podName, "--"];
const nodeApi = new NodesApi({ const nodeApi = new NodesApi({
objectConstructor: Node, objectConstructor: Node,
request: KubeJsonApi.forCluster(this.cluster), request: KubeJsonApi.forCluster(this.cluster),
@ -93,7 +91,7 @@ export class NodeShellSession extends ShellSession {
.makeApiClient(k8s.CoreV1Api) .makeApiClient(k8s.CoreV1Api)
.createNamespacedPod("kube-system", { .createNamespacedPod("kube-system", {
metadata: { metadata: {
name: this.podId, name: this.podName,
namespace: "kube-system", namespace: "kube-system",
}, },
spec: { spec: {
@ -121,33 +119,39 @@ export class NodeShellSession extends ShellSession {
} }
protected waitForRunningPod(): Promise<void> { protected waitForRunningPod(): Promise<void> {
return new Promise((resolve, reject) => { logger.debug(`[NODE-SHELL]: waiting for ${this.podName} to be running`);
const watch = new k8s.Watch(this.kc);
watch return new Promise((resolve, reject) => {
new k8s.Watch(this.kc)
.watch(`/api/v1/namespaces/kube-system/pods`, .watch(`/api/v1/namespaces/kube-system/pods`,
{}, {},
// callback is called for each received object. // callback is called for each received object.
(type, obj) => { (type, { metadata: { name }, status }) => {
if (obj.metadata.name == this.podId && obj.status.phase === "Running") { if (name === this.podName) {
resolve(); switch (status.phase) {
case "Running":
return resolve();
case "Failed":
return reject(`Failed to be created: ${status.message || "unknown error"}`);
}
} }
}, },
// done callback is called if the watch terminates normally // done callback is called if the watch terminates normally
(err) => { (err) => {
console.log(err); logger.error(`[NODE-SHELL]: ${this.podName} was not created in time`);
reject(err); reject(err);
}, },
) )
.then(req => { .then(req => {
setTimeout(() => { setTimeout(() => {
console.log("aborting"); logger.error(`[NODE-SHELL]: aborting wait for ${this.podName}, timing out`);
req.abort(); req.abort();
}, 2 * 60 * 1000); reject("Pod creation timed out");
}, 2 * 60 * 1000); // 2 * 60 * 1000
}) })
.catch(err => { .catch(error => {
console.log("watch failed"); logger.error(`[NODE-SHELL]: waiting for ${this.podName} failed: ${error}`);
reject(err); reject(error);
}); });
}); });
} }
@ -161,6 +165,7 @@ export class NodeShellSession extends ShellSession {
this this
.kc .kc
.makeApiClient(k8s.CoreV1Api) .makeApiClient(k8s.CoreV1Api)
.deleteNamespacedPod(this.podId, "kube-system"); .deleteNamespacedPod(this.podName, "kube-system")
.catch(error => logger.warn(`[NODE-SHELL]: failed to remove pod shell`, error));
} }
} }