Skip to content

Commit 9c3456a

Browse files
Merge pull request #2680 from yurnov/proxy-url-from-kubeconfig
Read proxy-url from kubeconfig cluster
2 parents 5d5e3ed + e192676 commit 9c3456a

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎kubernetes/base/config/kube_config.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ def _load_cluster_info(self):
523523
self.verify_ssl = not self._cluster['insecure-skip-tls-verify']
524524
if 'tls-server-name' in self._cluster:
525525
self.tls_server_name = self._cluster['tls-server-name']
526+
if 'proxy-url' in self._cluster:
527+
self.proxy = self._cluster['proxy-url']
526528

527529
def _set_config(self, client_configuration):
528530
if 'token' in self.__dict__:
@@ -534,7 +536,8 @@ def _refresh_api_key(client_configuration):
534536
self._set_config(client_configuration)
535537
client_configuration.refresh_api_key_hook = _refresh_api_key
536538
# copy these keys directly from self to configuration object
537-
keys = ['host', 'ssl_ca_cert', 'cert_file', 'key_file', 'verify_ssl','tls_server_name']
539+
keys = ['host', 'ssl_ca_cert', 'cert_file', 'key_file', 'verify_ssl',
540+
'tls_server_name', 'proxy']
538541
for key in keys:
539542
if key in self.__dict__:
540543
setattr(client_configuration, key, getattr(self, key))

‎kubernetes/base/config/kube_config_test.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def _raise_exception(st):
102102
TEST_CLIENT_CERT = "client-cert"
103103
TEST_CLIENT_CERT_BASE64 = _base64(TEST_CLIENT_CERT)
104104
TEST_TLS_SERVER_NAME = "kubernetes.io"
105+
TEST_PROXY_URL = "http://proxy.example.com:3128"
105106

106107
TEST_OIDC_TOKEN = "test-oidc-token"
107108
TEST_OIDC_INFO = "{\"name\": \"test\"}"
@@ -580,6 +581,13 @@ class TestKubeConfigLoader(BaseTestCase):
580581
"user": "ssl"
581582
}
582583
},
584+
{
585+
"name": "proxy_cluster_context",
586+
"context": {
587+
"cluster": "proxy_cluster",
588+
"user": "simple_token"
589+
}
590+
},
583591
],
584592
"clusters": [
585593
{
@@ -632,6 +640,13 @@ class TestKubeConfigLoader(BaseTestCase):
632640
"tls-server-name": TEST_TLS_SERVER_NAME,
633641
}
634642
},
643+
{
644+
"name": "proxy_cluster",
645+
"cluster": {
646+
"server": TEST_HOST,
647+
"proxy-url": TEST_PROXY_URL,
648+
}
649+
},
635650
],
636651
"users": [
637652
{
@@ -1136,6 +1151,18 @@ def test_tls_server_name(self):
11361151
active_context="tls-server-name").load_and_set(actual)
11371152
self.assertEqual(expected, actual)
11381153

1154+
def test_proxy_url_from_cluster(self):
1155+
expected = FakeConfig(
1156+
host=TEST_HOST,
1157+
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
1158+
proxy=TEST_PROXY_URL,
1159+
)
1160+
actual = FakeConfig()
1161+
KubeConfigLoader(
1162+
config_dict=self.TEST_KUBE_CONFIG,
1163+
active_context="proxy_cluster_context").load_and_set(actual)
1164+
self.assertEqual(expected, actual)
1165+
11391166
def test_list_contexts(self):
11401167
loader = KubeConfigLoader(
11411168
config_dict=self.TEST_KUBE_CONFIG,
@@ -1285,6 +1312,14 @@ def test_new_client_from_config(self):
12851312
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
12861313
client.configuration.api_key['BearerToken'])
12871314

1315+
def test_new_client_from_config_proxy(self):
1316+
config_file = self._create_temp_file(
1317+
yaml.safe_dump(self.TEST_KUBE_CONFIG))
1318+
client = new_client_from_config(
1319+
config_file=config_file, context="proxy_cluster_context")
1320+
self.assertEqual(TEST_HOST, client.configuration.host)
1321+
self.assertEqual(TEST_PROXY_URL, client.configuration.proxy)
1322+
12881323
def test_new_client_from_config_dict(self):
12891324
client = new_client_from_config_dict(
12901325
config_dict=self.TEST_KUBE_CONFIG, context="simple_token")

0 commit comments

Comments
 (0)