1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
This commit is contained in:
Roman 2020-06-15 22:00:48 +03:00
parent d6258b38d0
commit 606d1a9198
5 changed files with 23 additions and 28 deletions

View File

@ -11,7 +11,7 @@ import path from "path"
import { promises } from "fs"
import { ensureDir } from "fs-extra"
import filenamify from "filenamify"
import uuid from "uuid"
import { v4 as uuid } from "uuid"
export type FeatureInstallRequest = {
name: string;
@ -92,7 +92,7 @@ export class ClusterManager {
configs.forEach(c => {
k8s.validateConfig(c)
const cluster = new Cluster({
id: uuid.v4(),
id: uuid(),
port: this.port,
kubeConfig: k8s.dumpConfigYaml(c),
preferences: clusterData.preferences,

View File

@ -87,8 +87,8 @@ export class ContextHandler {
}
protected async resolvePrometheusPath(): Promise<string> {
const service = await this.getPrometheusService()
return `${service.namespace}/services/${service.service}:${service.port}`
const {service, namespace, port} = await this.getPrometheusService()
return `${namespace}/services/${service}:${port}`
}
public async getPrometheusProvider() {
@ -129,7 +129,7 @@ export class ContextHandler {
return this.prometheusPath
}
public async getApiTarget(isWatchRequest = false) {
public async getApiTarget(isWatchRequest = false): Promise<ServerOptions> {
if (this.apiTarget && !isWatchRequest) {
return this.apiTarget
}
@ -173,7 +173,7 @@ export class ContextHandler {
}
public applyHeaders(req: http.IncomingMessage) {
req.headers["authorization"] = `Bearer ${this.id}`
req.headers["authorization"] = `Bearer ${this.id}` // Q: how it works when id == cluster.id ?
}
public async withTemporaryKubeconfig(callback: (kubeconfig: string) => Promise<any>) {
@ -204,10 +204,6 @@ export class ContextHandler {
}
public proxyServerError() {
if (!this.proxyServer) {
return null
}
return this.proxyServer.lastError
return this.proxyServer?.lastError || ""
}
}

View File

@ -133,7 +133,7 @@ app.on("will-quit", async (event) => {
app.exit(0);
})
// auto-restart app in dev-mode
if (isDevelopment) {
require('electron-reloader')(module);
}
// todo: check auto-restart app in dev-mode
// if (isDevelopment) {
// require('electron-reloader')(module);
// }

View File

@ -7,6 +7,7 @@ import { readFileSync, watch } from "fs"
import { PromiseIpc } from "electron-promise-ipc"
import { findMainWebContents } from "./webcontents"
import * as url from "url"
import { apiPrefix } from "../common/vars";
export class KubeAuthProxy {
public lastError: string
@ -45,9 +46,10 @@ export class KubeAuthProxy {
const clusterUrl = url.parse(this.cluster.apiUrl)
let args = [
"proxy",
"-p", this.port.toString(),
"--port", this.port.toString(),
"--kubeconfig", this.cluster.kubeconfigPath(),
"--accept-hosts", clusterUrl.hostname,
"--api-prefix", apiPrefix.BASE,
]
if (process.env.DEBUG_PROXY === "true") {
args = args.concat(["-v", "9"])
@ -94,7 +96,7 @@ export class KubeAuthProxy {
}
protected async sendIpcLogMessage(data: string, stream: string) {
await this.promiseIpc.send(`kube-auth:${this.cluster.id}`, findMainWebContents(), { data: data, stream: stream })
await this.promiseIpc.send(`kube-auth:${this.cluster.id}`, findMainWebContents(), { data, stream })
}
public exit() {

View File

@ -31,17 +31,15 @@ export class LensProxy {
protected buildProxyServer() {
const proxy = this.createProxy();
const proxyServer = http.createServer(function(req: http.IncomingMessage, res: http.ServerResponse) {
const proxyServer = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
this.handleRequest(proxy, req, res);
}.bind(this));
proxyServer.on("upgrade", function(req: http.IncomingMessage, socket: Socket, head: Buffer) {
});
proxyServer.on("upgrade", (req: http.IncomingMessage, socket: Socket, head: Buffer) => {
this.handleWsUpgrade(req, socket, head)
}.bind(this));
});
proxyServer.on("error", (err) => {
logger.error(err)
});
return proxyServer;
}
@ -55,11 +53,10 @@ export class LensProxy {
res.writeHead(proxyRes.statusCode, {
"Content-Type": "text/plain"
})
res.end(cluster.contextHandler.proxyServerError().toString())
res.end(cluster.contextHandler.proxyServerError())
return
}
}
if (req.method !== "GET") {
return
}
@ -94,11 +91,11 @@ export class LensProxy {
}
protected createWsListener() {
const ws = new WebSocket.Server({ noServer: true})
const ws = new WebSocket.Server({ noServer: true })
ws.on("connection", ((con: WebSocket, req: http.IncomingMessage) => {
const cluster = this.clusterManager.getClusterForRequest(req)
const contextHandler = cluster.contextHandler
const nodeParam = (url.parse(req.url, true).query["node"] || "").toString();
const nodeParam = url.parse(req.url, true).query["node"]?.toString();
contextHandler.withTemporaryKubeconfig((kubeconfigPath) => {
return new Promise<boolean>(async (resolve, reject) => {
@ -108,7 +105,7 @@ export class LensProxy {
})
})
})
}).bind(this))
}))
return ws
}