From 9e5a5b1a7a44a61e84b94aaafb54657c0cfac78d Mon Sep 17 00:00:00 2001 From: micw523 Date: Fri, 26 Oct 2018 20:10:28 -0500 Subject: [PATCH] Squashed commits: create from yaml --- examples/create_deployment_from_yaml.py | 31 +++++++ kubernetes/__init__.py | 1 + kubernetes/e2e_test/test_utils.py | 86 +++++++++++++++++++ kubernetes/e2e_test/test_yaml/app.yaml | 21 +++++ kubernetes/e2e_test/test_yaml/core-pod.yaml | 11 +++ .../e2e_test/test_yaml/core-service.yaml | 11 +++ kubernetes/e2e_test/test_yaml/extension.yaml | 17 ++++ kubernetes/utils/__init__.py | 15 ++++ kubernetes/utils/create_from_yaml.py | 74 ++++++++++++++++ setup.py | 3 +- 10 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 examples/create_deployment_from_yaml.py create mode 100644 kubernetes/e2e_test/test_utils.py create mode 100644 kubernetes/e2e_test/test_yaml/app.yaml create mode 100644 kubernetes/e2e_test/test_yaml/core-pod.yaml create mode 100644 kubernetes/e2e_test/test_yaml/core-service.yaml create mode 100644 kubernetes/e2e_test/test_yaml/extension.yaml create mode 100644 kubernetes/utils/__init__.py create mode 100644 kubernetes/utils/create_from_yaml.py diff --git a/examples/create_deployment_from_yaml.py b/examples/create_deployment_from_yaml.py new file mode 100644 index 0000000000..052886d737 --- /dev/null +++ b/examples/create_deployment_from_yaml.py @@ -0,0 +1,31 @@ +# 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 yaml + +from kubernetes import client, config, utils + + +def main(): + # Configs can be set in Configuration class directly or using helper + # utility. If no argument provided, the config will be loaded from + # default location. + config.load_kube_config() + k8s_api = utils.create_from_yaml("nginx-deployment.yaml") + + +if __name__ == '__main__': + main() diff --git a/kubernetes/__init__.py b/kubernetes/__init__.py index 4dbe66f567..33c19ac878 100644 --- a/kubernetes/__init__.py +++ b/kubernetes/__init__.py @@ -20,3 +20,4 @@ import kubernetes.config import kubernetes.watch import kubernetes.stream +import kubernetes.utils 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 new file mode 100644 index 0000000000..2310e7dd75 --- /dev/null +++ b/kubernetes/utils/__init__.py @@ -0,0 +1,15 @@ +# 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 .create_from_yaml import create_from_yaml 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 diff --git a/setup.py b/setup.py index 9624448903..85ad8d17a9 100644 --- a/setup.py +++ b/setup.py @@ -57,7 +57,8 @@ extras_require=EXTRAS, packages=['kubernetes', 'kubernetes.client', 'kubernetes.config', 'kubernetes.watch', 'kubernetes.client.apis', - 'kubernetes.stream', 'kubernetes.client.models'], + 'kubernetes.stream', 'kubernetes.client.models', + 'kubernetes.utils'], include_package_data=True, long_description="""\ Python client for kubernetes http://kubernetes.io/