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

View File

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

View File

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

View File

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

View File

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