From a327e65e358c10e0b49664b276eaac99b93e6efa Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Mon, 19 Apr 2021 11:24:36 +0300 Subject: [PATCH] reserve cluster proxy port just in time Signed-off-by: Jari Kolehmainen --- src/main/__test__/kubeconfig-manager.test.ts | 4 +- src/main/cluster.ts | 2 +- src/main/kubeconfig-manager.ts | 51 +++++++++----------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/main/__test__/kubeconfig-manager.test.ts b/src/main/__test__/kubeconfig-manager.test.ts index 1cc39a4ada..38bfc73aea 100644 --- a/src/main/__test__/kubeconfig-manager.test.ts +++ b/src/main/__test__/kubeconfig-manager.test.ts @@ -80,7 +80,7 @@ describe("kubeconfig manager tests", () => { }); const contextHandler = new ContextHandler(cluster); const port = await getFreePort(); - const kubeConfManager = await KubeconfigManager.create(cluster, contextHandler, port); + const kubeConfManager = new KubeconfigManager(cluster, contextHandler, port); expect(logger.error).not.toBeCalled(); expect(await kubeConfManager.getPath()).toBe(`tmp${path.sep}kubeconfig-foo`); @@ -102,7 +102,7 @@ describe("kubeconfig manager tests", () => { }); const contextHandler = new ContextHandler(cluster); const port = await getFreePort(); - const kubeConfManager = await KubeconfigManager.create(cluster, contextHandler, port); + const kubeConfManager = new KubeconfigManager(cluster, contextHandler, port); const configPath = await kubeConfManager.getPath(); expect(await fse.pathExists(configPath)).toBe(true); diff --git a/src/main/cluster.ts b/src/main/cluster.ts index 7a02c3e0c9..5ee18646f0 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -294,7 +294,7 @@ export class Cluster implements ClusterModel, ClusterState { try { this.initializing = true; this.contextHandler = new ContextHandler(this); - this.kubeconfigManager = await KubeconfigManager.create(this, this.contextHandler, port); + this.kubeconfigManager = new KubeconfigManager(this, this.contextHandler, port); this.kubeProxyUrl = `http://localhost:${port}${apiKubePrefix}`; this.initialized = true; logger.info(`[CLUSTER]: "${this.contextName}" init success`, { diff --git a/src/main/kubeconfig-manager.ts b/src/main/kubeconfig-manager.ts index f88ce87840..d43a8105d2 100644 --- a/src/main/kubeconfig-manager.ts +++ b/src/main/kubeconfig-manager.ts @@ -11,26 +11,13 @@ export class KubeconfigManager { protected configDir = app.getPath("temp"); protected tempFile: string; - private constructor(protected cluster: Cluster, protected contextHandler: ContextHandler, protected port: number) { } - - static async create(cluster: Cluster, contextHandler: ContextHandler, port: number) { - const kcm = new KubeconfigManager(cluster, contextHandler, port); - - await kcm.init(); - - return kcm; - } - - protected async init() { - try { - await this.contextHandler.ensurePort(); - this.tempFile = await this.createProxyKubeconfig(); - } catch (err) { - logger.error(`Failed to created temp config for auth-proxy`, { err }); - } - } + constructor(protected cluster: Cluster, protected contextHandler: ContextHandler, protected port: number) { } async getPath() { + if (!this.tempFile) { + await this.init(); + } + // create proxy kubeconfig if it is removed if (this.tempFile !== undefined && !(await fs.pathExists(this.tempFile))) { try { @@ -41,7 +28,25 @@ export class KubeconfigManager { } return this.tempFile; + } + async unlink() { + if (!this.tempFile) { + return; + } + + logger.info(`Deleting temporary kubeconfig: ${this.tempFile}`); + await fs.unlink(this.tempFile); + this.tempFile = undefined; + } + + protected async init() { + try { + await this.contextHandler.ensurePort(); + this.tempFile = await this.createProxyKubeconfig(); + } catch (err) { + logger.error(`Failed to created temp config for auth-proxy`, { err }); + } } protected resolveProxyUrl() { @@ -87,14 +92,4 @@ export class KubeconfigManager { return tempFile; } - - async unlink() { - if (!this.tempFile) { - return; - } - - logger.info(`Deleting temporary kubeconfig: ${this.tempFile}`); - await fs.unlink(this.tempFile); - this.tempFile = undefined; - } }