From 8bb3954700c947cacc7699163b14bc5454e8dd77 Mon Sep 17 00:00:00 2001 From: Roman Date: Fri, 10 Jul 2020 23:48:57 +0300 Subject: [PATCH] clean up, debugging Signed-off-by: Roman --- src/common/cluster-store.ts | 4 ---- src/common/utils/index.ts | 1 + .../utils/randomFileName.ts} | 8 +------- src/main/cluster-manager.ts | 5 +++-- src/main/cluster.ts | 11 ++++------- src/main/kubeconfig-manager.ts | 8 ++++---- src/main/lens-proxy.ts | 4 ++-- 7 files changed, 15 insertions(+), 26 deletions(-) rename src/{main/file-helpers.ts => common/utils/randomFileName.ts} (55%) diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index bfaa09c0e1..9f151a7286 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -55,10 +55,6 @@ export class ClusterStore extends BaseStore { return Array.from(this.clusters.values()); } - @computed get inactiveClusters() { - return Array.from(this.clusters.values()).filter(cluster => !cluster.initialized); - } - getById(id: ClusterId): Cluster { return this.clusters.get(id); } diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index 35b65207ac..a5a2d9e3f5 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -3,3 +3,4 @@ export * from "./base64" export * from "./camelCase" export * from "./splitArray" +export * from "./randomFileName" diff --git a/src/main/file-helpers.ts b/src/common/utils/randomFileName.ts similarity index 55% rename from src/main/file-helpers.ts rename to src/common/utils/randomFileName.ts index 927c1b98a6..73d09301be 100644 --- a/src/main/file-helpers.ts +++ b/src/common/utils/randomFileName.ts @@ -1,10 +1,4 @@ -import fs from "fs" - -export function ensureDir(dirname: string) { - if (!fs.existsSync(dirname)) { - fs.mkdirSync(dirname) - } -} +// Creates system valid random filename export function randomFileName(name: string) { return `${Math.random().toString(36).substring(2, 15)}-${Math.random().toString(36).substring(2, 15)}-${name}` diff --git a/src/main/cluster-manager.ts b/src/main/cluster-manager.ts index 095f022ea6..be1059cead 100644 --- a/src/main/cluster-manager.ts +++ b/src/main/cluster-manager.ts @@ -26,9 +26,10 @@ export class ClusterManager { } constructor(public readonly proxyPort: number) { - // auto-init fresh clusters + // auto-init clusters autorun(() => { - clusterStore.inactiveClusters.forEach(cluster => { + const freshClusters = clusterStore.clustersList.filter(cluster => !cluster.initialized); + freshClusters.forEach(cluster => { cluster.init().then(() => cluster.refreshCluster()); }); }); diff --git a/src/main/cluster.ts b/src/main/cluster.ts index 360d0ce5bd..52f2679395 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -42,7 +42,7 @@ export class Cluster implements ClusterModel { @observable kubeConfigPath: string; @observable contextName: string; @observable port: number; - @observable url: string; + @observable url: string; // cluster-api url @observable apiUrl: UrlWithStringQuery; // same as url, but parsed @observable kubeAuthProxyUrl: string; @observable webContentUrl: string; @@ -66,6 +66,7 @@ export class Cluster implements ClusterModel { } @action + // fixme: completely broken async init() { try { this.contextHandler = new ContextHandler(this); @@ -76,7 +77,7 @@ export class Cluster implements ClusterModel { this.kubeconfigManager = new KubeconfigManager(this); this.url = this.kubeconfigManager.getCurrentClusterServer(); this.apiUrl = url.parse(this.url); - logger.info(`[CLUSTER]: INIT`, { + logger.info(`[CLUSTER]: init success`, { id: this.id, port: this.port, url: this.url, @@ -85,7 +86,7 @@ export class Cluster implements ClusterModel { }); this.initialized = true; } catch (err) { - logger.error(`[CLUSTER]: INIT FAILED`, { + logger.error(`[CLUSTER]: init error`, { id: this.id, error: err.stack, }); @@ -128,10 +129,6 @@ export class Cluster implements ClusterModel { return kc } - stopServer() { - this.contextHandler.stopServer() - } - async installFeature(name: string, config: any) { await installFeature(name, this, config) await this.refreshCluster() diff --git a/src/main/kubeconfig-manager.ts b/src/main/kubeconfig-manager.ts index a0f2a7ec5d..f1b5ac6433 100644 --- a/src/main/kubeconfig-manager.ts +++ b/src/main/kubeconfig-manager.ts @@ -1,8 +1,8 @@ import type { Cluster } from "./cluster" import { app } from "electron" -import fs from "fs" +import fs from "fs-extra" import { KubeConfig } from "@kubernetes/client-node" -import { ensureDir, randomFileName } from "./file-helpers" +import { randomFileName } from "../common/utils" import { dumpConfigYaml, loadKubeConfig } from "./k8s" import logger from "./logger" @@ -34,7 +34,7 @@ export class KubeconfigManager { * This way any user of the config does not need to know anything about the auth etc. details. */ protected createTemporaryKubeconfig(): string { - ensureDir(this.configDir); + fs.ensureDir(this.configDir); const path = `${this.configDir}/${randomFileName("kubeconfig")}`; const { contextName, kubeAuthProxyUrl } = this.cluster; const kubeConfig = this.loadConfig(); @@ -57,7 +57,7 @@ export class KubeconfigManager { } ]; fs.writeFileSync(path, dumpConfigYaml(kubeConfig)); - logger.info(`Created temp kube-config file at "${path}"`); + logger.info(`Created temp kube-config file for context "${this.cluster.contextName}" at "${path}"`); return path; } diff --git a/src/main/lens-proxy.ts b/src/main/lens-proxy.ts index 86a0386c2b..b836ece43a 100644 --- a/src/main/lens-proxy.ts +++ b/src/main/lens-proxy.ts @@ -29,8 +29,8 @@ export class LensProxy { listen(): this { const proxyServer = this.buildProxyServer(); const { proxyPort } = this.clusterManager; - this.proxyServer = proxyServer.listen(proxyPort, "127.0.0.1"); - logger.info(`Lens proxy server started at ${proxyPort}`); + this.proxyServer = proxyServer.listen(proxyPort); + logger.info(`LensProxy server has started http://localhost:${proxyPort}`); return this; }