Skip to content

Commit

Permalink
fix: whitelist k8s service in no_proxy environment variable (#458)
Browse files Browse the repository at this point in the history
Signed-off-by: vitaliy-guliy <[email protected]>
  • Loading branch information
vitaliy-guliy authored Dec 2, 2024
1 parent 4f9ec7f commit 96b74da
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions code/extensions/che-api/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@ export async function activate(_extensionContext: vscode.ExtensionContext): Prom
_extensionContext.environmentVariableCollection.replace('WORKSPACE_NAMESPACE', workspaceNamespace);
_extensionContext.environmentVariableCollection.replace('PROJECTS_ROOT', projectsRoot);

await container.get(K8SServiceImpl).ensureKubernetesServiceHostWhitelisted();

return api;
}
32 changes: 32 additions & 0 deletions code/extensions/che-api/src/impl/k8s-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import {
} from '@devfile/api';
import * as k8s from '@kubernetes/client-node';

import * as vscode from 'vscode';
import { ApiType } from '@kubernetes/client-node';
import { injectable } from 'inversify';
import { K8SRawResponse, K8SService } from '../api/k8s-service';
import { env } from 'process';

const request = require('request');

Expand All @@ -36,6 +38,36 @@ export class K8SServiceImpl implements K8SService {
this.k8sConfig.loadFromCluster();
}

async ensureKubernetesServiceHostWhitelisted(): Promise<void> {
const proxy = env.HTTPS_PROXY || env.HTTP_PROXY || env.https_proxy || env.http_proxy;
const noProxy = env.NO_PROXY || env.no_proxy;
if (proxy && noProxy && env.KUBERNETES_SERVICE_HOST) {

// take k8s service host
const k8sHost = env.KUBERNETES_SERVICE_HOST;

// check whether it is set to no_proxy environment variable
if (!noProxy.split(',').includes(k8sHost)) {
const action = await vscode.window.showInformationMessage(
'The cluster you are using is behind a proxy, but kubernetes service host is not whitelisted in no_proxy environment variable. ' +
'This may cause the kubernetes service to be unaccessible. ' +
'Do you want to fix this and add the kubernetes service host to the no_proxy environment variable?', 'Add', 'Cancel');

if ('Add' === action) {
if (env.NO_PROXY) {
env.NO_PROXY += ',' + k8sHost;
console.log('Kubernetes Service Host has been added to env.NO_PROXY environment variable');
}
if (env.no_proxy) {
env.no_proxy += ',' + k8sHost;
console.log('Kubernetes Service Host has been added to env.no_proxy environment variable');
}
}
}

}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
sendRawQuery(requestURL: string, opts: any): Promise<K8SRawResponse> {
this.k8sConfig.applyToRequest(opts);
Expand Down

0 comments on commit 96b74da

Please sign in to comment.