Skip to content

Commit 2132879

Browse files
author
vadym1226
committed
Merge pull request #34 from kubernetes-client/b1
Change utility functions to use newly generated client
2 parents f05bf50 + 232aaa3 commit 2132879

8 files changed

+43
-1328
lines changed

api_client.py

-657
This file was deleted.

config/incluster_config.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import os
1616

17-
from kubernetes.client import configuration
17+
from kubernetes.client import Configuration
1818

1919
from .config_exception import ConfigException
2020

@@ -77,9 +77,11 @@ def _load_config(self):
7777
self.ssl_ca_cert = self._cert_filename
7878

7979
def _set_config(self):
80+
configuration = Configuration()
8081
configuration.host = self.host
8182
configuration.ssl_ca_cert = self.ssl_ca_cert
8283
configuration.api_key['authorization'] = "bearer " + self.token
84+
Configuration.set_default(configuration)
8385

8486

8587
def load_incluster_config():

config/kube_config.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import urllib3
2424
import yaml
2525

26-
from kubernetes.client import ApiClient, ConfigurationObject, configuration
26+
from kubernetes.client import ApiClient, Configuration
2727

2828
from .config_exception import ConfigException
2929
from .dateutil import UTC, format_rfc3339, parse_rfc3339
@@ -118,7 +118,6 @@ class KubeConfigLoader(object):
118118

119119
def __init__(self, config_dict, active_context=None,
120120
get_google_credentials=None,
121-
client_configuration=configuration,
122121
config_base_path="",
123122
config_persister=None):
124123
self._config = ConfigNode('kube-config', config_dict)
@@ -139,7 +138,6 @@ def _refresh_credentials():
139138
self._get_google_credentials = get_google_credentials
140139
else:
141140
self._get_google_credentials = _refresh_credentials
142-
self._client_configuration = client_configuration
143141

144142
def set_active_context(self, context_name=None):
145143
if context_name is None:
@@ -240,19 +238,19 @@ def _load_cluster_info(self):
240238
if 'insecure-skip-tls-verify' in self._cluster:
241239
self.verify_ssl = not self._cluster['insecure-skip-tls-verify']
242240

243-
def _set_config(self):
241+
def _set_config(self, client_configuration):
244242
if 'token' in self.__dict__:
245-
self._client_configuration.api_key['authorization'] = self.token
243+
client_configuration.api_key['authorization'] = self.token
246244
# copy these keys directly from self to configuration object
247245
keys = ['host', 'ssl_ca_cert', 'cert_file', 'key_file', 'verify_ssl']
248246
for key in keys:
249247
if key in self.__dict__:
250-
setattr(self._client_configuration, key, getattr(self, key))
248+
setattr(client_configuration, key, getattr(self, key))
251249

252-
def load_and_set(self):
250+
def load_and_set(self, client_configuration):
253251
self._load_authentication()
254252
self._load_cluster_info()
255-
self._set_config()
253+
self._set_config(client_configuration)
256254

257255
def list_contexts(self):
258256
return [context.value for context in self._config['contexts']]
@@ -331,15 +329,15 @@ def list_kube_config_contexts(config_file=None):
331329

332330

