From 606d1a9198227994fa0b93ad080094d8d2808d3f Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 15 Jun 2020 22:00:48 +0300 Subject: [PATCH] chore --- src/main/cluster-manager.ts | 4 ++-- src/main/context-handler.ts | 14 +++++--------- src/main/index_backup.ts | 8 ++++---- src/main/kube-auth-proxy.ts | 6 ++++-- src/main/proxy.ts | 19 ++++++++----------- 5 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/main/cluster-manager.ts b/src/main/cluster-manager.ts index 29786b4914..7ab828a6fd 100644 --- a/src/main/cluster-manager.ts +++ b/src/main/cluster-manager.ts @@ -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, diff --git a/src/main/context-handler.ts b/src/main/context-handler.ts index f3a6d463b7..cd9c446eff 100644 --- a/src/main/context-handler.ts +++ b/src/main/context-handler.ts @@ -87,8 +87,8 @@ export class ContextHandler { } protected async resolvePrometheusPath(): Promise { - 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 { 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) { @@ -204,10 +204,6 @@ export class ContextHandler { } public proxyServerError() { - if (!this.proxyServer) { - return null - } - - return this.proxyServer.lastError + return this.proxyServer?.lastError || "" } } diff --git a/src/main/index_backup.ts b/src/main/index_backup.ts index 0ccd1a447e..195f5c5ad1 100644 --- a/src/main/index_backup.ts +++ b/src/main/index_backup.ts @@ -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); +// } diff --git a/src/main/kube-auth-proxy.ts b/src/main/kube-auth-proxy.ts index d825918330..19d5fae08c 100644 --- a/src/main/kube-auth-proxy.ts +++ b/src/main/kube-auth-proxy.ts @@ -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() { diff --git a/src/main/proxy.ts b/src/main/proxy.ts index 7f13e80256..160a96276a 100644 --- a/src/main/proxy.ts +++ b/src/main/proxy.ts @@ -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(async (resolve, reject) => { @@ -108,7 +105,7 @@ export class LensProxy { }) }) }) - }).bind(this)) + })) return ws }