1919
2020import urllib3
2121import yaml
22- from kubernetes .client import configuration
22+ from kubernetes .client import ApiClient , ConfigurationObject , configuration
2323from oauth2client .client import GoogleCredentials
2424
2525from .config_exception import ConfigException
@@ -97,7 +97,8 @@ def as_data(self):
9797class KubeConfigLoader (object ):
9898
9999 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 ,
101102 config_base_path = "" ):
102103 self ._config = ConfigNode ('kube-config' , config_dict )
103104 self ._current_context = None
@@ -111,10 +112,7 @@ def __init__(self, config_dict, active_context=None,
111112 self ._get_google_credentials = lambda : (
112113 GoogleCredentials .get_application_default ()
113114 .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
118116
119117 def set_active_context (self , context_name = None ):
120118 if context_name is None :
@@ -279,17 +277,31 @@ def list_kube_config_contexts(config_file=None):
279277 return loader .list_contexts (), loader .current_context
280278
281279
282- def load_kube_config (config_file = None , context = None ):
280+ def load_kube_config (config_file = None , context = None ,
281+ client_configuration = configuration ):
283282 """Loads authentication and cluster information from kube-config file
284283 and stores them in kubernetes.client.configuration.
285284
286285 :param config_file: Name of the kube-config file.
287286 :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.
289290 """
290291
291292 if config_file is None :
292293 config_file = os .path .expanduser (KUBE_CONFIG_DEFAULT_LOCATION )
293294
294295 _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