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

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2021-04-19 11:24:36 +03:00
parent 61b00b4c12
commit a327e65e35
3 changed files with 26 additions and 31 deletions

View File

@ -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);

View File

@ -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`, {

View File

@ -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;
}
}