Skip to content

Commit

Permalink
Allow create from string or from dict
Browse files Browse the repository at this point in the history
This is a fix for #722
  • Loading branch information
oz123 committed Mar 26, 2019
1 parent 0af22b1 commit b6d7c25
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions kubernetes/utils/create_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.


import io
import re

from os import path

import yaml
Expand All @@ -30,7 +31,7 @@ def create_from_yaml(
Perform an action from a yaml file. Pass True for verbose to
print confirmation information.
Input:
yaml_file: string. Contains the path to yaml file.
yaml_file: string. Contains yaml or a path to yaml file.
k8s_client: an ApiClient object, initialized with the client args.
verbose: If True, print confirmation from the create action.
Default is False.
Expand All @@ -54,35 +55,46 @@ def create_from_yaml(
processing of the request.
Valid values are: - All: all dry run stages will be processed
"""
if path.exists(yaml_file):
with open(path.abspath(yaml_file)) as f:
yaml_file = f.read()
else:
yaml_file = io.StringIO(yaml_file)

yml_document_all = yaml.safe_load_all(io.StringIO(yaml_file))
# Load all documents from a single YAML file
for yml_document in yml_document_all:
create_from_map(k8s_client, yml_document, verbose=verbose,
**kwargs)


def create_from_map(k8s_client, yml_document, verbose=False, **kwargs):
# If it is a list type, will need to iterate its items
api_exceptions = []

if "List" in yml_document["kind"]:
# Could be "List" or "Pod/Service/...List"
# This is a list type. iterate within its items
kind = yml_document["kind"].replace("List", "")
for yml_object in yml_document["items"]:
# Mitigate cases when server returns a xxxList object
# See kubernetes-client/python#586
if kind is not "":
yml_object["apiVersion"] = yml_document["apiVersion"]
yml_object["kind"] = kind
try:
create_from_yaml_single_item(
k8s_client, yml_object, verbose, **kwargs)
except client.rest.ApiException as api_exception:
api_exceptions.append(api_exception)
else:
# This is a single object. Call the single item method
try:
create_from_yaml_single_item(
k8s_client, yml_document, verbose, **kwargs)
except client.rest.ApiException as api_exception:
api_exceptions.append(api_exception)

with open(path.abspath(yaml_file)) as f:
yml_document_all = yaml.safe_load_all(f)
api_exceptions = []
# Load all documents from a single YAML file
for yml_document in yml_document_all:
# If it is a list type, will need to iterate its items
if "List" in yml_document["kind"]:
# Could be "List" or "Pod/Service/...List"
# This is a list type. iterate within its items
kind = yml_document["kind"].replace("List", "")
for yml_object in yml_document["items"]:
# Mitigate cases when server returns a xxxList object
# See kubernetes-client/python#586
if kind is not "":
yml_object["apiVersion"] = yml_document["apiVersion"]
yml_object["kind"] = kind
try:
create_from_yaml_single_item(
k8s_client, yml_object, verbose, **kwargs)
except client.rest.ApiException as api_exception:
api_exceptions.append(api_exception)
else:
# This is a single object. Call the single item method
try:
create_from_yaml_single_item(
k8s_client, yml_document, verbose, **kwargs)
except client.rest.ApiException as api_exception:
api_exceptions.append(api_exception)
# In case we have exceptions waiting for us, raise them
if api_exceptions:
raise FailToCreateError(api_exceptions)
Expand Down

0 comments on commit b6d7c25

Please sign in to comment.