Skip to content

Commit b6d7c25

Browse files
committed
Allow create from string or from dict
This is a fix for #722
1 parent 0af22b1 commit b6d7c25

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

kubernetes/utils/create_from_yaml.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
15+
import io
1616
import re
17+
1718
from os import path
1819

1920
import yaml
@@ -30,7 +31,7 @@ def create_from_yaml(
3031
Perform an action from a yaml file. Pass True for verbose to
3132
print confirmation information.
3233
Input:
33-
yaml_file: string. Contains the path to yaml file.
34+
yaml_file: string. Contains yaml or a path to yaml file.
3435
k8s_client: an ApiClient object, initialized with the client args.
3536
verbose: If True, print confirmation from the create action.
3637
Default is False.
@@ -54,35 +55,46 @@ def create_from_yaml(
5455
processing of the request.
5556
Valid values are: - All: all dry run stages will be processed
5657
"""
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)
5797

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)
8698
# In case we have exceptions waiting for us, raise them
8799
if api_exceptions:
88100
raise FailToCreateError(api_exceptions)

0 commit comments

Comments
 (0)