13
13
# limitations under the License.
14
14
15
15
"""
16
- Creates, updates, and deletes a deployment using AppsV1Api.
16
+ The example covers the following:
17
+ - Creation of a deployment using AppsV1Api
18
+ - update/patch to perform rolling restart on the deployment
19
+ - deletetion of the deployment
17
20
"""
18
21
22
+ import datetime
23
+
24
+ import pytz
25
+
19
26
from kubernetes import client , config
20
27
21
28
DEPLOYMENT_NAME = "nginx-deployment"
@@ -29,56 +36,110 @@ def create_deployment_object():
29
36
ports = [client .V1ContainerPort (container_port = 80 )],
30
37
resources = client .V1ResourceRequirements (
31
38
requests = {"cpu" : "100m" , "memory" : "200Mi" },
32
- limits = {"cpu" : "500m" , "memory" : "500Mi" }
33
- )
39
+ limits = {"cpu" : "500m" , "memory" : "500Mi" },
40
+ ),
34
41
)
42
+
35
43
# Create and configurate a spec section
36
44
template = client .V1PodTemplateSpec (
37
45
metadata = client .V1ObjectMeta (labels = {"app" : "nginx" }),
38
- spec = client .V1PodSpec (containers = [container ]))
46
+ spec = client .V1PodSpec (containers = [container ]),
47
+ )
48
+
39
49
# Create the specification of deployment
40
50
spec = client .V1DeploymentSpec (
41
- replicas = 3 ,
42
- template = template ,
43
- selector = {'matchLabels' : {'app' : 'nginx' }})
51
+ replicas = 3 , template = template , selector = {
52
+ "matchLabels" :
53
+ {"app" : "nginx" }})
54
+
44
55
# Instantiate the deployment object
45
56
deployment = client .V1Deployment (
46
57
api_version = "apps/v1" ,
47
58
kind = "Deployment" ,
48
59
metadata = client .V1ObjectMeta (name = DEPLOYMENT_NAME ),
49
- spec = spec )
60
+ spec = spec ,
61
+ )
50
62
51
63
return deployment
52
64
53
65
54
- def create_deployment (api_instance , deployment ):
66
+ def create_deployment (api , deployment ):
55
67
# Create deployement
56
- api_response = api_instance .create_namespaced_deployment (
57
- body = deployment ,
58
- namespace = "default" )
59
- print ("Deployment created. status='%s'" % str (api_response .status ))
68
+ resp = api .create_namespaced_deployment (
69
+ body = deployment , namespace = "default"
70
+ )
60
71
72
+ print ("\n [INFO] deployment `nginx-deployment` created.\n " )
73
+ print ("%s\t %s\t \t \t %s\t %s" % ("NAMESPACE" , "NAME" , "REVISION" , "IMAGE" ))
74
+ print (
75
+ "%s\t \t %s\t %s\t \t %s\n "
76
+ % (
77
+ resp .metadata .namespace ,
78
+ resp .metadata .name ,
79
+ resp .metadata .generation ,
80
+ resp .spec .template .spec .containers [0 ].image ,
81
+ )
82
+ )
61
83
62
- def update_deployment (api_instance , deployment ):
84
+
85
+ def update_deployment (api , deployment ):
63
86
# Update container image
64
87
deployment .spec .template .spec .containers [0 ].image = "nginx:1.16.0"
65
- # Update the deployment
66
- api_response = api_instance .patch_namespaced_deployment (
67
- name = DEPLOYMENT_NAME ,
68
- namespace = "default" ,
69
- body = deployment )
70
- print ("Deployment updated. status='%s'" % str (api_response .status ))
71
88
89
+ # patch the deployment
90
+ resp = api .patch_namespaced_deployment (
91
+ name = DEPLOYMENT_NAME , namespace = "default" , body = deployment
92
+ )
93
+
94
+ print ("\n [INFO] deployment's container image updated.\n " )
95
+ print ("%s\t %s\t \t \t %s\t %s" % ("NAMESPACE" , "NAME" , "REVISION" , "IMAGE" ))
96
+ print (
97
+ "%s\t \t %s\t %s\t \t %s\n "
98
+ % (
99
+ resp .metadata .namespace ,
100
+ resp .metadata .name ,
101
+ resp .metadata .generation ,
102
+ resp .spec .template .spec .containers [0 ].image ,
103
+ )
104
+ )
105
+
106
+
107
+ def restart_deployment (api , deployment ):
108
+ # update `spec.template.metadata` section
109
+ # to add `kubectl.kubernetes.io/restartedAt` annotation
110
+ deployment .spec .template .metadata .annotations = {
111
+ "kubectl.kubernetes.io/restartedAt" : datetime .datetime .utcnow ()
112
+ .replace (tzinfo = pytz .UTC )
113
+ .isoformat ()
114
+ }
115
+
116
+ # patch the deployment
117
+ resp = api .patch_namespaced_deployment (
118
+ name = DEPLOYMENT_NAME , namespace = "default" , body = deployment
119
+ )
120
+
121
+ print ("\n [INFO] deployment `nginx-deployment` restarted.\n " )
122
+ print ("%s\t \t \t %s\t %s" % ("NAME" , "REVISION" , "RESTARTED-AT" ))
123
+ print (
124
+ "%s\t %s\t \t %s\n "
125
+ % (
126
+ resp .metadata .name ,
127
+ resp .metadata .generation ,
128
+ resp .spec .template .metadata .annotations ,
129
+ )
130
+ )
72
131
73
- def delete_deployment (api_instance ):
132
+
133
+ def delete_deployment (api ):
74
134
# Delete deployment
75
- api_response = api_instance .delete_namespaced_deployment (
135
+ resp = api .delete_namespaced_deployment (
76
136
name = DEPLOYMENT_NAME ,
77
137
namespace = "default" ,
78
138
body = client .V1DeleteOptions (
79
- propagation_policy = 'Foreground' ,
80
- grace_period_seconds = 5 ))
81
- print ("Deployment deleted. status='%s'" % str (api_response .status ))
139
+ propagation_policy = "Foreground" , grace_period_seconds = 5
140
+ ),
141
+ )
142
+ print ("\n [INFO] deployment `nginx-deployment` deleted." )
82
143
83
144
84
145
def main ():
@@ -101,8 +162,10 @@ def main():
101
162
102
163
update_deployment (apps_v1 , deployment )
103
164
165
+ restart_deployment (apps_v1 , deployment )
166
+
104
167
delete_deployment (apps_v1 )
105
168
106
169
107
- if __name__ == ' __main__' :
170
+ if __name__ == " __main__" :
108
171
main ()
0 commit comments