Skip to content

Commit 05e8f57

Browse files
authored
Merge pull request #1403 from mecampbellsoup/add-yaml-objects-param-to-create-from-yaml-util
Allow optional list of YAML objects as param to create_from_yaml util
2 parents 6c4d917 + 13814c0 commit 05e8f57

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

kubernetes/e2e_test/test_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
# under the License.
1414

1515
import unittest
16+
from os import path
1617

1718
import yaml
19+
1820
from kubernetes import utils, client
1921
from kubernetes.client.rest import ApiException
2022
from kubernetes.e2e_test import base
@@ -37,6 +39,7 @@ def tearDownClass(cls):
3739
k8s_client = client.api_client.ApiClient(configuration=cls.config)
3840
core_v1 = client.CoreV1Api(api_client=k8s_client)
3941
core_v1.delete_namespace(name=cls.test_namespace)
42+
4043
# Tests for creating individual API objects
4144

4245
def test_create_apps_deployment_from_yaml(self):
@@ -59,6 +62,31 @@ def test_create_apps_deployment_from_yaml(self):
5962
except ApiException:
6063
continue
6164

65+
def test_create_apps_deployment_from_yaml_object(self):
66+
"""
67+
Should be able to pass YAM objects directly to helper function.
68+
"""
69+
k8s_client = client.api_client.ApiClient(configuration=self.config)
70+
_path = self.path_prefix + "apps-deployment.yaml"
71+
with open(path.abspath(_path)) as f:
72+
yaml_objects = yaml.safe_load_all(f)
73+
utils.create_from_yaml(
74+
k8s_client,
75+
yaml_objects=yaml_objects,
76+
)
77+
app_api = client.AppsV1Api(k8s_client)
78+
dep = app_api.read_namespaced_deployment(name="nginx-app",
79+
namespace="default")
80+
self.assertIsNotNone(dep)
81+
while True:
82+
try:
83+
app_api.delete_namespaced_deployment(
84+
name="nginx-app", namespace="default",
85+
body={})
86+
break
87+
except ApiException:
88+
continue
89+
6290
def test_create_apps_deployment_from_yaml_obj(self):
6391
k8s_client = client.api_client.ApiClient(configuration=self.config)
6492
with open(self.path_prefix + "apps-deployment.yaml") as f:

kubernetes/utils/create_from_yaml.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
def create_from_yaml(
2828
k8s_client,
29-
yaml_file,
29+
yaml_file=None,
30+
yaml_objects=None,
3031
verbose=False,
3132
namespace="default",
3233
**kwargs):
@@ -36,6 +37,8 @@ def create_from_yaml(
3637
Input:
3738
yaml_file: string. Contains the path to yaml file.
3839
k8s_client: an ApiClient object, initialized with the client args.
40+
yaml_objects: List[dict]. Optional list of YAML objects; used instead
41+
of reading the `yaml_file`. Default is None.
3942
verbose: If True, print confirmation from the create action.
4043
Default is False.
4144
namespace: string. Contains the namespace to create all
@@ -62,12 +65,11 @@ def create_from_yaml(
6265
FailToCreateError which holds list of `client.rest.ApiException`
6366
instances for each object that failed to create.
6467
"""
65-
with open(path.abspath(yaml_file)) as f:
66-
yml_document_all = yaml.safe_load_all(f)
6768

69+
def create_with(objects):
6870
failures = []
6971
k8s_objects = []
70-
for yml_document in yml_document_all:
72+
for yml_document in objects:
7173
if yml_document is None:
7274
continue
7375
try:
@@ -79,9 +81,19 @@ def create_from_yaml(
7981
failures.extend(failure.api_exceptions)
8082
if failures:
8183
raise FailToCreateError(failures)
82-
8384
return k8s_objects
8485

86+
if yaml_objects:
87+
yml_document_all = yaml_objects
88+
return create_with(yml_document_all)
89+
elif yaml_file:
90+
with open(path.abspath(yaml_file)) as f:
91+
yml_document_all = yaml.safe_load_all(f)
92+
return create_with(yml_document_all)
93+
else:
94+
raise ValueError(
95+
'One of `yaml_file` or `yaml_objects` arguments must be provided')
96+
8597

8698
def create_from_dict(k8s_client, data, verbose=False, namespace='default',
8799
**kwargs):

0 commit comments

Comments
 (0)