From 33ca6c9c17f5f1cd7cb282bb06976a74d4baa194 Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Thu, 22 Apr 2021 07:18:53 +0300 Subject: [PATCH] Reserve cluster proxy port just in time (#2554) * reserve cluster proxy port just in time Signed-off-by: Jari Kolehmainen * fix Signed-off-by: Jari Kolehmainen * refactor Signed-off-by: Jari Kolehmainen --- src/main/__test__/kubeconfig-manager.test.ts | 8 ++- src/main/cluster.ts | 2 +- src/main/kubeconfig-manager.ts | 59 ++++++++++---------- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/main/__test__/kubeconfig-manager.test.ts b/src/main/__test__/kubeconfig-manager.test.ts index 5ae7681d37..cf93f71957 100644 --- a/src/main/__test__/kubeconfig-manager.test.ts +++ b/src/main/__test__/kubeconfig-manager.test.ts @@ -76,7 +76,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`); @@ -98,13 +98,15 @@ 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); await kubeConfManager.unlink(); expect(await fse.pathExists(configPath)).toBe(false); await kubeConfManager.unlink(); // doesn't throw - expect(await kubeConfManager.getPath()).toBeUndefined(); + expect(async () => { + await kubeConfManager.getPath(); + }).rejects.toThrow("already unlinked"); }); }); diff --git a/src/main/cluster.ts b/src/main/cluster.ts index c7a142cc30..6606a71605 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..6fe5cddf39 100644 --- a/src/main/kubeconfig-manager.ts +++ b/src/main/kubeconfig-manager.ts @@ -9,16 +9,39 @@ import logger from "./logger"; export class KubeconfigManager { protected configDir = app.getPath("temp"); - protected tempFile: string; + protected tempFile: string = null; - private constructor(protected cluster: Cluster, protected contextHandler: ContextHandler, protected port: number) { } + 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); + async getPath(): Promise { + if (this.tempFile === undefined) { + throw new Error("kubeconfig is already unlinked"); + } - await kcm.init(); + if (!this.tempFile) { + await this.init(); + } - return kcm; + // create proxy kubeconfig if it is removed without unlink called + if (!(await fs.pathExists(this.tempFile))) { + try { + this.tempFile = await this.createProxyKubeconfig(); + } catch (err) { + logger.error(`Failed to created temp config for auth-proxy`, { err }); + } + } + + 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() { @@ -30,20 +53,6 @@ export class KubeconfigManager { } } - async getPath() { - // create proxy kubeconfig if it is removed - if (this.tempFile !== undefined && !(await fs.pathExists(this.tempFile))) { - try { - this.tempFile = await this.createProxyKubeconfig(); - } catch (err) { - logger.error(`Failed to created temp config for auth-proxy`, { err }); - } - } - - return this.tempFile; - - } - protected resolveProxyUrl() { return `http://127.0.0.1:${this.port}/${this.cluster.id}`; } @@ -87,14 +96,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; - } }