333331
def load_kube_config(config_file=None, context=None,
334-
client_configuration=configuration,
332+
client_configuration=None,
335333
persist_config=True):
336334
"""Loads authentication and cluster information from kube-config file
337335
and stores them in kubernetes.client.configuration.
338336
339337
:param config_file: Name of the kube-config file.
340338
:param context: set the active context. If is set to None, current_context
341339
from config file will be used.
342-
:param client_configuration: The kubernetes.client.ConfigurationObject to
340+
:param client_configuration: The kubernetes.client.Configuration to
343341
set configs to.
344342
:param persist_config: If True, config file will be updated when changed
345343
(e.g GCP token refresh).
@@ -355,10 +353,15 @@ def _save_kube_config(config_map):
355353
yaml.safe_dump(config_map, f, default_flow_style=False)
356354
config_persister = _save_kube_config
357355

358-
_get_kube_config_loader_for_yaml_file(
356+
loader = _get_kube_config_loader_for_yaml_file(
359357
config_file, active_context=context,
360-
client_configuration=client_configuration,
361-
config_persister=config_persister).load_and_set()
358+
config_persister=config_persister)
359+
if client_configuration is None:
360+
config = type.__call__(Configuration)
361+
loader.load_and_set(config)
362+
Configuration.set_default(config)
363+
else:
364+
loader.load_and_set(client_configuration)
362365

363366

364367
def new_client_from_config(
@@ -368,8 +371,8 @@ def new_client_from_config(
368371
"""Loads configuration the same as load_kube_config but returns an ApiClient
369372
to be used with any API object. This will allow the caller to concurrently
370373
talk with multiple clusters."""
371-
client_config = ConfigurationObject()
374+
client_config = type.__call__(Configuration)
372375
load_kube_config(config_file=config_file, context=context,
373376
client_configuration=client_config,
374377
persist_config=persist_config)
375-
return ApiClient(config=client_config)
378+
return ApiClient(configuration=client_config)

config/kube_config_test.py

+18-26
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ def _create_temp_file(self, content=""):
8383
os.close(handler)
8484
return name
8585

86-
def expect_exception(self, func, message_part):
86+
def expect_exception(self, func, message_part, *args, **kwargs):
8787
with self.assertRaises(ConfigException) as context:
88-
func()
88+
func(*args, **kwargs)
8989
self.assertIn(message_part, str(context.exception))
9090

9191

@@ -473,8 +473,7 @@ def test_no_user_context(self):
473473
actual = FakeConfig()
474474
KubeConfigLoader(
475475
config_dict=self.TEST_KUBE_CONFIG,
476-
active_context="no_user",
477-
client_configuration=actual).load_and_set()
476+
active_context="no_user").load_and_set(actual)
478477
self.assertEqual(expected, actual)
479478

480479
def test_simple_token(self):
@@ -483,8 +482,7 @@ def test_simple_token(self):
483482
actual = FakeConfig()
484483
KubeConfigLoader(
485484
config_dict=self.TEST_KUBE_CONFIG,
486-
active_context="simple_token",
487-
client_configuration=actual).load_and_set()
485+
active_context="simple_token").load_and_set(actual)
488486
self.assertEqual(expected, actual)
489487

490488
def test_load_user_token(self):
@@ -502,9 +500,8 @@ def test_gcp_no_refresh(self):
502500
KubeConfigLoader(
503501
config_dict=self.TEST_KUBE_CONFIG,
504502
active_context="gcp",
505-
client_configuration=actual,
506503
get_google_credentials=lambda: _raise_exception(
507-
"SHOULD NOT BE CALLED")).load_and_set()
504+
"SHOULD NOT BE CALLED")).load_and_set(actual)
508505
self.assertEqual(expected, actual)
509506

510507
def test_load_gcp_token_no_refresh(self):
@@ -536,8 +533,7 @@ def test_user_pass(self):
536533
actual = FakeConfig()
537534
KubeConfigLoader(
538535
config_dict=self.TEST_KUBE_CONFIG,
539-
active_context="user_pass",
540-
client_configuration=actual).load_and_set()
536+
active_context="user_pass").load_and_set(actual)
541537
self.assertEqual(expected, actual)
542538

543539
def test_load_user_pass_token(self):
@@ -548,12 +544,13 @@ def test_load_user_pass_token(self):
548544
self.assertEqual(TEST_BASIC_TOKEN, loader.token)
549545

550546
def test_ssl_no_cert_files(self):
551-
actual = FakeConfig()
552547
loader = KubeConfigLoader(
553548
config_dict=self.TEST_KUBE_CONFIG,
554-
active_context="ssl-no_file",
555-
client_configuration=actual)
556-
self.expect_exception(loader.load_and_set, "does not exists")
549+
active_context="ssl-no_file")
550+
self.expect_exception(
551+
loader.load_and_set,
552+
"does not exists",
553+
FakeConfig())
557554

558555
def test_ssl(self):
559556
expected = FakeConfig(
@@ -566,8 +563,7 @@ def test_ssl(self):
566563
actual = FakeConfig()
567564
KubeConfigLoader(
568565
config_dict=self.TEST_KUBE_CONFIG,
569-
active_context="ssl",
570-
client_configuration=actual).load_and_set()
566+
active_context="ssl").load_and_set(actual)
571567
self.assertEqual(expected, actual)
572568

573569
def test_ssl_no_verification(self):
@@ -582,8 +578,7 @@ def test_ssl_no_verification(self):
582578
actual = FakeConfig()
583579
KubeConfigLoader(
584580
config_dict=self.TEST_KUBE_CONFIG,
585-
active_context="no_ssl_verification",
586-
client_configuration=actual).load_and_set()
581+
active_context="no_ssl_verification").load_and_set(actual)
587582
self.assertEqual(expected, actual)
588583

589584
def test_list_contexts(self):
@@ -631,8 +626,7 @@ def test_ssl_with_relative_ssl_files(self):
631626
KubeConfigLoader(
632627
config_dict=self.TEST_KUBE_CONFIG,
633628
active_context="ssl-local-file",
634-
config_base_path=temp_dir,
635-
client_configuration=actual).load_and_set()
629+
config_base_path=temp_dir).load_and_set(actual)
636630
self.assertEqual(expected, actual)
637631
finally:
638632
shutil.rmtree(temp_dir)
@@ -663,9 +657,9 @@ def test_new_client_from_config(self):
663657
config_file = self._create_temp_file(yaml.dump(self.TEST_KUBE_CONFIG))
664658
client = new_client_from_config(
665659
config_file=config_file, context="simple_token")
666-
self.assertEqual(TEST_HOST, client.config.host)
660+
self.assertEqual(TEST_HOST, client.configuration.host)
667661
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
668-
client.config.api_key['authorization'])
662+
client.configuration.api_key['authorization'])
669663

670664
def test_no_users_section(self):
671665
expected = FakeConfig(host=TEST_HOST)
@@ -674,17 +668,15 @@ def test_no_users_section(self):
674668
del test_kube_config['users']
675669
KubeConfigLoader(
676670
config_dict=test_kube_config,
677-
active_context="gcp",
678-
client_configuration=actual).load_and_set()
671+
active_context="gcp").load_and_set(actual)
679672
self.assertEqual(expected, actual)
680673

681674
def test_non_existing_user(self):
682675
expected = FakeConfig(host=TEST_HOST)
683676
actual = FakeConfig()
684677
KubeConfigLoader(
685678
config_dict=self.TEST_KUBE_CONFIG,
686-
active_context="non_existing_user",
687-
client_configuration=actual).load_and_set()
679+
active_context="non_existing_user").load_and_set(actual)
688680
self.assertEqual(expected, actual)
689681

690682

0 commit comments

Comments
 (0)