From cc8e38b46ee79b08c76b31d173301549722693b9 Mon Sep 17 00:00:00 2001 From: DjangoPeng Date: Sun, 24 Sep 2017 11:22:13 +0800 Subject: [PATCH 1/5] add examples of deployment object --- examples/deployment_examples.py | 110 ++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 examples/deployment_examples.py diff --git a/examples/deployment_examples.py b/examples/deployment_examples.py new file mode 100644 index 000000000..344e982a6 --- /dev/null +++ b/examples/deployment_examples.py @@ -0,0 +1,110 @@ +# 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 + +DEPLOYMENT_NAME = "nginx-deployment" + +def create_deployment_object(): + # Instantiate an empty deployment object + deployment = client.ExtensionsV1beta1Deployment() + # Fill required Deployment fields (apiVersion, kind and metadata) + deployment.api_version = "extensions/v1beta1" + deployment.kind = "Deployment" + deployment.metadata = client.V1ObjectMeta(name=DEPLOYMENT_NAME) + # Create and configurate a spec section + spec = client.ExtensionsV1beta1DeploymentSpec() + spec.replicas = 3 + spec.template = client.V1PodTemplateSpec() + spec.template.metadata = client.V1ObjectMeta(labels={"app": "nginx"}) + spec.template.spec = client.V1PodSpec() + # Configureate Pod template container + container = client.V1Container() + container.name = "nginx" + container.image = "nginx:1.7.9" + contianer.ports = [client.V1containerPort(container_port=80)] + spec.template.spec.containers = [container] + # Assign spec section into deployment.spec + deployment.spec = spec + + return deployment + +def create_deployment(api_instance, deployment): + # Create deployement + api_response = api_instance.create_namespaced_deployment( + body=deployment, + namespace="default") + print("Deployment created. status='%s'" % str(api_response.status)) + +def update_deployment(api_instance, deployment): + # Update container image + deployment.container.image = "nginx:1.9.1" + # Update the deployment + api_response = api_instance.replace_namespaced_deployment( + name=DEPLOYMENT_NAME, + namespace="default", + body=deployment) + print("Deployment updated. status='%s'" % str(api_response.status)) + +def roll_back_deployment(api_instance): + # Instanciate an empty DeploymentRollback object + rollback = client.ExtensionsV1beta1DeploymentRollback() + # Fill required DeploymentRollback fields + rollback.api_version = "extensions/v1beta1" + rollback.kind = "DeploymentRollback" + rollback.name = DEPLOYMENT_NAME + # Configurate the rollback + rollback.rollback_to = client.ExtensionsV1beta1RollbackConfig() + rollback.rollback_to.revision = 0 + # Execute the rollback + api_response = api_instance.create_namespaced_deployment_rollback( + name=DEPLOYMENT_NAME, + namespace="default", + body=rollback) + print("Deployment rolled back. status='%s'" % str(api_response.status)) + +def delete_deployment(api_instance): + # Delete deployment + api_response = api_instance.delete_namespaced_deployment( + name=DEPLOYMENT_NAME, + namespace="default", + client.V1DeleteOptions(propagation_policy='Foreground', + grace_period_seconds=5)) + print("Deployment deleted. status='%s'" % str(api_response.status)) + + +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() + extensions_v1beta1 = client.ExtensionsV1beta1Api() + # Create a deployment object with client-python API. The deployment we + # created is same as the `nginx-deployment.yaml` in the /examples folder. + deployment = create_deployment_object() + + create_deployment(extensions_v1beta1, deployment) + + update_deployment(extensions_v1beta1, deployment) + + roll_back_deployment(extensions_v1beta1) + + delete_deployment(extensions_v1beta1) + +if __name__ == '__main__': + main() From c49db53c58c46f2412a4af6a2bcf101f3fecb42f Mon Sep 17 00:00:00 2001 From: DjangoPeng Date: Wed, 27 Sep 2017 06:46:35 +0800 Subject: [PATCH 2/5] update for pep8 --- examples/deployment_examples.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/examples/deployment_examples.py b/examples/deployment_examples.py index 344e982a6..3acd459d2 100644 --- a/examples/deployment_examples.py +++ b/examples/deployment_examples.py @@ -20,6 +20,7 @@ from kubernetes import client, config DEPLOYMENT_NAME = "nginx-deployment" + def create_deployment_object(): # Instantiate an empty deployment object deployment = client.ExtensionsV1beta1Deployment() @@ -44,23 +45,26 @@ def create_deployment_object(): return deployment + def create_deployment(api_instance, deployment): # Create deployement api_response = api_instance.create_namespaced_deployment( - body=deployment, - namespace="default") + body=deployment, + namespace="default") print("Deployment created. status='%s'" % str(api_response.status)) + def update_deployment(api_instance, deployment): # Update container image deployment.container.image = "nginx:1.9.1" # Update the deployment api_response = api_instance.replace_namespaced_deployment( - name=DEPLOYMENT_NAME, - namespace="default", - body=deployment) + name=DEPLOYMENT_NAME, + namespace="default", + body=deployment) print("Deployment updated. status='%s'" % str(api_response.status)) + def roll_back_deployment(api_instance): # Instanciate an empty DeploymentRollback object rollback = client.ExtensionsV1beta1DeploymentRollback() @@ -73,18 +77,19 @@ def roll_back_deployment(api_instance): rollback.rollback_to.revision = 0 # Execute the rollback api_response = api_instance.create_namespaced_deployment_rollback( - name=DEPLOYMENT_NAME, - namespace="default", - body=rollback) + name=DEPLOYMENT_NAME, + namespace="default", + body=rollback) print("Deployment rolled back. status='%s'" % str(api_response.status)) + def delete_deployment(api_instance): # Delete deployment api_response = api_instance.delete_namespaced_deployment( - name=DEPLOYMENT_NAME, - namespace="default", - client.V1DeleteOptions(propagation_policy='Foreground', - grace_period_seconds=5)) + name=DEPLOYMENT_NAME, + namespace="default", + client.V1DeleteOptions(propagation_policy='Foreground', + grace_period_seconds=5)) print("Deployment deleted. status='%s'" % str(api_response.status)) @@ -106,5 +111,6 @@ def main(): delete_deployment(extensions_v1beta1) + if __name__ == '__main__': main() From 6c7235db2225adf79bd6c9c78b5499d45788ae9e Mon Sep 17 00:00:00 2001 From: DjangoPeng Date: Wed, 27 Sep 2017 18:20:20 +0800 Subject: [PATCH 3/5] securely update deployment with patch_namespaced_deployment --- examples/deployment_examples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/deployment_examples.py b/examples/deployment_examples.py index 3acd459d2..f2b93d53f 100644 --- a/examples/deployment_examples.py +++ b/examples/deployment_examples.py @@ -58,7 +58,7 @@ def update_deployment(api_instance, deployment): # Update container image deployment.container.image = "nginx:1.9.1" # Update the deployment - api_response = api_instance.replace_namespaced_deployment( + api_response = api_instance.patch_namespaced_deployment( name=DEPLOYMENT_NAME, namespace="default", body=deployment) From 24435a5b04632e4c33cd78217eebe4fc510c4515 Mon Sep 17 00:00:00 2001 From: DjangoPeng Date: Thu, 12 Oct 2017 18:20:46 +0800 Subject: [PATCH 4/5] fix syntax error --- examples/deployment_examples.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/deployment_examples.py b/examples/deployment_examples.py index f2b93d53f..c0d459bba 100644 --- a/examples/deployment_examples.py +++ b/examples/deployment_examples.py @@ -38,7 +38,7 @@ def create_deployment_object(): container = client.V1Container() container.name = "nginx" container.image = "nginx:1.7.9" - contianer.ports = [client.V1containerPort(container_port=80)] + container.ports = [client.V1ContainerPort(container_port=80)] spec.template.spec.containers = [container] # Assign spec section into deployment.spec deployment.spec = spec @@ -56,7 +56,7 @@ def create_deployment(api_instance, deployment): def update_deployment(api_instance, deployment): # Update container image - deployment.container.image = "nginx:1.9.1" + deployment.spec.template.spec.containers[0].image = "nginx:1.9.1" # Update the deployment api_response = api_instance.patch_namespaced_deployment( name=DEPLOYMENT_NAME, @@ -80,7 +80,6 @@ def roll_back_deployment(api_instance): name=DEPLOYMENT_NAME, namespace="default", body=rollback) - print("Deployment rolled back. status='%s'" % str(api_response.status)) def delete_deployment(api_instance): @@ -88,7 +87,7 @@ def delete_deployment(api_instance): api_response = api_instance.delete_namespaced_deployment( name=DEPLOYMENT_NAME, namespace="default", - client.V1DeleteOptions(propagation_policy='Foreground', + body=client.V1DeleteOptions(propagation_policy='Foreground', grace_period_seconds=5)) print("Deployment deleted. status='%s'" % str(api_response.status)) From 695a05f6babd028c0e5e33319e66d72e2f046b13 Mon Sep 17 00:00:00 2001 From: DjangoPeng Date: Fri, 13 Oct 2017 12:23:11 +0800 Subject: [PATCH 5/5] make examples work on latest master branch --- examples/deployment_examples.py | 61 ++++++++++++--------------------- 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/examples/deployment_examples.py b/examples/deployment_examples.py index c0d459bba..29f55d34d 100644 --- a/examples/deployment_examples.py +++ b/examples/deployment_examples.py @@ -22,26 +22,25 @@ DEPLOYMENT_NAME = "nginx-deployment" def create_deployment_object(): - # Instantiate an empty deployment object - deployment = client.ExtensionsV1beta1Deployment() - # Fill required Deployment fields (apiVersion, kind and metadata) - deployment.api_version = "extensions/v1beta1" - deployment.kind = "Deployment" - deployment.metadata = client.V1ObjectMeta(name=DEPLOYMENT_NAME) - # Create and configurate a spec section - spec = client.ExtensionsV1beta1DeploymentSpec() - spec.replicas = 3 - spec.template = client.V1PodTemplateSpec() - spec.template.metadata = client.V1ObjectMeta(labels={"app": "nginx"}) - spec.template.spec = client.V1PodSpec() # Configureate Pod template container - container = client.V1Container() - container.name = "nginx" - container.image = "nginx:1.7.9" - container.ports = [client.V1ContainerPort(container_port=80)] - spec.template.spec.containers = [container] - # Assign spec section into deployment.spec - deployment.spec = spec + container = client.V1Container( + name="nginx", + image="nginx:1.7.9", + ports=[client.V1ContainerPort(container_port=80)]) + # Create and configurate a spec section + template = client.V1PodTemplateSpec( + metadata=client.V1ObjectMeta(labels={"app": "nginx"}), + spec=client.V1PodSpec(containers=[container])) + # Create the specification of deployment + spec = client.ExtensionsV1beta1DeploymentSpec( + replicas=3, + template=template) + # Instantiate the deployment object + deployment = client.ExtensionsV1beta1Deployment( + api_version="extensions/v1beta1", + kind="Deployment", + metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME), + spec=spec) return deployment @@ -65,30 +64,14 @@ def update_deployment(api_instance, deployment): print("Deployment updated. status='%s'" % str(api_response.status)) -def roll_back_deployment(api_instance): - # Instanciate an empty DeploymentRollback object - rollback = client.ExtensionsV1beta1DeploymentRollback() - # Fill required DeploymentRollback fields - rollback.api_version = "extensions/v1beta1" - rollback.kind = "DeploymentRollback" - rollback.name = DEPLOYMENT_NAME - # Configurate the rollback - rollback.rollback_to = client.ExtensionsV1beta1RollbackConfig() - rollback.rollback_to.revision = 0 - # Execute the rollback - api_response = api_instance.create_namespaced_deployment_rollback( - name=DEPLOYMENT_NAME, - namespace="default", - body=rollback) - - def delete_deployment(api_instance): # Delete deployment api_response = api_instance.delete_namespaced_deployment( name=DEPLOYMENT_NAME, namespace="default", - body=client.V1DeleteOptions(propagation_policy='Foreground', - grace_period_seconds=5)) + body=client.V1DeleteOptions( + propagation_policy='Foreground', + grace_period_seconds=5)) print("Deployment deleted. status='%s'" % str(api_response.status)) @@ -106,8 +89,6 @@ def main(): update_deployment(extensions_v1beta1, deployment) - roll_back_deployment(extensions_v1beta1) - delete_deployment(extensions_v1beta1)