Skip to content

Commit 6100392

Browse files
committed
Add optional namespace to create_from_dict
This follows up on the addition to create_from_yaml, the behavior is the same.
1 parent ab002f7 commit 6100392

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

kubernetes/utils/create_from_yaml.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,19 @@ def create_from_yaml(
6060
yml_document_all = yaml.safe_load_all(f)
6161

6262
failures = []
63-
6463
for yml_document in yml_document_all:
6564
try:
6665
create_from_dict(k8s_client, yml_document, verbose,
66+
namespace=namespace,
6767
**kwargs)
6868
except FailToCreateError as failure:
6969
failures.extend(failure.api_exceptions)
7070
if failures:
7171
raise FailToCreateError(failures)
7272

7373

74-
def create_from_dict(k8s_client, data, verbose=False, **kwargs):
74+
def create_from_dict(k8s_client, data, verbose=False, namespace='default',
75+
**kwargs):
7576
"""
7677
Perform an action from a dictionary containing valid kubernetes
7778
API object (i.e. List, Service, etc).
@@ -81,6 +82,11 @@ def create_from_dict(k8s_client, data, verbose=False, **kwargs):
8182
data: a dictionary holding valid kubernetes objects
8283
verbose: If True, print confirmation from the create action.
8384
Default is False.
85+
namespace: string. Contains the namespace to create all
86+
resources inside. The namespace must preexist otherwise
87+
the resource creation will fail. If the API object in
88+
the yaml file already contains a namespace definition
89+
this parameter has no effect.
8490
8591
Raises:
8692
FailToCreateError which holds list of `client.rest.ApiException`
@@ -101,14 +107,15 @@ def create_from_dict(k8s_client, data, verbose=False, **kwargs):
101107
yml_object["kind"] = kind
102108
try:
103109
create_from_yaml_single_item(
104-
k8s_client, yml_object, verbose, **kwargs)
110+
k8s_client, yml_object, verbose, namespace=namespace,
111+
**kwargs)
105112
except client.rest.ApiException as api_exception:
106113
api_exceptions.append(api_exception)
107114
else:
108115
# This is a single object. Call the single item method
109116
try:
110117
create_from_yaml_single_item(
111-
k8s_client, data, verbose, **kwargs)
118+
k8s_client, data, verbose, namespace=namespace, **kwargs)
112119
except client.rest.ApiException as api_exception:
113120
api_exceptions.append(api_exception)
114121

@@ -135,17 +142,17 @@ def create_from_yaml_single_item(
135142
kind = yml_object["kind"]
136143
kind = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', kind)
137144
kind = re.sub('([a-z0-9])([A-Z])', r'\1_\2', kind).lower()
138-
# Decide which namespace we are going to put the object in,
139-
# if any
140-
if "namespace" in yml_object["metadata"]:
141-
namespace = yml_object["metadata"]["namespace"]
142-
else:
143-
namespace = "default"
144145
# Expect the user to create namespaced objects more often
145146
if hasattr(k8s_api, "create_namespaced_{0}".format(kind)):
147+
# Decide which namespace we are going to put the object in,
148+
# if any
149+
if "namespace" in yml_object["metadata"]:
150+
namespace = yml_object["metadata"]["namespace"]
151+
kwargs['namespace'] = namespace
146152
resp = getattr(k8s_api, "create_namespaced_{0}".format(kind))(
147-
body=yml_object, namespace=namespace, **kwargs)
153+
body=yml_object, **kwargs)
148154
else:
155+
kwargs.pop('namespace', None)
149156
resp = getattr(k8s_api, "create_{0}".format(kind))(
150157
body=yml_object, **kwargs)
151158
if verbose:

0 commit comments

Comments
 (0)