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