From 9e40421bcc685d8d65b900dfeba0fec26c3cfea8 Mon Sep 17 00:00:00 2001 From: Oz N Tiram Date: Thu, 20 Jun 2019 22:48:35 +0200 Subject: [PATCH] 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) --- kubernetes/utils/create_from_yaml.py | 37 +++++++++++----------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/kubernetes/utils/create_from_yaml.py b/kubernetes/utils/create_from_yaml.py index b5101174c..435ebdd38 100644 --- a/kubernetes/utils/create_from_yaml.py +++ b/kubernetes/utils/create_from_yaml.py @@ -14,18 +14,12 @@ import re -import sys from os import path import yaml from kubernetes import client -if sys.version_info.major < 3: - from StringIO import StringIO # noqa: F406 -else: - from io import StringIO # noqa: F406 - def create_from_yaml( k8s_client, @@ -47,14 +41,6 @@ def create_from_yaml( the yaml file already contains a namespace definition this parameter has no effect. - Returns: - An k8s api object or list of apis objects created from YAML. - When a single object is generated, return type is dependent - on output_list. - - Throws a FailToCreateError exception if creation of any object - fails with helpful messages from the server. - Available parameters for creating : :param async_req bool :param bool include_uninitialized: If true, partially initialized @@ -65,19 +51,24 @@ def create_from_yaml( directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed + + Raises: + FailToCreateError which holds list of `client.rest.ApiException` + instances for each object that failed to create. """ with open(path.abspath(yaml_file)) as f: - content = f.read() - try: - yaml_file = StringIO(content) - except TypeError: - yaml_file = StringIO(content.decode('utf-8')) + yml_document_all = yaml.safe_load_all(f) - yml_document_all = yaml.safe_load_all(yaml_file) + failures = [] - for yml_document in yml_document_all: - create_from_dict(k8s_client, yml_document, verbose, - **kwargs) + for yml_document in yml_document_all: + try: + create_from_dict(k8s_client, yml_document, verbose, + **kwargs) + except FailToCreateError as failure: + failures.extend(failure.api_exceptions) + if failures: + raise FailToCreateError(failures) def create_from_dict(k8s_client, data, verbose=False, **kwargs):