From c1d7dcadc59fec8f214b634ae74308bb17d468c7 Mon Sep 17 00:00:00 2001 From: Denis Golovin Date: Fri, 5 Aug 2022 11:19:20 -0700 Subject: [PATCH 1/3] Set HTTP(S)_PROXY env variable with proxy-url prop from cluster Signed-off-by: Denis Golovin dgolovin@redhat.com --- src/odo.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/odo.ts b/src/odo.ts index 214b95bed..f814e269d 100644 --- a/src/odo.ts +++ b/src/odo.ts @@ -620,8 +620,8 @@ export class OdoImpl implements Odo { return deployedComponents; } - public getKubeconfigEnv(): {KUBECONFIG?: string} { - const addEnv: {KUBECONFIG?: string} = {}; + public getKubeconfigEnv(): {KUBECONFIG?: string, HTTP_PROXY?: string, HTTPS_PROXY?: string, [key: string] : any} { + const addEnv: {KUBECONFIG?: string, HTTP_PROXY?: string, HTTPS_PROXY?: string, [key: string] : any} = {}; let kc: KubeConfig; // TODO: Remove when odo works without kubeconfig present try { @@ -636,11 +636,28 @@ export class OdoImpl implements Odo { // use fake config to let odo get component types from registry addEnv.KUBECONFIG = path.resolve(__dirname, '..', '..', 'config', 'kubeconfig'); } + + if (kc && pathExistsSync(configPath)) { + // add HTTP(S)_PROXY env var in case cluster from current context has proxy-url property + const cc = kc.getContextObject(kc.currentContext); + const ccc = cc ? kc.getCluster(cc.cluster) : undefined; + const cccp:string = ccc?.['proxy-url']; // proxy url + if (cccp) { + const cccpu = Uri.parse(cccp); + // no scheme means http proxy + if (!cccpu.scheme || cccpu.scheme === 'http') { + addEnv.HTTP_PROXY = cccp; + } else { + // everything else is https proxy + addEnv.HTTPS_PROXY = cccp; + } + } + } return addEnv; } public async getCompTypesJson(): Promise { - const result: cliInstance.CliExitData = await this.execute(Command.listCatalogComponentsJson(), undefined, true, this.getKubeconfigEnv()); + const result: cliInstance.CliExitData = await this.execute(Command.listCatalogComponentsJson(), undefined, true); const compTypesJson: ComponentTypesJson = this.loadJSON(result.stdout); return compTypesJson?.items; } @@ -658,7 +675,7 @@ export class OdoImpl implements Odo { public async getComponentTypes(): Promise { // if kc is produced, KUBECONFIG env var is empty or pointing - const result: cliInstance.CliExitData = await this.execute(Command.listCatalogComponentsJson(), undefined, true, this.getKubeconfigEnv()); + const result: cliInstance.CliExitData = await this.execute(Command.listCatalogComponentsJson(), undefined, true); const compTypesJson: ComponentTypesJson = this.loadJSON(result.stdout); const devfileItems: ComponentTypeAdapter[] = []; @@ -786,7 +803,7 @@ export class OdoImpl implements Odo { terminal.show(); } - public async execute(command: CommandText, cwd?: string, fail = true, addEnv = {}): Promise { + public async execute(command: CommandText, cwd?: string, fail = true, addEnv = this.getKubeconfigEnv()): Promise { const env = this.createEnv(); const commandActual = `${command}`; const commandPrivacy = `${command.privacyMode(true)}`; From 50921ec3e7a8859ae0f1f7d20cc68f3507e4b372 Mon Sep 17 00:00:00 2001 From: Denis Golovin Date: Mon, 8 Aug 2022 15:19:40 -0700 Subject: [PATCH 2/3] Call getKubeconfigEnv() in createEnv() Signed-off-by: Denis Golovin dgolovin@redhat.com --- src/odo.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/odo.ts b/src/odo.ts index f814e269d..4e251b380 100644 --- a/src/odo.ts +++ b/src/odo.ts @@ -790,7 +790,7 @@ export class OdoImpl implements Odo { } public createEnv(): any { - const env = {...process.env }; + const env = {...process.env, ...this.getKubeconfigEnv() }; env.ODO_DISABLE_TELEMETRY = 'true'; return env; } @@ -803,7 +803,7 @@ export class OdoImpl implements Odo { terminal.show(); } - public async execute(command: CommandText, cwd?: string, fail = true, addEnv = this.getKubeconfigEnv()): Promise { + public async execute(command: CommandText, cwd?: string, fail = true, addEnv = {}): Promise { const env = this.createEnv(); const commandActual = `${command}`; const commandPrivacy = `${command.privacyMode(true)}`; From dab78fb884418ac94dac2974aa7eda935f0e911f Mon Sep 17 00:00:00 2001 From: Denis Golovin Date: Tue, 9 Aug 2022 13:39:17 -0700 Subject: [PATCH 3/3] Fix *_PROXY env variables calculation --- src/odo.ts | 24 ++++++++++++------------ src/util/kubeUtils.ts | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/odo.ts b/src/odo.ts index 4e251b380..9015e48e5 100644 --- a/src/odo.ts +++ b/src/odo.ts @@ -622,7 +622,7 @@ export class OdoImpl implements Odo { public getKubeconfigEnv(): {KUBECONFIG?: string, HTTP_PROXY?: string, HTTPS_PROXY?: string, [key: string] : any} { const addEnv: {KUBECONFIG?: string, HTTP_PROXY?: string, HTTPS_PROXY?: string, [key: string] : any} = {}; - let kc: KubeConfig; + let kc: KubeConfigUtils; // TODO: Remove when odo works without kubeconfig present try { kc = new KubeConfigUtils(); @@ -631,28 +631,28 @@ export class OdoImpl implements Odo { } const configPath = path.join(Platform.getUserHomePath(), '.kube', 'config'); - - if (kc && !pathExistsSync(configPath)) { // config is loaded, yay! But there is still use case for missing config file - // use fake config to let odo get component types from registry - addEnv.KUBECONFIG = path.resolve(__dirname, '..', '..', 'config', 'kubeconfig'); - } - - if (kc && pathExistsSync(configPath)) { + // kc loaded ether from files listed in env variable or default config location + if (kc && process.env.KUBECONFIG || pathExistsSync(configPath)) { // add HTTP(S)_PROXY env var in case cluster from current context has proxy-url property - const cc = kc.getContextObject(kc.currentContext); - const ccc = cc ? kc.getCluster(cc.cluster) : undefined; - const cccp:string = ccc?.['proxy-url']; // proxy url + const cccp:string = kc.getProxy(); if (cccp) { const cccpu = Uri.parse(cccp); // no scheme means http proxy if (!cccpu.scheme || cccpu.scheme === 'http') { addEnv.HTTP_PROXY = cccp; } else { - // everything else is https proxy + // everything else is https proxy for now addEnv.HTTPS_PROXY = cccp; } } } + + // config is loaded, but in case of no KUBECONFIG var or config file + // dummy config is created + if (kc && !addEnv.KUBECONFIG && !pathExistsSync(configPath)) { + // use fake config to let odo get component types from registry + addEnv.KUBECONFIG = path.resolve(__dirname, '..', '..', 'config', 'kubeconfig'); + } return addEnv; } diff --git a/src/util/kubeUtils.ts b/src/util/kubeUtils.ts index f1bc17c1a..d6eec92eb 100644 --- a/src/util/kubeUtils.ts +++ b/src/util/kubeUtils.ts @@ -31,7 +31,7 @@ export class KubeConfigUtils extends KubeConfig { return findHomeDir(); } - getProxy(contextName: string): string | undefined { + getProxy(contextName: string = this.currentContext): string | undefined { if (process.env.KUBECONFIG?.[1]) { const cFiles = process.env.KUBECONFIG.split(path.delimiter).filter(file => file); //const yaml =