Skip to content

Commit 8665a76

Browse files
authored
Merge pull request #345 from DjangoPeng/master
add examples of deployment object
2 parents 640c2aa + 695a05f commit 8665a76

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

examples/deployment_examples.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
24+
def create_deployment_object():
25+
# Configureate Pod template container
26+
container = client.V1Container(
27+
name="nginx",
28+
image="nginx:1.7.9",
29+
ports=[client.V1ContainerPort(container_port=80)])
30+
# Create and configurate a spec section
31+
template = client.V1PodTemplateSpec(
32+
metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
33+
spec=client.V1PodSpec(containers=[container]))
34+
# Create the specification of deployment
35+
spec = client.ExtensionsV1beta1DeploymentSpec(
36+
replicas=3,
37+
template=template)
38+
# Instantiate the deployment object
39+
deployment = client.ExtensionsV1beta1Deployment(
40+
api_version="extensions/v1beta1",
41+
kind="Deployment",
42+
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
43+
spec=spec)
44+
45+
return deployment
46+
47+
48+
def create_deployment(api_instance, deployment):
49+
# Create deployement
50+
api_response = api_instance.create_namespaced_deployment(
51+
body=deployment,
52+
namespace="default")
53+
print("Deployment created. status='%s'" % str(api_response.status))
54+
55+
56+
def update_deployment(api_instance, deployment):
57+
# Update container image
58+
deployment.spec.template.spec.containers[0].image = "nginx:1.9.1"
59+
# Update the deployment
60+
api_response = api_instance.patch_namespaced_deployment(
61+
name=DEPLOYMENT_NAME,
62+
namespace="default",
63+
body=deployment)
64+
print("Deployment updated. status='%s'" % str(api_response.status))
65+
66+
67+
def delete_deployment(api_instance):
68+
# Delete deployment
69+
api_response = api_instance.delete_namespaced_deployment(
70+
name=DEPLOYMENT_NAME,
71+
namespace="default",
72+
body=client.V1DeleteOptions(
73+
propagation_policy='Foreground',
74+
grace_period_seconds=5))
75+
print("Deployment deleted. status='%s'" % str(api_response.status))
76+
77+
78+
def main():
79+
# Configs can be set in Configuration class directly or using helper
80+
# utility. If no argument provided, the config will be loaded from
81+
# default location.
82+
config.load_kube_config()
83+
extensions_v1beta1 = client.ExtensionsV1beta1Api()
84+
# Create a deployment object with client-python API. The deployment we
85+
# created is same as the `nginx-deployment.yaml` in the /examples folder.
86+
deployment = create_deployment_object()
87+
88+
create_deployment(extensions_v1beta1, deployment)
89+
90+
update_deployment(extensions_v1beta1, deployment)
91+
92+
delete_deployment(extensions_v1beta1)
93+
94+
95+
if __name__ == '__main__':
96+
main()

0 commit comments

Comments
 (0)