1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Reserve cluster proxy port just in time (#2554)

* reserve cluster proxy port just in time

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* refactor

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2021-04-22 07:18:53 +03:00 committed by GitHub
parent d0712b3c32
commit 33ca6c9c17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 34 deletions

View File

@ -76,7 +76,7 @@ describe("kubeconfig manager tests", () => {
}); });
const contextHandler = new ContextHandler(cluster); const contextHandler = new ContextHandler(cluster);
const port = await getFreePort(); 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(logger.error).not.toBeCalled();
expect(await kubeConfManager.getPath()).toBe(`tmp${path.sep}kubeconfig-foo`); expect(await kubeConfManager.getPath()).toBe(`tmp${path.sep}kubeconfig-foo`);
@ -98,13 +98,15 @@ describe("kubeconfig manager tests", () => {
}); });
const contextHandler = new ContextHandler(cluster); const contextHandler = new ContextHandler(cluster);
const port = await getFreePort(); const port = await getFreePort();
const kubeConfManager = await KubeconfigManager.create(cluster, contextHandler, port); const kubeConfManager = new KubeconfigManager(cluster, contextHandler, port);
const configPath = await kubeConfManager.getPath(); const configPath = await kubeConfManager.getPath();
expect(await fse.pathExists(configPath)).toBe(true); expect(await fse.pathExists(configPath)).toBe(true);
await kubeConfManager.unlink(); await kubeConfManager.unlink();
expect(await fse.pathExists(configPath)).toBe(false); expect(await fse.pathExists(configPath)).toBe(false);
await kubeConfManager.unlink(); // doesn't throw await kubeConfManager.unlink(); // doesn't throw
expect(await kubeConfManager.getPath()).toBeUndefined(); expect(async () => {
await kubeConfManager.getPath();
}).rejects.toThrow("already unlinked");
}); });
}); });

View File

@ -294,7 +294,7 @@ export class Cluster implements ClusterModel, ClusterState {
try { try {
this.initializing = true; this.initializing = true;
this.contextHandler = new ContextHandler(this); 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.kubeProxyUrl = `http://localhost:${port}${apiKubePrefix}`;
this.initialized = true; this.initialized = true;
logger.info(`[CLUSTER]: "${this.contextName}" init success`, { logger.info(`[CLUSTER]: "${this.contextName}" init success`, {

View File

@ -9,16 +9,39 @@ import logger from "./logger";
export class KubeconfigManager { export class KubeconfigManager {
protected configDir = app.getPath("temp"); 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) { async getPath(): Promise<string> {
const kcm = new KubeconfigManager(cluster, contextHandler, port); 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() { 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() { protected resolveProxyUrl() {
return `http://127.0.0.1:${this.port}/${this.cluster.id}`; return `http://127.0.0.1:${this.port}/${this.cluster.id}`;
} }
@ -87,14 +96,4 @@ export class KubeconfigManager {
return tempFile; return tempFile;
} }
async unlink() {
if (!this.tempFile) {
return;
}
logger.info(`Deleting temporary kubeconfig: ${this.tempFile}`);
await fs.unlink(this.tempFile);
this.tempFile = undefined;
}
} }