Skip to content

Commit aa28bc7

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

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

kubernetes/utils/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414

1515
from __future__ import absolute_import
1616

17-
from .create_from_yaml import FailToCreateError, create_from_yaml
17+
from .create_from_yaml import (FailToCreateError, create_from_yaml,
18+
create_from_map)

kubernetes/utils/create_from_yaml.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
14+
import io
1615
import re
16+
1717
from os import path
1818

1919
import yaml
@@ -30,7 +30,7 @@ def create_from_yaml(
3030
Perform an action from a yaml file. Pass True for verbose to
3131
print confirmation information.
3232
Input:
33-
yaml_file: string. Contains the path to yaml file.
33+
yaml_file: string. Contains yaml string or a path to yaml file.
3434
k8s_client: an ApiClient object, initialized with the client args.
3535
verbose: If True, print confirmation from the create action.
3636
Default is False.
@@ -54,35 +54,44 @@ def create_from_yaml(
5454
processing of the request.
5555
Valid values are: - All: all dry run stages will be processed
5656
"""
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)
5794

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

0 commit comments

Comments
 (0)