Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 168c5e7

Browse files
authored
Merge pull request #15 from pokoli/proxy
Add http_proxy configuration with unittest
2 parents ab3fc54 + 10d7016 commit 168c5e7

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

configuration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ def __init__(self):
8282
# check host name
8383
# Set this to True/False to enable/disable SSL hostname verification.
8484
self.assert_hostname = None
85-
8685
# urllib3 connection pool's maximum number of connections saved
8786
# per pool. Increasing this is useful for cases when you are
8887
# making a lot of possibly parallel requests to the same host,
8988
# which is often the case here.
9089
# When set to `None`, will default to whatever urllib3 uses
9190
self.connection_pool_maxsize = None
91+
# http proxy setting
92+
self.http_proxy_url = None
9293

9394
# WebSocket subprotocol to use for exec and portforward.
9495
self.ws_streaming_protocol = "v4.channel.k8s.io"

rest.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,14 @@ def __init__(self, pools_size=4, config=configuration):
106106
kwargs['assert_hostname'] = config.assert_hostname
107107

108108
# https pool manager
109-
self.pool_manager = urllib3.PoolManager(
110-
**kwargs
111-
)
109+
if config.http_proxy_url is not None:
110+
self.pool_manager = urllib3.proxy_from_url(
111+
config.http_proxy_url, **kwargs
112+
)
113+
else:
114+
self.pool_manager = urllib3.PoolManager(
115+
**kwargs
116+
)
112117

113118
def request(self, method, url, query_params=None, headers=None,
114119
body=None, post_params=None, _preload_content=True,

rest_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2017 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import unittest
15+
import urllib3
16+
17+
from mock import patch
18+
19+
from kubernetes.client import ConfigurationObject
20+
from kubernetes.client.rest import RESTClientObject
21+
22+
23+
class RestTest(unittest.TestCase):
24+
25+
def test_poolmanager(self):
26+
'Test that a poolmanager is created for rest client'
27+
with patch.object(urllib3, 'PoolManager') as pool:
28+
RESTClientObject(config=ConfigurationObject())
29+
pool.assert_called_once()
30+
31+
def test_proxy(self):
32+
'Test that proxy is created when the config especifies it'
33+
config = ConfigurationObject()
34+
config.http_proxy_url = 'http://proxy.example.com'
35+
36+
with patch.object(urllib3, 'proxy_from_url') as proxy:
37+
RESTClientObject(config=config)
38+
proxy.assert_called_once()
39+
40+
41+
if __name__ == '__main__':
42+
unittest.main()

0 commit comments

Comments
 (0)