2626
2727def create_from_yaml (
2828 k8s_client ,
29- yaml_file ,
29+ yaml_file = None ,
30+ yaml_objects = None ,
3031 verbose = False ,
3132 namespace = "default" ,
3233 ** kwargs ):
@@ -36,6 +37,8 @@ def create_from_yaml(
3637 Input:
3738 yaml_file: string. Contains the path to yaml file.
3839 k8s_client: an ApiClient object, initialized with the client args.
40+ yaml_objects: List[dict]. Optional list of YAML objects; used instead
41+ of reading the `yaml_file`. Default is None.
3942 verbose: If True, print confirmation from the create action.
4043 Default is False.
4144 namespace: string. Contains the namespace to create all
@@ -62,12 +65,11 @@ def create_from_yaml(
6265 FailToCreateError which holds list of `client.rest.ApiException`
6366 instances for each object that failed to create.
6467 """
65- with open (path .abspath (yaml_file )) as f :
66- yml_document_all = yaml .safe_load_all (f )
6768
69+ def create_with (objects ):
6870 failures = []
6971 k8s_objects = []
70- for yml_document in yml_document_all :
72+ for yml_document in objects :
7173 if yml_document is None :
7274 continue
7375 try :
@@ -79,9 +81,19 @@ def create_from_yaml(
7981 failures .extend (failure .api_exceptions )
8082 if failures :
8183 raise FailToCreateError (failures )
82-
8384 return k8s_objects
8485
86+ if yaml_objects :
87+ yml_document_all = yaml_objects
88+ return create_with (yml_document_all )
89+ elif yaml_file :
90+ with open (path .abspath (yaml_file )) as f :
91+ yml_document_all = yaml .safe_load_all (f )
92+ return create_with (yml_document_all )
93+ else :
94+ raise ValueError (
95+ 'One of `yaml_file` or `yaml_objects` arguments must be provided' )
96+
8597
8698def create_from_dict (k8s_client , data , verbose = False , namespace = 'default' ,
8799 ** kwargs ):
0 commit comments