19
19
20
20
import urllib3
21
21
import yaml
22
- from kubernetes .client import configuration
22
+ from kubernetes .client import ApiClient , ConfigurationObject , configuration
23
23
from oauth2client .client import GoogleCredentials
24
24
25
25
from .config_exception import ConfigException
@@ -97,7 +97,8 @@ def as_data(self):
97
97
class KubeConfigLoader (object ):
98
98
99
99
def __init__ (self , config_dict , active_context = None ,
100
- get_google_credentials = None , client_configuration = None ,
100
+ get_google_credentials = None ,
101
+ client_configuration = configuration ,
101
102
config_base_path = "" ):
102
103
self ._config = ConfigNode ('kube-config' , config_dict )
103
104
self ._current_context = None
@@ -111,10 +112,7 @@ def __init__(self, config_dict, active_context=None,
111
112
self ._get_google_credentials = lambda : (
112
113
GoogleCredentials .get_application_default ()
113
114
.get_access_token ().access_token )
114
- if client_configuration :
115
- self ._client_configuration = client_configuration
116
- else :
117
- self ._client_configuration = configuration
115
+ self ._client_configuration = client_configuration
118
116
119
117
def set_active_context (self , context_name = None ):
120
118
if context_name is None :
@@ -279,17 +277,31 @@ def list_kube_config_contexts(config_file=None):
279
277
return loader .list_contexts (), loader .current_context
280
278
281
279
282
- def load_kube_config (config_file = None , context = None ):
280
+ def load_kube_config (config_file = None , context = None ,
281
+ client_configuration = configuration ):
283
282
"""Loads authentication and cluster information from kube-config file
284
283
and stores them in kubernetes.client.configuration.
285
284
286
285
:param config_file: Name of the kube-config file.
287
286
:param context: set the active context. If is set to None, current_context
288
- from config file will be used.
287
+ from config file will be used.
288
+ :param client_configuration: The kubernetes.client.ConfigurationObject to
289
+ set configs to.
289
290
"""
290
291
291
292
if config_file is None :
292
293
config_file = os .path .expanduser (KUBE_CONFIG_DEFAULT_LOCATION )
293
294
294
295
_get_kube_config_loader_for_yaml_file (
295
- config_file , active_context = context ).load_and_set ()
296
+ config_file , active_context = context ,
297
+ client_configuration = client_configuration ).load_and_set ()
298
+
299
+
300
+ def new_client_from_config (config_file = None , context = None ):
301
+ """Loads configuration the same as load_kube_config but returns an ApiClient
302
+ to be used with any API object. This will allow the caller to concurrently
303
+ talk with multiple clusters."""
304
+ client_config = ConfigurationObject ()
305
+ load_kube_config (config_file = config_file , context = context ,
306
+ client_configuration = client_config )
307
+ return ApiClient (config = client_config )
0 commit comments