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,56 +34,108 @@ 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 = {
48
+ "matchLabels" :
49
+ { " app" : " nginx" }})
44
50
# Instantiate the deployment object
45
51
deployment = client .V1Deployment (
46
52
api_version = "apps/v1" ,
47
53
kind = "Deployment" ,
48
54
metadata = client .V1ObjectMeta (name = DEPLOYMENT_NAME ),
49
- spec = spec )
55
+ spec = spec ,
56
+ )
50
57
51
58
return deployment
52
59
53
60
54
- def create_deployment (api_instance , deployment ):
61
+ def create_deployment (api , deployment ):
55
62
# 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 ))
63
+ resp = api .create_namespaced_deployment (
64
+ body = deployment , namespace = "default"
65
+ )
60
66
67
+ print ("\n [INFO] deployment `nginx-deployment` created.\n " )
68
+ print ("%s\t %s\t \t \t %s\t %s" % ("NAMESPACE" , "NAME" , "REVISION" , "IMAGE" ))
69
+ print (
70
+ "%s\t \t %s\t %s\t \t %s\n "
71
+ % (
72
+ resp .metadata .namespace ,
73
+ resp .metadata .name ,
74
+ resp .metadata .generation ,
75
+ resp .spec .template .spec .containers [0 ].image ,
76
+ )
77
+ )
78
+ return resp
61
79
62
- def update_deployment (api_instance , deployment ):
80
+
81
+ def update_deployment (api , deployment ):
63
82
# Update container image
64
83
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
84
85
+ # patch the deployment
86
+ resp = api .patch_namespaced_deployment (
87
+ name = DEPLOYMENT_NAME , namespace = "default" , body = deployment
88
+ )
89
+
90
+ print ("\n [INFO] deployment's container image updated.\n " )
91
+ print ("%s\t %s\t \t \t %s\t %s" % ("NAMESPACE" , "NAME" , "REVISION" , "IMAGE" ))
92
+ print (
93
+ "%s\t \t %s\t %s\t \t %s\n "
94
+ % (
95
+ resp .metadata .namespace ,
96
+ resp .metadata .name ,
97
+ resp .metadata .generation ,
98
+ resp .spec .template .spec .containers [0 ].image ,
99
+ )
100
+ )
101
+
102
+
103
+ def rolling_restart_deployment (api , deployment ):
104
+ # update `spec.template.metadata` section
105
+ # to add `kubectl.kubernetes.io/restartedAt` annotation
106
+ deployment .spec .template .metadata .annotations = {
107
+ "kubectl.kubernetes.io/restartedAt" : datetime .datetime .utcnow ()
108
+ .replace (tzinfo = pytz .UTC )
109
+ .isoformat ()
110
+ } # noqa
111
+
112
+ # patch the deployment
113
+ resp = api .patch_namespaced_deployment (
114
+ name = DEPLOYMENT_NAME , namespace = "default" , body = deployment
115
+ )
72
116
73
- def delete_deployment (api_instance ):
117
+ print ("\n [INFO] deployment `nginx-deployment` restarted.\n " )
118
+ print ("%s\t \t \t %s\t %s" % ("NAME" , "REVISION" , "RESTARTED-AT" ))
119
+ print (
120
+ "%s\t %s\t \t %s\n "
121
+ % (
122
+ resp .metadata .name ,
123
+ resp .metadata .generation ,
124
+ resp .spec .template .metadata .annotations ,
125
+ )
126
+ ) # noqa
127
+
128
+
129
+ def delete_deployment (api ):
74
130
# Delete deployment
75
- api_response = api_instance .delete_namespaced_deployment (
131
+ resp = api .delete_namespaced_deployment (
76
132
name = DEPLOYMENT_NAME ,
77
133
namespace = "default" ,
78
134
body = client .V1DeleteOptions (
79
- propagation_policy = 'Foreground' ,
80
- grace_period_seconds = 5 ))
81
- print ("Deployment deleted. status='%s'" % str (api_response .status ))
135
+ propagation_policy = "Foreground" , grace_period_seconds = 5
136
+ ),
137
+ )
138
+ print ("\n [INFO] deployment `nginx-deployment` deleted." )
82
139
83
140
84
141
def main ():
@@ -101,8 +158,10 @@ def main():
101
158
102
159
update_deployment (apps_v1 , deployment )
103
160
161
+ rolling_restart_deployment (apps_v1 , deployment )
162
+
104
163
delete_deployment (apps_v1 )
105
164
106
165
107
- if __name__ == ' __main__' :
166
+ if __name__ == " __main__" :
108
167
main ()
0 commit comments