Skip to content

Commit 95a893c

Browse files
committed
add no_proxy support to configuration and REST client.
1 parent 19d3bc9 commit 95a893c

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

kubernetes/client/configuration.py

+3
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ def __init__(self, host="http://localhost",
156156
self.proxy = None
157157
"""Proxy URL
158158
"""
159+
self.no_proxy = None
160+
"""bypass proxy for host in the no_proxy list.
161+
"""
159162
self.proxy_headers = None
160163
"""Proxy headers
161164
"""

kubernetes/client/rest.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import urllib3
2626

2727
from kubernetes.client.exceptions import ApiException, ApiValueError
28+
from requests.utils import should_bypass_proxies
2829

2930

3031
logger = logging.getLogger(__name__)
@@ -83,7 +84,7 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
8384
maxsize = 4
8485

8586
# https pool manager
86-
if configuration.proxy:
87+
if configuration.proxy and not should_bypass_proxies(configuration.host, no_proxy=configuration.no_proxy or ''):
8788
self.pool_manager = urllib3.ProxyManager(
8889
num_pools=pools_size,
8990
maxsize=maxsize,

kubernetes/test/test_api_client.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import unittest
77

88
import kubernetes
9-
9+
from kubernetes.client.configuration import Configuration
10+
import urllib3
1011

1112
class TestApiClient(unittest.TestCase):
1213

@@ -23,3 +24,28 @@ def test_atexit_closes_threadpool(self):
2324
self.assertIsNotNone(client._pool)
2425
atexit._run_exitfuncs()
2526
self.assertIsNone(client._pool)
27+
28+
def test_rest_proxycare(self):
29+
30+
pool = { 'proxy': urllib3.ProxyManager, 'direct': urllib3.PoolManager }
31+
32+
for dst, proxy, no_proxy, expected_pool in [
33+
( 'http://kube.local/', None, None, pool['direct']),
34+
( 'http://kube.local/', 'http://proxy.local:8080/', None, pool['proxy']),
35+
( 'http://127.0.0.1:8080/', 'http://proxy.local:8080/', 'localhost,127.0.0.0/8,.local', pool['direct']),
36+
( 'http://kube.local/', 'http://proxy.local:8080/', 'localhost,127.0.0.0/8,.local', pool['direct']),
37+
( 'http://kube.others.com:1234/','http://proxy.local:8080/', 'localhost,127.0.0.0/8,.local', pool['proxy']),
38+
( 'http://kube.others.com:1234/','http://proxy.local:8080/', '*', pool['direct']),
39+
]:
40+
# setup input
41+
config = Configuration()
42+
setattr(config, 'host', dst)
43+
if proxy is not None:
44+
setattr(config, 'proxy', proxy)
45+
if no_proxy is not None:
46+
setattr(config, 'no_proxy', no_proxy)
47+
# setup done
48+
49+
# test
50+
client = kubernetes.client.ApiClient(configuration=config)
51+
self.assertEqual( expected_pool, type(client.rest_client.pool_manager) )

0 commit comments

Comments
 (0)