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
This commit is contained in:
Kubernetes Prow Robot 2021-07-19 12:50:52 -07:00 committed by GitHub
commit 05e8f5798a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 5 deletions

View File

@ -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 @@ class TestUtils(unittest.TestCase):
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 @@ class TestUtils(unittest.TestCase):
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:

View File

@ -26,7 +26,8 @@ LOWER_OR_NUM_FOLLOWED_BY_UPPER_RE = re.compile('([a-z0-9])([A-Z])')
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):