diff --git a/examples/create_from_yaml.py b/examples/create_deployment_from_yaml.py similarity index 82% rename from examples/create_from_yaml.py rename to examples/create_deployment_from_yaml.py index 1fbccd9ff2..052886d737 100644 --- a/examples/create_from_yaml.py +++ b/examples/create_deployment_from_yaml.py @@ -16,7 +16,7 @@ import yaml -from kubernetes import utils, config +from kubernetes import client, config, utils def main(): @@ -24,8 +24,7 @@ def main(): # utility. If no argument provided, the config will be loaded from # default location. config.load_kube_config() - k8s_client, resp = utils.create_deployment_from_yaml("./nginx-deployment.yaml") - print("Deployment created. status='%s'" % str(resp.status)) + k8s_api = utils.create_from_yaml("nginx-deployment.yaml") if __name__ == '__main__': diff --git a/kubernetes/e2e_test/test_utils.py b/kubernetes/e2e_test/test_utils.py new file mode 100644 index 0000000000..402ef6241e --- /dev/null +++ b/kubernetes/e2e_test/test_utils.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import unittest + +from kubernetes import utils, client +from kubernetes.e2e_test import base + +class TestUtils(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls.config = base.get_e2e_configuration() + + def test_app_yaml(self): + k8s_api = utils.create_from_yaml( + "kubernetes/e2e_test/test_yaml/app.yaml", + configuration=self.config) + self.assertEqual("apps/v1beta1", + k8s_api.get_api_resources().group_version) + deployments = k8s_api.list_namespaced_deployment( + namespace="default").items + deployment_name = [] + for item in deployments: + deployment_name.append(item.metadata.name) + self.assertIn("nginx-app", deployment_name) + resp = k8s_api.delete_namespaced_deployment( + name="nginx-app", namespace="default", + body={}) + + def test_extension_yaml(self): + k8s_api = utils.create_from_yaml( + "kubernetes/e2e_test/test_yaml/extension.yaml", + configuration=self.config) + self.assertEqual("extensions/v1beta1", + k8s_api.get_api_resources().group_version) + deployments = k8s_api.list_namespaced_deployment( + namespace="default").items + deployment_name = [] + for item in deployments: + deployment_name.append(item.metadata.name) + self.assertIn("nginx-deployment", deployment_name) + resp = k8s_api.delete_namespaced_deployment( + name="nginx-deployment", namespace="default", + body={}) + + def test_core_pod_yaml(self): + k8s_api = utils.create_from_yaml( + "kubernetes/e2e_test/test_yaml/core-pod.yaml", + configuration=self.config) + self.assertEqual("v1", + k8s_api.get_api_resources().group_version) + pods = k8s_api.list_namespaced_pod(namespace="default").items + pod_name = [] + for item in pods: + pod_name.append(item.metadata.name) + self.assertIn("myapp-pod", pod_name) + resp = k8s_api.delete_namespaced_pod( + name="myapp-pod", namespace="default", + body={}) + + def test_core_service_yaml(self): + k8s_api = utils.create_from_yaml( + "kubernetes/e2e_test/test_yaml/core-service.yaml", + configuration=self.config) + self.assertEqual("v1", + k8s_api.get_api_resources().group_version) + svcs = k8s_api.list_namespaced_service(namespace="default").items + svc_name = [] + for item in svcs: + svc_name.append(item.metadata.name) + self.assertIn("my-service", svc_name) + resp = k8s_api.delete_namespaced_service( + name="my-service", namespace="default", + body={}) diff --git a/kubernetes/e2e_test/test_yaml/app.yaml b/kubernetes/e2e_test/test_yaml/app.yaml new file mode 100644 index 0000000000..a2ffa6b996 --- /dev/null +++ b/kubernetes/e2e_test/test_yaml/app.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: nginx-app + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.15.4 + ports: + - containerPort: 80 diff --git a/kubernetes/e2e_test/test_yaml/core-pod.yaml b/kubernetes/e2e_test/test_yaml/core-pod.yaml new file mode 100644 index 0000000000..8276a37fb5 --- /dev/null +++ b/kubernetes/e2e_test/test_yaml/core-pod.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod +metadata: + name: myapp-pod + labels: + app: myapp +spec: + containers: + - name: myapp-container + image: busybox + command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600'] \ No newline at end of file diff --git a/kubernetes/e2e_test/test_yaml/core-service.yaml b/kubernetes/e2e_test/test_yaml/core-service.yaml new file mode 100644 index 0000000000..a805c9116c --- /dev/null +++ b/kubernetes/e2e_test/test_yaml/core-service.yaml @@ -0,0 +1,11 @@ +kind: Service +apiVersion: v1 +metadata: + name: my-service +spec: + selector: + app: MyApp + ports: + - protocol: TCP + port: 80 + targetPort: 9376 \ No newline at end of file diff --git a/kubernetes/e2e_test/test_yaml/extension.yaml b/kubernetes/e2e_test/test_yaml/extension.yaml new file mode 100644 index 0000000000..d05940d29b --- /dev/null +++ b/kubernetes/e2e_test/test_yaml/extension.yaml @@ -0,0 +1,17 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: nginx-deployment +spec: + replicas: 3 + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.7.9 + ports: + - containerPort: 80 + diff --git a/kubernetes/utils/__init__.py b/kubernetes/utils/__init__.py index b1a7aa47ee..2310e7dd75 100644 --- a/kubernetes/utils/__init__.py +++ b/kubernetes/utils/__init__.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .create_deployment_from_yaml import create_deployment_from_yaml \ No newline at end of file +from .create_from_yaml import create_from_yaml diff --git a/kubernetes/utils/create_deployment_from_yaml.py b/kubernetes/utils/create_deployment_from_yaml.py deleted file mode 100644 index ae58824241..0000000000 --- a/kubernetes/utils/create_deployment_from_yaml.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright 2016 The Kubernetes Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from os import path -import sys - -import yaml - -from kubernetes import client - -def create_deployment_from_yaml(yaml_file): - with open(path.abspath(yaml_file)) as f: - dep = yaml.load(f) - api_type, _, api_version = dep["apiVersion"].partition("/") - api_type = api_type.capitalize() - api_version = api_version.capitalize() - k8s_client = getattr(client, "%s%sApi" % (api_type, api_version))() - resp = k8s_client.create_namespaced_deployment(body=dep, namespace="default") - return k8s_client, resp - - - - - diff --git a/kubernetes/utils/create_from_yaml.py b/kubernetes/utils/create_from_yaml.py new file mode 100644 index 0000000000..0e609c4b6f --- /dev/null +++ b/kubernetes/utils/create_from_yaml.py @@ -0,0 +1,74 @@ +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from os import path +import sys + +from six import iteritems + +import yaml + +from kubernetes import client + +def create_from_yaml(yaml_file, verbose=0, **kwargs): + """ + Perform an action from a yaml file. Pass 1 for verbose to + print confirmation information. + + Available parameters for generating the client: + :param configuration: Configuration for the client. + :param host: The base path for the server to call. + :param header_name: a header to pass when making calls to the API. + :param header_value: a header value to pass when making calls to the API. + + Available parameters for performing the subsequent action: + :param async_req bool + :param bool include_uninitialized: If true, partially initialized resources are included in the response. + :param str pretty: If 'true', then the output is pretty printed. + :param str dry_run: When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed + """ + + client_params = ["configuration", "host", "header_name", "header_value"] + action_params = ["async_req", "include_unitialized", "pretty", "dry_run", + '_return_http_data_only', '_preload_content', '_request_timeout'] + client_args = {} + action_args = {} + params = locals() + for key, val in iteritems(params["kwargs"]): + if key in client_params: + client_args[key] = val + elif key in action_params: + action_args[key] = val + + k8s_client = client.api_client.ApiClient(**client_args) + with open(path.abspath(yaml_file)) as f: + dep = yaml.load(f) + api_type, _, api_version = dep["apiVersion"].partition("/") + if api_version == "": + api_version = api_type + api_type = "core" + fcn_to_call = "{0}{1}Api".format(api_type.capitalize(), + api_version.capitalize()) + k8s_api = getattr(client, fcn_to_call)(k8s_client) + action_type = dep["kind"] + if "namespace" in dep["metadata"]: + dep_namespace = dep["metadata"]["namespace"] + else: + dep_namespace = "default" + resp = getattr(k8s_api, "create_namespaced_{0}".format(action_type.lower()))( + body=dep, namespace=dep_namespace, **action_args) + if verbose: + print("{0} created. status='{1}'".format(action_type, str(resp.status))) + return k8s_api + \ No newline at end of file