Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2fed56a

Browse files
Oz Tiramoz123
Oz Tiram
authored andcommittedMar 20, 2019
Change create_from_yaml to accept string or file
This keeps the function backwords compatible. Changed the parameter yaml to yml in order not to mask the imported module.
1 parent 93e8902 commit 2fed56a

File tree

3 files changed

+19
-46
lines changed

3 files changed

+19
-46
lines changed
 

‎kubernetes/e2e_test/test_utils.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def setUpClass(cls):
2626

2727
def test_app_yaml(self):
2828
k8s_client = client.api_client.ApiClient(configuration=self.config)
29-
k8s_api = utils.create_from_file(
29+
k8s_api = utils.create_from_yaml(
3030
k8s_client,
3131
"kubernetes/e2e_test/test_yaml/apps-deployment.yaml")
3232
self.assertEqual(
@@ -42,7 +42,7 @@ def test_app_yaml(self):
4242

4343
def test_extension_yaml(self):
4444
k8s_client = client.api_client.ApiClient(configuration=self.config)
45-
k8s_api = utils.create_from_file(
45+
k8s_api = utils.create_from_yaml(
4646
k8s_client,
4747
"kubernetes/e2e_test/test_yaml/extensions-deployment.yaml")
4848
self.assertEqual(
@@ -58,7 +58,7 @@ def test_extension_yaml(self):
5858

5959
def test_core_pod_yaml(self):
6060
k8s_client = client.api_client.ApiClient(configuration=self.config)
61-
k8s_api = utils.create_from_file(
61+
k8s_api = utils.create_from_yaml(
6262
k8s_client,
6363
"kubernetes/e2e_test/test_yaml/core-pod.yaml")
6464
self.assertEqual(
@@ -73,7 +73,7 @@ def test_core_pod_yaml(self):
7373

7474
def test_core_service_yaml(self):
7575
k8s_client = client.api_client.ApiClient(configuration=self.config)
76-
k8s_api = utils.create_from_file(
76+
k8s_api = utils.create_from_yaml(
7777
k8s_client,
7878
"kubernetes/e2e_test/test_yaml/core-service.yaml")
7979
self.assertEqual("v1", k8s_api.get_api_resources().group_version)
@@ -87,7 +87,7 @@ def test_core_service_yaml(self):
8787

8888
def test_core_namespace_yaml(self):
8989
k8s_client = client.api_client.ApiClient(configuration=self.config)
90-
k8s_api = utils.create_from_file(
90+
k8s_api = utils.create_from_yaml(
9191
k8s_client,
9292
"kubernetes/e2e_test/test_yaml/core-namespace.yaml")
9393
self.assertEqual("v1", k8s_api.get_api_resources().group_version)
@@ -97,13 +97,13 @@ def test_core_namespace_yaml(self):
9797

9898
def test_deployment_in_namespace(self):
9999
k8s_client = client.ApiClient(configuration=self.config)
100-
core_api = utils.create_from_file(
100+
core_api = utils.create_from_yaml(
101101
k8s_client,
102102
"kubernetes/e2e_test/test_yaml/core-namespace-dep.yaml")
103103
self.assertEqual("v1", core_api.get_api_resources().group_version)
104104
nmsp = core_api.read_namespace(name="dep")
105105
self.assertIsNotNone(nmsp)
106-
dep_api = utils.create_from_file(
106+
dep_api = utils.create_from_yaml(
107107
k8s_client,
108108
"kubernetes/e2e_test/test_yaml/extensions-deployment-dep.yaml")
109109
dep = dep_api.read_namespaced_deployment(
@@ -117,7 +117,7 @@ def test_deployment_in_namespace(self):
117117

118118
def test_api_service(self):
119119
k8s_client = client.api_client.ApiClient(configuration=self.config)
120-
k8s_api = utils.create_from_file(
120+
k8s_api = utils.create_from_yaml(
121121
k8s_client,
122122
"kubernetes/e2e_test/test_yaml/api-service.yaml")
123123
self.assertEqual(

‎kubernetes/utils/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from .create_from_yaml import (create_from_yaml,
16-
create_from_file,
17-
create_from_map)
15+
from .create_from_yaml import create_from_map, create_from_yaml

‎kubernetes/utils/create_from_yaml.py

+10-35
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
from kubernetes import client
2222

2323

24-
def create_from_file(k8s_client, yaml_path, verbose=False, **kwargs):
24+
def create_from_yaml(k8s_client, yml, verbose=False, **kwargs):
2525
"""
26-
Perform an action from a yaml file.
26+
Perform an action from a yaml string or file.
2727
2828
:param k8s_client: an ApiClient object, initialized with the client args
29-
:yaml_path: the path on the file system to the yaml file
30-
:type yaml_path: str
29+
:yml: a string containing valid yaml or a file name.
30+
:type yml: str
3131
:param verbose: print confimation information
3232
:type verbose: bool
3333
@@ -43,36 +43,11 @@ def create_from_file(k8s_client, yaml_path, verbose=False, **kwargs):
4343
an error response and no further processing of the request.
4444
Valid values are: - All: all dry run stages will be processed
4545
"""
46-
with open(path.abspath(yaml_path)) as f:
47-
k8s_api = create_from_yaml(k8s_client, f.read(), verbose=verbose,
48-
**kwargs)
49-
50-
return k8s_api
51-
52-
53-
def create_from_yaml(k8s_client, yaml_str, verbose=False, **kwargs):
54-
"""
55-
Perform an action from a yaml string.
56-
57-
:param k8s_client: an ApiClient object, initialized with the client args
58-
:yaml_str: a string containing valid yaml or json.
59-
:type yaml: str
60-
:param verbose: print confimation information
61-
:type verbose: bool
62-
63-
Available parameters for performing the subsequent action:
64-
:param async_req:
65-
:type async_req: bool
66-
:param include_uninitialized: include partially initialized resources in
67-
the response.
68-
:type include_uninitialized: bool
69-
:param str pretty: pretty print the output
70-
:param str dry_run: When present, indicates that modifications should not
71-
be persisted. An invalid or unrecognized dry_run directive will result in
72-
an error response and no further processing of the request.
73-
Valid values are: - All: all dry run stages will be processed
74-
"""
75-
stream = yaml.safe_load_all(yaml_str)
46+
if path.exists(yml):
47+
with open(path.abspath(yml)) as f:
48+
stream = yaml.safe_load_all(f.read())
49+
else:
50+
stream = yaml.safe_load_all(yml)
7651
for obj in stream:
7752
k8s_api = create_from_map(k8s_client, obj, verbose, **kwargs)
7853

@@ -123,7 +98,7 @@ def create_from_map(k8s_client, yml_object, verbose, **kwargs):
12398
# Expect the user to create namespaced objects more often
12499
if hasattr(k8s_api, "create_namespaced_{0}".format(kind)):
125100
resp = getattr(k8s_api, "create_namespaced_{0}".format(kind))(
126-
body=yml_object, namespace=namespace, **kwargs)
101+
body=yml_object, namespace=namespace, **kwargs)
127102
else:
128103
resp = getattr(k8s_api, "create_{0}".format(kind))(
129104
body=yml_object, **kwargs)

0 commit comments

Comments
 (0)