@@ -688,31 +688,43 @@ def save_config(self, path):
688
688
yaml .safe_dump (self .config_files [path ], f ,
689
689
default_flow_style = False )
690
690
691
-
692
691
def _get_kube_config_loader_for_yaml_file (
693
692
filename , persist_config = False , ** kwargs ):
694
-
695
- kcfg = KubeConfigMerger (filename )
696
- if persist_config and 'config_persister' not in kwargs :
697
- kwargs ['config_persister' ] = kcfg .save_changes
698
-
699
- if kcfg .config is None :
700
- raise ConfigException (
701
- 'Invalid kube-config file. '
702
- 'No configuration found.' )
703
-
704
- return KubeConfigLoader (
705
- config_dict = kcfg .config ,
706
- config_base_path = None ,
693
+ return _get_kube_config_loader (
694
+ filename = filename ,
695
+ persist_config = persist_config ,
707
696
** kwargs )
708
697
698
+ def _get_kube_config_loader (
699
+ filename = None ,
700
+ config_dict = None ,
701
+ persist_config = False ,
702
+ ** kwargs ):
703
+ if config_dict is None :
704
+ kcfg = KubeConfigMerger (filename )
705
+ if persist_config and 'config_persister' not in kwargs :
706
+ kwargs ['config_persister' ] = kcfg .save_changes
707
+
708
+ if kcfg .config is None :
709
+ raise ConfigException (
710
+ 'Invalid kube-config file. '
711
+ 'No configuration found.' )
712
+ return KubeConfigLoader (
713
+ config_dict = kcfg .config ,
714
+ config_base_path = None ,
715
+ ** kwargs )
716
+ else :
717
+ return KubeConfigLoader (
718
+ config_dict = config_dict ,
719
+ config_base_path = None ,
720
+ ** kwargs )
709
721
710
722
def list_kube_config_contexts (config_file = None ):
711
723
712
724
if config_file is None :
713
725
config_file = KUBE_CONFIG_DEFAULT_LOCATION
714
726
715
- loader = _get_kube_config_loader_for_yaml_file ( config_file )
727
+ loader = _get_kube_config_loader ( filename = config_file )
716
728
return loader .list_contexts (), loader .current_context
717
729
718
730
@@ -734,8 +746,8 @@ def load_kube_config(config_file=None, context=None,
734
746
if config_file is None :
735
747
config_file = KUBE_CONFIG_DEFAULT_LOCATION
736
748
737
- loader = _get_kube_config_loader_for_yaml_file (
738
- config_file , active_context = context ,
749
+ loader = _get_kube_config_loader (
750
+ filename = config_file , active_context = context ,
739
751
persist_config = persist_config )
740
752
741
753
if client_configuration is None :
@@ -745,6 +757,36 @@ def load_kube_config(config_file=None, context=None,
745
757
else :
746
758
loader .load_and_set (client_configuration )
747
759
760
+ def load_kube_config_from_dict (config_dict , context = None ,
761
+ client_configuration = None ,
762
+ persist_config = True ):
763
+ """Loads authentication and cluster information from config_dict file
764
+ and stores them in kubernetes.client.configuration.
765
+
766
+ :param config_dict: Takes the config file as a dict.
767
+ :param context: set the active context. If is set to None, current_context
768
+ from config file will be used.
769
+ :param client_configuration: The kubernetes.client.Configuration to
770
+ set configs to.
771
+ :param persist_config: If True, config file will be updated when changed
772
+ (e.g GCP token refresh).
773
+ """
774
+
775
+ if config_dict is None :
776
+ raise ConfigException (
777
+ 'Invalid kube-config dict. '
778
+ 'No configuration found.' )
779
+
780
+ loader = _get_kube_config_loader (
781
+ config_dict = config_dict , active_context = context ,
782
+ persist_config = persist_config )
783
+
784
+ if client_configuration is None :
785
+ config = type .__call__ (Configuration )
786
+ loader .load_and_set (config )
787
+ Configuration .set_default (config )
788
+ else :
789
+ loader .load_and_set (client_configuration )
748
790
749
791
def new_client_from_config (
750
792
config_file = None ,
0 commit comments