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