26
26
27
27
def create_from_yaml (
28
28
k8s_client ,
29
- yaml_file ,
29
+ yaml_file = None ,
30
+ yaml_objects = None ,
30
31
verbose = False ,
31
32
namespace = "default" ,
32
33
** kwargs ):
@@ -36,6 +37,8 @@ def create_from_yaml(
36
37
Input:
37
38
yaml_file: string. Contains the path to yaml file.
38
39
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.
39
42
verbose: If True, print confirmation from the create action.
40
43
Default is False.
41
44
namespace: string. Contains the namespace to create all
@@ -62,12 +65,11 @@ def create_from_yaml(
62
65
FailToCreateError which holds list of `client.rest.ApiException`
63
66
instances for each object that failed to create.
64
67
"""
65
- with open (path .abspath (yaml_file )) as f :
66
- yml_document_all = yaml .safe_load_all (f )
67
68
69
+ def create_with (objects ):
68
70
failures = []
69
71
k8s_objects = []
70
- for yml_document in yml_document_all :
72
+ for yml_document in objects :
71
73
if yml_document is None :
72
74
continue
73
75
try :
@@ -79,9 +81,19 @@ def create_from_yaml(
79
81
failures .extend (failure .api_exceptions )
80
82
if failures :
81
83
raise FailToCreateError (failures )
82
-
83
84
return k8s_objects
84
85
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
+
85
97
86
98
def create_from_dict (k8s_client , data , verbose = False , namespace = 'default' ,
87
99
** kwargs ):
0 commit comments