Skip to content

Commit 9e40421

Browse files
committed
create_from_yaml function deals with files only
Adding the ability to deal with strings containing yaml seems to repel to much. So we stay with create_from_yaml with a bad name. This removes the need fro StringIO to wrap strings. Also note: ``` with open('foo.txt') as f: y = yaml.safe_load_all(f) for i in y: print(i) \# raises ValueError: I/O operation on closed file. ``` Hence, we indent the whole method body into the open block. with open('foo.txt') as f: y = yaml.safe_load_all(f) for i in y: print(i)
1 parent ed67d89 commit 9e40421

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

kubernetes/utils/create_from_yaml.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@
1414

1515

1616
import re
17-
import sys
1817
from os import path
1918

2019
import yaml
2120

2221
from kubernetes import client
2322

24-
if sys.version_info.major < 3:
25-
from StringIO import StringIO # noqa: F406
26-
else:
27-
from io import StringIO # noqa: F406
28-
2923

3024
def create_from_yaml(
3125
k8s_client,
@@ -47,14 +41,6 @@ def create_from_yaml(
4741
the yaml file already contains a namespace definition
4842
this parameter has no effect.
4943
50-
Returns:
51-
An k8s api object or list of apis objects created from YAML.
52-
When a single object is generated, return type is dependent
53-
on output_list.
54-
55-
Throws a FailToCreateError exception if creation of any object
56-
fails with helpful messages from the server.
57-
5844
Available parameters for creating <kind>:
5945
:param async_req bool
6046
:param bool include_uninitialized: If true, partially initialized
@@ -65,19 +51,24 @@ def create_from_yaml(
6551
directive will result in an error response and no further
6652
processing of the request.
6753
Valid values are: - All: all dry run stages will be processed
54+
55+
Raises:
56+
FailToCreateError which holds list of `client.rest.ApiException`
57+
instances for each object that failed to create.
6858
"""
6959
with open(path.abspath(yaml_file)) as f:
70-
content = f.read()
71-
try:
72-
yaml_file = StringIO(content)
73-
except TypeError:
74-
yaml_file = StringIO(content.decode('utf-8'))
60+
yml_document_all = yaml.safe_load_all(f)
7561

76-
yml_document_all = yaml.safe_load_all(yaml_file)
62+
failures = []
7763

78-
for yml_document in yml_document_all:
79-
create_from_dict(k8s_client, yml_document, verbose,
80-
**kwargs)
64+
for yml_document in yml_document_all:
65+
try:
66+
create_from_dict(k8s_client, yml_document, verbose,
67+
**kwargs)
68+
except FailToCreateError as failure:
69+
failures.extend(failure.api_exceptions)
70+
if failures:
71+
raise FailToCreateError(failures)
8172

8273

8374
def create_from_dict(k8s_client, data, verbose=False, **kwargs):

0 commit comments

Comments
 (0)