diff --git a/kubernetes/e2e_test/test_utils.py b/kubernetes/e2e_test/test_utils.py index ab752dff79..05a056c5f0 100644 --- a/kubernetes/e2e_test/test_utils.py +++ b/kubernetes/e2e_test/test_utils.py @@ -13,8 +13,10 @@ # under the License. import unittest +from os import path import yaml + from kubernetes import utils, client from kubernetes.client.rest import ApiException from kubernetes.e2e_test import base @@ -37,6 +39,7 @@ def tearDownClass(cls): k8s_client = client.api_client.ApiClient(configuration=cls.config) core_v1 = client.CoreV1Api(api_client=k8s_client) core_v1.delete_namespace(name=cls.test_namespace) + # Tests for creating individual API objects def test_create_apps_deployment_from_yaml(self): @@ -59,6 +62,31 @@ def test_create_apps_deployment_from_yaml(self): except ApiException: continue + def test_create_apps_deployment_from_yaml_object(self): + """ + Should be able to pass YAM objects directly to helper function. + """ + k8s_client = client.api_client.ApiClient(configuration=self.config) + _path = self.path_prefix + "apps-deployment.yaml" + with open(path.abspath(_path)) as f: + yaml_objects = yaml.safe_load_all(f) + utils.create_from_yaml( + k8s_client, + yaml_objects=yaml_objects, + ) + app_api = client.AppsV1Api(k8s_client) + dep = app_api.read_namespaced_deployment(name="nginx-app", + namespace="default") + self.assertIsNotNone(dep) + while True: + try: + app_api.delete_namespaced_deployment( + name="nginx-app", namespace="default", + body={}) + break + except ApiException: + continue + def test_create_apps_deployment_from_yaml_obj(self): k8s_client = client.api_client.ApiClient(configuration=self.config) with open(self.path_prefix + "apps-deployment.yaml") as f: diff --git a/kubernetes/utils/create_from_yaml.py b/kubernetes/utils/create_from_yaml.py index 0fb265a1df..3a996a13d4 100644 --- a/kubernetes/utils/create_from_yaml.py +++ b/kubernetes/utils/create_from_yaml.py @@ -26,7 +26,8 @@ def create_from_yaml( k8s_client, - yaml_file, + yaml_file=None, + yaml_objects=None, verbose=False, namespace="default", **kwargs): @@ -36,6 +37,8 @@ def create_from_yaml( Input: yaml_file: string. Contains the path to yaml file. k8s_client: an ApiClient object, initialized with the client args. + yaml_objects: List[dict]. Optional list of YAML objects; used instead + of reading the `yaml_file`. Default is None. verbose: If True, print confirmation from the create action. Default is False. namespace: string. Contains the namespace to create all @@ -62,12 +65,11 @@ def create_from_yaml( FailToCreateError which holds list of `client.rest.ApiException` instances for each object that failed to create. """ - with open(path.abspath(yaml_file)) as f: - yml_document_all = yaml.safe_load_all(f) + def create_with(objects): failures = [] k8s_objects = [] - for yml_document in yml_document_all: + for yml_document in objects: if yml_document is None: continue try: @@ -79,9 +81,19 @@ def create_from_yaml( failures.extend(failure.api_exceptions) if failures: raise FailToCreateError(failures) - return k8s_objects + if yaml_objects: + yml_document_all = yaml_objects + return create_with(yml_document_all) + elif yaml_file: + with open(path.abspath(yaml_file)) as f: + yml_document_all = yaml.safe_load_all(f) + return create_with(yml_document_all) + else: + raise ValueError( + 'One of `yaml_file` or `yaml_objects` arguments must be provided') + def create_from_dict(k8s_client, data, verbose=False, namespace='default', **kwargs):