12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
-
15
+ import io
16
16
import re
17
+
17
18
from os import path
18
19
19
20
import yaml
@@ -30,7 +31,7 @@ def create_from_yaml(
30
31
Perform an action from a yaml file. Pass True for verbose to
31
32
print confirmation information.
32
33
Input:
33
- yaml_file: string. Contains the path to yaml file.
34
+ yaml_file: string. Contains yaml or a path to yaml file.
34
35
k8s_client: an ApiClient object, initialized with the client args.
35
36
verbose: If True, print confirmation from the create action.
36
37
Default is False.
@@ -54,35 +55,46 @@ def create_from_yaml(
54
55
processing of the request.
55
56
Valid values are: - All: all dry run stages will be processed
56
57
"""
58
+ if path .exists (yaml_file ):
59
+ with open (path .abspath (yaml_file )) as f :
60
+ yaml_file = f .read ()
61
+ else :
62
+ yaml_file = io .StringIO (yaml_file )
63
+
64
+ yml_document_all = yaml .safe_load_all (io .StringIO (yaml_file ))
65
+ # Load all documents from a single YAML file
66
+ for yml_document in yml_document_all :
67
+ create_from_map (k8s_client , yml_document , verbose = verbose ,
68
+ ** kwargs )
69
+
70
+
71
+ def create_from_map (k8s_client , yml_document , verbose = False , ** kwargs ):
72
+ # If it is a list type, will need to iterate its items
73
+ api_exceptions = []
74
+
75
+ if "List" in yml_document ["kind" ]:
76
+ # Could be "List" or "Pod/Service/...List"
77
+ # This is a list type. iterate within its items
78
+ kind = yml_document ["kind" ].replace ("List" , "" )
79
+ for yml_object in yml_document ["items" ]:
80
+ # Mitigate cases when server returns a xxxList object
81
+ # See kubernetes-client/python#586
82
+ if kind is not "" :
83
+ yml_object ["apiVersion" ] = yml_document ["apiVersion" ]
84
+ yml_object ["kind" ] = kind
85
+ try :
86
+ create_from_yaml_single_item (
87
+ k8s_client , yml_object , verbose , ** kwargs )
88
+ except client .rest .ApiException as api_exception :
89
+ api_exceptions .append (api_exception )
90
+ else :
91
+ # This is a single object. Call the single item method
92
+ try :
93
+ create_from_yaml_single_item (
94
+ k8s_client , yml_document , verbose , ** kwargs )
95
+ except client .rest .ApiException as api_exception :
96
+ api_exceptions .append (api_exception )
57
97
58
- with open (path .abspath (yaml_file )) as f :
59
- yml_document_all = yaml .safe_load_all (f )
60
- api_exceptions = []
61
- # Load all documents from a single YAML file
62
- for yml_document in yml_document_all :
63
- # If it is a list type, will need to iterate its items
64
- if "List" in yml_document ["kind" ]:
65
- # Could be "List" or "Pod/Service/...List"
66
- # This is a list type. iterate within its items
67
- kind = yml_document ["kind" ].replace ("List" , "" )
68
- for yml_object in yml_document ["items" ]:
69
- # Mitigate cases when server returns a xxxList object
70
- # See kubernetes-client/python#586
71
- if kind is not "" :
72
- yml_object ["apiVersion" ] = yml_document ["apiVersion" ]
73
- yml_object ["kind" ] = kind
74
- try :
75
- create_from_yaml_single_item (
76
- k8s_client , yml_object , verbose , ** kwargs )
77
- except client .rest .ApiException as api_exception :
78
- api_exceptions .append (api_exception )
79
- else :
80
- # This is a single object. Call the single item method
81
- try :
82
- create_from_yaml_single_item (
83
- k8s_client , yml_document , verbose , ** kwargs )
84
- except client .rest .ApiException as api_exception :
85
- api_exceptions .append (api_exception )
86
98
# In case we have exceptions waiting for us, raise them
87
99
if api_exceptions :
88
100
raise FailToCreateError (api_exceptions )
0 commit comments