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