Skip to content

Commit 9e534d0

Browse files
committed
Returns the created k8s objects in create_from_{dict,yaml}.
Signed-off-by: Tao He <[email protected]>
1 parent f41b95b commit 9e534d0

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

kubernetes/utils/create_from_yaml.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def create_from_yaml(
5252
processing of the request.
5353
Valid values are: - All: all dry run stages will be processed
5454
55+
Returns:
56+
The created kubernetes API objects.
57+
5558
Raises:
5659
FailToCreateError which holds list of `client.rest.ApiException`
5760
instances for each object that failed to create.
@@ -60,16 +63,20 @@ def create_from_yaml(
6063
yml_document_all = yaml.safe_load_all(f)
6164

6265
failures = []
66+
k8s_objects = []
6367
for yml_document in yml_document_all:
6468
try:
65-
create_from_dict(k8s_client, yml_document, verbose,
66-
namespace=namespace,
67-
**kwargs)
69+
created = create_from_dict(k8s_client, yml_document, verbose,
70+
namespace=namespace,
71+
**kwargs)
72+
k8s_objects.append(created)
6873
except FailToCreateError as failure:
6974
failures.extend(failure.api_exceptions)
7075
if failures:
7176
raise FailToCreateError(failures)
7277

78+
return k8s_objects
79+
7380

7481
def create_from_dict(k8s_client, data, verbose=False, namespace='default',
7582
**kwargs):
@@ -88,12 +95,16 @@ def create_from_dict(k8s_client, data, verbose=False, namespace='default',
8895
the yaml file already contains a namespace definition
8996
this parameter has no effect.
9097
98+
Returns:
99+
The created kubernetes API objects.
100+
91101
Raises:
92102
FailToCreateError which holds list of `client.rest.ApiException`
93103
instances for each object that failed to create.
94104
"""
95105
# If it is a list type, will need to iterate its items
96106
api_exceptions = []
107+
k8s_objects = []
97108

98109
if "List" in data["kind"]:
99110
# Could be "List" or "Pod/Service/...List"
@@ -106,23 +117,26 @@ def create_from_dict(k8s_client, data, verbose=False, namespace='default',
106117
yml_object["apiVersion"] = data["apiVersion"]
107118
yml_object["kind"] = kind
108119
try:
109-
create_from_yaml_single_item(
120+
created = create_from_yaml_single_item(
110121
k8s_client, yml_object, verbose, namespace=namespace,
111122
**kwargs)
123+
k8s_objects.append(created)
112124
except client.rest.ApiException as api_exception:
113125
api_exceptions.append(api_exception)
114126
else:
115127
# This is a single object. Call the single item method
116128
try:
117-
create_from_yaml_single_item(
129+
created = create_from_yaml_single_item(
118130
k8s_client, data, verbose, namespace=namespace, **kwargs)
131+
k8s_objects.append(created)
119132
except client.rest.ApiException as api_exception:
120133
api_exceptions.append(api_exception)
121134

122135
# In case we have exceptions waiting for us, raise them
123136
if api_exceptions:
124137
raise FailToCreateError(api_exceptions)
125138

139+
return k8s_objects
126140

127141
def create_from_yaml_single_item(
128142
k8s_client, yml_object, verbose=False, **kwargs):
@@ -160,6 +174,7 @@ def create_from_yaml_single_item(
160174
if hasattr(resp, 'status'):
161175
msg += " status='{0}'".format(str(resp.status))
162176
print(msg)
177+
return resp
163178

164179

165180
class FailToCreateError(Exception):

0 commit comments

Comments
 (0)