Skip to content

Commit 1f45d15

Browse files
committed
Add method to dynamically set namespace in yaml files
1 parent 374233d commit 1f45d15

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

kubernetes/utils/create_from_yaml.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
from kubernetes import client
2424

2525

26-
def create_from_yaml(k8s_client, yaml_file, verbose=False, **kwargs):
26+
def create_namespaced_from_yaml(k8s_client, yaml_file, verbose=False, namespace=None, **kwargs):
2727
"""
2828
Perform an action from a yaml file. Pass True for verbose to
2929
print confirmation information.
3030
Input:
3131
yaml_file: string. Contains the path to yaml file.
3232
k8s_cline: an ApiClient object, initialized with the client args.
33+
namespace: string. Defining the namespace where the new objects will be created
3334
3435
Available parameters for performing the subsequent action:
3536
:param async_req bool
@@ -55,12 +56,18 @@ def create_from_yaml(k8s_client, yaml_file, verbose=False, **kwargs):
5556
kind = yml_object["kind"]
5657
kind = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', kind)
5758
kind = re.sub('([a-z0-9])([A-Z])', r'\1_\2', kind).lower()
58-
# Decide which namespace we are going to put the object in,
59-
# if any
60-
if "namespace" in yml_object["metadata"]:
61-
namespace = yml_object["metadata"]["namespace"]
59+
60+
# If a namespace if defined overwrite it
61+
if namespace is not None:
62+
yml_object["metadata"]["namespace"] = namespace
6263
else:
63-
namespace = "default"
64+
# Decide which namespace we are going to put the object in,
65+
# if any
66+
if "namespace" in yml_object["metadata"]:
67+
namespace = yml_object["metadata"]["namespace"]
68+
else:
69+
namespace = "default"
70+
6471
# Expect the user to create namespaced objects more often
6572
if hasattr(k8s_api, "create_namespaced_{0}".format(kind)):
6673
resp = getattr(k8s_api, "create_namespaced_{0}".format(kind))(
@@ -71,3 +78,20 @@ def create_from_yaml(k8s_client, yaml_file, verbose=False, **kwargs):
7178
if verbose:
7279
print("{0} created. status='{1}'".format(kind, str(resp.status)))
7380
return k8s_api
81+
82+
83+
def create_from_yaml(k8s_client, yaml_file, verbose=False, **kwargs):
84+
"""
85+
Perform an action from a yaml file. Pass True for verbose to
86+
print confirmation information.
87+
Input:
88+
yaml_file: string. Contains the path to yaml file.
89+
k8s_cline: an ApiClient object, initialized with the client args.
90+
91+
Available parameters for performing the subsequent action:
92+
:param async_req bool
93+
:param bool include_uninitialized: If true, partially initialized resources are included in the response.
94+
:param str pretty: If 'true', then the output is pretty printed.
95+
:param str dry_run: When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun 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
96+
"""
97+
return create_namespaced_from_yaml(k8s_client, yaml_file, verbose, None, **kwargs)

0 commit comments

Comments
 (0)