Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set HTTP(S)_PROXY env variable with proxy-url prop from cluster #2504

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/odo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,9 @@ export class OdoImpl implements Odo {
return deployedComponents;
}

public getKubeconfigEnv(): {KUBECONFIG?: string} {
const addEnv: {KUBECONFIG?: string} = {};
let kc: KubeConfig;
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: KubeConfigUtils;
// TODO: Remove when odo works without kubeconfig present
try {
kc = new KubeConfigUtils();
Expand All @@ -631,16 +631,33 @@ export class OdoImpl implements Odo {
}

const configPath = path.join(Platform.getUserHomePath(), '.kube', 'config');
// 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 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 for now
addEnv.HTTPS_PROXY = cccp;
}
}
}

if (kc && !pathExistsSync(configPath)) { // config is loaded, yay! But there is still use case for missing config file
// 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;
}

public async getCompTypesJson(): Promise<DevfileComponentType[]> {
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;
}
Expand All @@ -658,7 +675,7 @@ export class OdoImpl implements Odo {
public async getComponentTypes(): Promise<ComponentType[]> {
// 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[] = [];

Expand Down Expand Up @@ -773,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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/kubeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down