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;
constructor(protected router: Router, functions: LensProxyFunctions) {
constructor(protected router: Router, { shellApiRequest, kubeApiRequest, getClusterForRequest }: LensProxyFunctions) {
super();
const { shellApiRequest, kubeApiRequest } = functions;
this.getClusterForRequest = functions.getClusterForRequest;
this.getClusterForRequest = getClusterForRequest;
this.proxyServer = spdy.createServer({
spdy: {
@ -79,9 +77,15 @@ export class LensProxy extends Singleton {
this.proxyServer
.on("upgrade", (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => {
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;
(async () => reqHandler({ req, socket, head }))()
(async () => reqHandler({ req, socket, head, cluster }))()
.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 url from "url";
import { apiKubePrefix } from "../../common/vars";
import { ClusterManager } from "../cluster-manager";
import type { ProxyApiRequestArgs } from "./types";
const skipRawHeaders = new Set(["Host", "Authorization"]);
export async function kubeApiRequest({ req, socket, head }: ProxyApiRequestArgs) {
const cluster = ClusterManager.getInstance().getClusterForRequest(req);
if (!cluster) {
return;
}
export async function kubeApiRequest({ req, socket, head, cluster }: ProxyApiRequestArgs) {
const proxyUrl = await cluster.contextHandler.resolveAuthProxyUrl() + req.url.replace(apiKubePrefix, "");
const apiUrl = url.parse(cluster.apiUrl);
const pUrl = url.parse(proxyUrl);

View File

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

View File

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