From 0fc7bbbefc6ad9dc52aa77d1219c714d4388f316 Mon Sep 17 00:00:00 2001 From: mbohlool Date: Wed, 7 Jun 2017 20:19:20 -0700 Subject: [PATCH 1/3] added configuration 'http_proxy' to allow the usage of a proxy --- configuration.py | 3 ++- rest.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/configuration.py b/configuration.py index 4377eb8f7..3e211d117 100644 --- a/configuration.py +++ b/configuration.py @@ -82,13 +82,14 @@ class ConfigurationObject(object): # check host name # Set this to True/False to enable/disable SSL hostname verification. self.assert_hostname = None - # urllib3 connection pool's maximum number of connections saved # per pool. Increasing this is useful for cases when you are # making a lot of possibly parallel requests to the same host, # which is often the case here. # When set to `None`, will default to whatever urllib3 uses self.connection_pool_maxsize = None + # http proxy setting + self.http_proxy_url = None # WebSocket subprotocol to use for exec and portforward. self.ws_streaming_protocol = "v4.channel.k8s.io" diff --git a/rest.py b/rest.py index 5c2b39ff2..2dd18f20e 100644 --- a/rest.py +++ b/rest.py @@ -106,9 +106,14 @@ class RESTClientObject(object): kwargs['assert_hostname'] = config.assert_hostname # https pool manager - self.pool_manager = urllib3.PoolManager( - **kwargs - ) + if config.http_proxy_url is not None: + self.pool_manager = urllib3.proxy_from_url( + config.http_proxy_url, **kwargs + ) + else: + self.pool_manager = urllib3.PoolManager( + **kwargs + ) def request(self, method, url, query_params=None, headers=None, body=None, post_params=None, _preload_content=True, From a161197020a120a81ba8c59f58be77cfcfb8b426 Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Tue, 13 Jun 2017 11:07:11 +0200 Subject: [PATCH 2/3] Add unitest for restclient PoolManager and http proxy --- rest_test.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 rest_test.py diff --git a/rest_test.py b/rest_test.py new file mode 100644 index 000000000..c75b18eeb --- /dev/null +++ b/rest_test.py @@ -0,0 +1,42 @@ +# Copyright 2017 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import unittest +import urllib3 + +from mock import patch + +from kubernetes.client import Configuration +from kubernetes.client.rest import RESTClientObject + + +class RestTest(unittest.TestCase): + + def test_poolmanager(self): + 'Test that a poolmanager is created for rest client' + with patch.object(urllib3, 'PoolManager') as pool: + RESTClientObject(config=Configuration()) + pool.assert_called_once() + + def test_proxy(self): + 'Test that proxy is created when the config especifies it' + config = Configuration() + config.http_proxy_url = 'http://proxy.example.com' + + with patch.object(urllib3, 'proxy_from_url') as proxy: + RESTClientObject(config=config) + proxy.assert_called_once() + + +if __name__ == '__main__': + unittest.main() From 10d7016cb659d8a31b0df16f4fad1f8e114e7fe5 Mon Sep 17 00:00:00 2001 From: mbohlool Date: Tue, 25 Jul 2017 12:46:36 -0700 Subject: [PATCH 3/3] Use ConfigurationObject --- rest_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rest_test.py b/rest_test.py index c75b18eeb..007e1c649 100644 --- a/rest_test.py +++ b/rest_test.py @@ -16,7 +16,7 @@ import urllib3 from mock import patch -from kubernetes.client import Configuration +from kubernetes.client import ConfigurationObject from kubernetes.client.rest import RESTClientObject @@ -25,12 +25,12 @@ class RestTest(unittest.TestCase): def test_poolmanager(self): 'Test that a poolmanager is created for rest client' with patch.object(urllib3, 'PoolManager') as pool: - RESTClientObject(config=Configuration()) + RESTClientObject(config=ConfigurationObject()) pool.assert_called_once() def test_proxy(self): 'Test that proxy is created when the config especifies it' - config = Configuration() + config = ConfigurationObject() config.http_proxy_url = 'http://proxy.example.com' with patch.object(urllib3, 'proxy_from_url') as proxy: