Skip to content

add no_proxy parameter to configuration for REST and websocket client #1579

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

Merged
merged 1 commit into from
Oct 18, 2021
Merged
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
3 changes: 3 additions & 0 deletions kubernetes/client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ def __init__(self, host="http://localhost",
self.proxy = None
"""Proxy URL
"""
self.no_proxy = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These files are generated by openapi-generator and will get reverted when we re-generate the client. The proper way to fix it is to send a PR to https://github.com/OpenAPITools/openapi-generator. A mid-term workaround is to add a patch in this repo, for example: https://github.com/kubernetes-client/python/blob/master/scripts/rest_client_patch.diff

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@itaru2622 itaru2622 Oct 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roycaihw the above PR to openapi-generator is waiting final review/merging.

The code in PR against openapi becomes a little different from the code in this PR. but it provides the same effect.
and also it is expected that the unittest code in this PR works fine against openapi's implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks @itaru2622!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the above PR to openapi-generator is merged into its master, and it will be released as v5.3.1 (according to milestone)

"""bypass proxy for host in the no_proxy list.
"""
self.proxy_headers = None
"""Proxy headers
"""
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import urllib3

from kubernetes.client.exceptions import ApiException, ApiValueError
from requests.utils import should_bypass_proxies


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 27 additions & 1 deletion kubernetes/test/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import unittest

import kubernetes

from kubernetes.client.configuration import Configuration
import urllib3

class TestApiClient(unittest.TestCase):

Expand All @@ -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) )