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

Skip to content

Commit cc8e38b

Browse files
committed
add examples of deployment object
1 parent 58a55d4 commit cc8e38b

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

examples/deployment_examples.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from os import path
16+
17+
import yaml
18+
19+
from kubernetes import client, config
20+
21+
DEPLOYMENT_NAME = "nginx-deployment"
22+
23+
def create_deployment_object():
24+
# Instantiate an empty deployment object
25+
deployment = client.ExtensionsV1beta1Deployment()
26+
# Fill required Deployment fields (apiVersion, kind and metadata)
27+
deployment.api_version = "extensions/v1beta1"
28+
deployment.kind = "Deployment"
29+
deployment.metadata = client.V1ObjectMeta(name=DEPLOYMENT_NAME)
30+
# Create and configurate a spec section
31+
spec = client.ExtensionsV1beta1DeploymentSpec()
32+
spec.replicas = 3
33+
spec.template = client.V1PodTemplateSpec()
34+
spec.template.metadata = client.V1ObjectMeta(labels={"app": "nginx"})
35+
spec.template.spec = client.V1PodSpec()
36+
# Configureate Pod template container
37+
container = client.V1Container()
38+
container.name = "nginx"
39+
container.image = "nginx:1.7.9"
40+
contianer.ports = [client.V1containerPort(container_port=80)]
41+
spec.template.spec.containers = [container]
42+
# Assign spec section into deployment.spec
43+
deployment.spec = spec
44+
45+
return deployment
46+
47+
def create_deployment(api_instance, deployment):
48+
# Create deployement
49+
api_response = api_instance.create_namespaced_deployment(
50+
body=deployment,
51+
namespace="default")
52+
print("Deployment created. status='%s'" % str(api_response.status))
53+
54+
def update_deployment(api_instance, deployment):
55+
# Update container image
56+
deployment.container.image = "nginx:1.9.1"
57+
# Update the deployment
58+
api_response = api_instance.replace_namespaced_deployment(
59+
name=DEPLOYMENT_NAME,
60+
namespace="default",
61+
body=deployment)
62+
print("Deployment updated. status='%s'" % str(api_response.status))
63+
64+
def roll_back_deployment(api_instance):
65+
# Instanciate an empty DeploymentRollback object
66+
rollback = client.ExtensionsV1beta1DeploymentRollback()
67+
# Fill required DeploymentRollback fields
68+
rollback.api_version = "extensions/v1beta1"
69+
rollback.kind = "DeploymentRollback"
70+
rollback.name = DEPLOYMENT_NAME
71+
# Configurate the rollback
72+
rollback.rollback_to = client.ExtensionsV1beta1RollbackConfig()
73+
rollback.rollback_to.revision = 0
74+
# Execute the rollback
75+
api_response = api_instance.create_namespaced_deployment_rollback(
76+
name=DEPLOYMENT_NAME,
77+
namespace="default",
78+
body=rollback)
79+
print("Deployment rolled back. status='%s'" % str(api_response.status))
80+
81+
def delete_deployment(api_instance):
82+
# Delete deployment
83+
api_response = api_instance.delete_namespaced_deployment(
84+
name=DEPLOYMENT_NAME,
85+
namespace="default",
86+
client.V1DeleteOptions(propagation_policy='Foreground',
87+
grace_period_seconds=5))
88+
print("Deployment deleted. status='%s'" % str(api_response.status))
89+
90+
91+
def main():
92+
# Configs can be set in Configuration class directly or using helper
93+
# utility. If no argument provided, the config will be loaded from
94+
# default location.
95+
config.load_kube_config()
96+
extensions_v1beta1 = client.ExtensionsV1beta1Api()
97+
# Create a deployment object with client-python API. The deployment we
98+
# created is same as the `nginx-deployment.yaml` in the /examples folder.
99+
deployment = create_deployment_object()
100+
101+
create_deployment(extensions_v1beta1, deployment)
102+
103+
update_deployment(extensions_v1beta1, deployment)
104+
105+
roll_back_deployment(extensions_v1beta1)
106+
107+
delete_deployment(extensions_v1beta1)
108+
109+
if __name__ == '__main__':
110+
main()

0 commit comments

Comments
 (0)