diff --git a/kubernetes/client/configuration.py b/kubernetes/client/configuration.py index d6b8f5f6e3..33aec424ee 100644 --- a/kubernetes/client/configuration.py +++ b/kubernetes/client/configuration.py @@ -156,6 +156,9 @@ def __init__(self, host="http://localhost", self.proxy = None """Proxy URL """ + self.no_proxy = None + """bypass proxy for host in the no_proxy list. + """ self.proxy_headers = None """Proxy headers """ diff --git a/kubernetes/client/rest.py b/kubernetes/client/rest.py index 2f3241611e..9e1b3859d5 100644 --- a/kubernetes/client/rest.py +++ b/kubernetes/client/rest.py @@ -25,6 +25,7 @@ import urllib3 from kubernetes.client.exceptions import ApiException, ApiValueError +from requests.utils import should_bypass_proxies logger = logging.getLogger(__name__) @@ -83,7 +84,7 @@ def __init__(self, configuration, pools_size=4, maxsize=None): maxsize = 4 # https pool manager - if configuration.proxy: + if configuration.proxy and not should_bypass_proxies(configuration.host, no_proxy=configuration.no_proxy or ''): self.pool_manager = urllib3.ProxyManager( num_pools=pools_size, maxsize=maxsize, diff --git a/kubernetes/test/test_api_client.py b/kubernetes/test/test_api_client.py index f0a9416cf7..486b4ac5b8 100644 --- a/kubernetes/test/test_api_client.py +++ b/kubernetes/test/test_api_client.py @@ -6,7 +6,8 @@ import unittest import kubernetes - +from kubernetes.client.configuration import Configuration +import urllib3 class TestApiClient(unittest.TestCase): @@ -23,3 +24,28 @@ def test_atexit_closes_threadpool(self): self.assertIsNotNone(client._pool) atexit._run_exitfuncs() self.assertIsNone(client._pool) + + def test_rest_proxycare(self): + + pool = { 'proxy': urllib3.ProxyManager, 'direct': urllib3.PoolManager } + + for dst, proxy, no_proxy, expected_pool in [ + ( 'http://kube.local/', None, None, pool['direct']), + ( 'http://kube.local/', 'http://proxy.local:8080/', None, pool['proxy']), + ( 'http://127.0.0.1:8080/', 'http://proxy.local:8080/', 'localhost,127.0.0.0/8,.local', pool['direct']), + ( 'http://kube.local/', 'http://proxy.local:8080/', 'localhost,127.0.0.0/8,.local', pool['direct']), + ( 'http://kube.others.com:1234/','http://proxy.local:8080/', 'localhost,127.0.0.0/8,.local', pool['proxy']), + ( 'http://kube.others.com:1234/','http://proxy.local:8080/', '*', pool['direct']), + ]: + # setup input + config = Configuration() + setattr(config, 'host', dst) + if proxy is not None: + setattr(config, 'proxy', proxy) + if no_proxy is not None: + setattr(config, 'no_proxy', no_proxy) + # setup done + + # test + client = kubernetes.client.ApiClient(configuration=config) + self.assertEqual( expected_pool, type(client.rest_client.pool_manager) )