Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow optional list of YAML objects as param to create_from_yaml util #1403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions kubernetes/e2e_test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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:
Expand Down
22 changes: 17 additions & 5 deletions kubernetes/utils/create_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

def create_from_yaml(
k8s_client,
yaml_file,
yaml_file=None,
yaml_objects=None,
verbose=False,
namespace="default",
**kwargs):
Expand All @@ -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
Expand 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:
Expand All @@ -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):
Expand Down