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

Support window nodes for node-shells (#3624)

* Support window nodes for node-shells

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Also check beta.kubernetes/os label

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add comment about linux fallback

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-11-08 20:11:40 -05:00 committed by GitHub
parent c09a57370e
commit 5eb1b8553d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 24 deletions

View File

@ -236,13 +236,10 @@ export class Node extends KubeObject {
}
getOperatingSystem(): string {
const label = this.getLabels().find(label => label.startsWith("kubernetes.io/os="));
if (label) {
return label.split("=", 2)[1];
}
return "linux";
return this.metadata?.labels?.["kubernetes.io/os"]
|| this.metadata?.labels?.["beta.kubernetes.io/os"]
|| this.status?.nodeInfo?.operatingSystem
|| "linux";
}
isUnschedulable() {

View File

@ -224,10 +224,7 @@ export class KubeApi<T extends KubeObject> {
isNamespaced = options.objectConstructor?.namespaced,
} = options || {};
if (!options.apiBase) {
options.apiBase = objectConstructor.apiBase;
}
const { apiBase, apiPrefix, apiGroup, apiVersion, resource } = parseKubeApi(options.apiBase);
const { apiBase, apiPrefix, apiGroup, apiVersion, resource } = parseKubeApi(options.apiBase || objectConstructor.apiBase);
this.kind = kind;
this.isNamespaced = isNamespaced;

View File

@ -21,6 +21,9 @@
import { JsonApi, JsonApiData, JsonApiError } from "./json-api";
import type { Response } from "node-fetch";
import type { Cluster } from "../../main/cluster";
import { LensProxy } from "../../main/lens-proxy";
import { apiKubePrefix, isDebugging } from "../vars";
export interface KubeJsonApiListMetadata {
resourceVersion: string;
@ -70,6 +73,20 @@ export interface KubeJsonApiError extends JsonApiError {
}
export class KubeJsonApi extends JsonApi<KubeJsonApiData> {
static forCluster(cluster: Cluster): KubeJsonApi {
const port = LensProxy.getInstance().port;
return new this({
serverAddress: `http://127.0.0.1:${port}`,
apiBase: apiKubePrefix,
debug: isDebugging,
}, {
headers: {
"Host": `${cluster.id}.localhost:${port}`,
},
});
}
protected parseError(error: KubeJsonApiError | any, res: Response): string[] {
const { status, reason, message } = error;
@ -80,4 +97,3 @@ export class KubeJsonApi extends JsonApi<KubeJsonApiData> {
return super.parseError(error, res);
}
}

View File

@ -240,27 +240,20 @@ export class ClusterManager extends Singleton {
}
getClusterForRequest(req: http.IncomingMessage): Cluster {
let cluster: Cluster = null;
// lens-server is connecting to 127.0.0.1:<port>/<uid>
if (req.headers.host.startsWith("127.0.0.1")) {
const clusterId = req.url.split("/")[1];
cluster = this.store.getById(clusterId);
const cluster = this.store.getById(clusterId);
if (cluster) {
// we need to swap path prefix so that request is proxied to kube api
req.url = req.url.replace(`/${clusterId}`, apiKubePrefix);
}
} else if (req.headers["x-cluster-id"]) {
cluster = this.store.getById(req.headers["x-cluster-id"].toString());
} else {
const clusterId = getClusterIdFromHost(req.headers.host);
cluster = this.store.getById(clusterId);
return cluster;
}
return cluster;
return this.store.getById(getClusterIdFromHost(req.headers.host));
}
}

View File

@ -26,6 +26,9 @@ import type { KubeConfig } from "@kubernetes/client-node";
import type { Cluster } from "../cluster";
import { ShellOpenError, ShellSession } from "./shell-session";
import { get } from "lodash";
import { Node, NodesApi } from "../../common/k8s-api/endpoints";
import { KubeJsonApi } from "../../common/k8s-api/kube-json-api";
import logger from "../logger";
export class NodeShellSession extends ShellSession {
ShellType = "node-shell";
@ -55,10 +58,27 @@ export class NodeShellSession extends ShellSession {
throw new ShellOpenError("failed to create node pod", error);
}
const args = ["exec", "-i", "-t", "-n", "kube-system", this.podId, "--", "sh", "-c", "((clear && bash) || (clear && ash) || (clear && sh))"];
const env = await this.getCachedShellEnv();
const args = ["exec", "-i", "-t", "-n", "kube-system", this.podId, "--"];
const nodeApi = new NodesApi({
objectConstructor: Node,
request: KubeJsonApi.forCluster(this.cluster),
});
const node = await nodeApi.get({ name: this.nodeName });
const nodeOs = node.getOperatingSystem();
await super.open(shell, args, env);
switch (nodeOs) {
default:
logger.warn(`[NODE-SHELL-SESSION]: could not determine node OS, falling back with assumption of linux`);
case "linux":
args.push("sh", "-c", "((clear && bash) || (clear && ash) || (clear && sh))");
break;
case "windows":
args.push("powershell");
break;
}
return super.open(shell, args, env);
}
protected createNodeShellPod() {