Skip to content

Commit

Permalink
Merge pull request #782 from johscheuer/create-from-yaml-namespaced
Browse files Browse the repository at this point in the history
Add method to dynamically set namespace in yaml files
  • Loading branch information
k8s-ci-robot committed Jun 18, 2019
2 parents 05c1a43 + 80339ef commit c86e489
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
51 changes: 51 additions & 0 deletions kubernetes/e2e_test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ class TestUtils(unittest.TestCase):
def setUpClass(cls):
cls.config = base.get_e2e_configuration()
cls.path_prefix = "kubernetes/e2e_test/test_yaml/"
cls.test_namespace = "e2e-test-utils"
k8s_client = client.api_client.ApiClient(configuration=cls.config)
core_v1 = client.CoreV1Api(api_client=k8s_client)
body = client.V1Namespace(metadata=client.V1ObjectMeta(name=cls.test_namespace))
core_v1.create_namespace(body=body)

@classmethod
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 Down Expand Up @@ -331,3 +341,44 @@ def test_create_from_multi_resource_yaml_with_multi_conflicts(self):
ext_api.delete_namespaced_deployment(
name="triple-nginx", namespace="default",
body={})

def test_create_namespaces_apps_deployment_from_yaml(self):
"""
Should be able to create an apps/v1beta1 deployment.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
utils.create_from_yaml(
k8s_client, self.path_prefix + "apps-deployment.yaml", namespace=self.test_namespace)
app_api = client.AppsV1beta1Api(k8s_client)
dep = app_api.read_namespaced_deployment(name="nginx-app",
namespace=self.test_namespace)
self.assertIsNotNone(dep)
app_api.delete_namespaced_deployment(
name="nginx-app", namespace=self.test_namespace,
body={})

def test_create_from_list_in_multi_resource_yaml(self):
"""
Should be able to create the items in the PodList and a deployment
specified in the multi-resource file
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
utils.create_from_yaml(
k8s_client, self.path_prefix + "multi-resource-with-list.yaml", namespace=self.test_namespace)
core_api = client.CoreV1Api(k8s_client)
app_api = client.AppsV1beta1Api(k8s_client)
pod_0 = core_api.read_namespaced_pod(
name="mock-pod-0", namespace=self.test_namespace)
self.assertIsNotNone(pod_0)
pod_1 = core_api.read_namespaced_pod(
name="mock-pod-1", namespace=self.test_namespace)
self.assertIsNotNone(pod_1)
dep = app_api.read_namespaced_deployment(
name="mock", namespace=self.test_namespace)
self.assertIsNotNone(dep)
core_api.delete_namespaced_pod(
name="mock-pod-0", namespace=self.test_namespace, body={})
core_api.delete_namespaced_pod(
name="mock-pod-1", namespace=self.test_namespace, body={})
app_api.delete_namespaced_deployment(
name="mock", namespace=self.test_namespace, body={})
18 changes: 13 additions & 5 deletions kubernetes/utils/create_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def create_from_yaml(
k8s_client,
yaml_file,
verbose=False,
namespace="default",
**kwargs):
"""
Perform an action from a yaml file. Pass True for verbose to
Expand All @@ -34,6 +35,11 @@ def create_from_yaml(
k8s_client: an ApiClient object, initialized with the client args.
verbose: If True, print confirmation from the create action.
Default is False.
namespace: string. Contains the namespace to create all
resources inside. The namespace must preexist otherwise
the resource creation will fail. If the API object in
the yaml file already contains a namespace definition
this parameter has no effect.
Returns:
An k8s api object or list of apis objects created from YAML.
Expand Down Expand Up @@ -73,14 +79,14 @@ def create_from_yaml(
yml_object["kind"] = kind
try:
create_from_yaml_single_item(
k8s_client, yml_object, verbose, **kwargs)
k8s_client, yml_object, verbose, namespace, **kwargs)
except client.rest.ApiException as api_exception:
api_exceptions.append(api_exception)
else:
# This is a single object. Call the single item method
try:
create_from_yaml_single_item(
k8s_client, yml_document, verbose, **kwargs)
k8s_client, yml_document, verbose, namespace, **kwargs)
except client.rest.ApiException as api_exception:
api_exceptions.append(api_exception)
# In case we have exceptions waiting for us, raise them
Expand All @@ -89,7 +95,11 @@ def create_from_yaml(


def create_from_yaml_single_item(
k8s_client, yml_object, verbose=False, **kwargs):
k8s_client,
yml_object,
verbose=False,
namespace="default",
**kwargs):
group, _, version = yml_object["apiVersion"].partition("/")
if version == "":
version = group
Expand All @@ -110,8 +120,6 @@ def create_from_yaml_single_item(
# if any
if "namespace" in yml_object["metadata"]:
namespace = yml_object["metadata"]["namespace"]
else:
namespace = "default"
# Expect the user to create namespaced objects more often
if hasattr(k8s_api, "create_namespaced_{0}".format(kind)):
resp = getattr(k8s_api, "create_namespaced_{0}".format(kind))(
Expand Down

0 comments on commit c86e489

Please sign in to comment.