Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c77a8ba

Browse files
update deployment_crud.py to include a deployment rolling restart method for typed client
1 parent 251a1b3 commit c77a8ba

File tree

1 file changed

+77
-20
lines changed

1 file changed

+77
-20
lines changed

examples/deployment_crud.py

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
# limitations under the License.
1414

1515
"""
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
1720
"""
1821

1922
from kubernetes import client, config
23+
import datetime
24+
import pytz
2025

2126
DEPLOYMENT_NAME = "nginx-deployment"
2227

@@ -29,45 +34,94 @@ def create_deployment_object():
2934
ports=[client.V1ContainerPort(container_port=80)],
3035
resources=client.V1ResourceRequirements(
3136
requests={"cpu": "100m", "memory": "200Mi"},
32-
limits={"cpu": "500m", "memory": "500Mi"}
33-
)
37+
limits={"cpu": "500m", "memory": "500Mi"},
38+
),
3439
)
3540
# Create and configurate a spec section
3641
template = client.V1PodTemplateSpec(
3742
metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
38-
spec=client.V1PodSpec(containers=[container]))
43+
spec=client.V1PodSpec(containers=[container]),
44+
)
3945
# Create the specification of deployment
4046
spec = client.V1DeploymentSpec(
41-
replicas=3,
42-
template=template,
43-
selector={'matchLabels': {'app': 'nginx'}})
47+
replicas=3, template=template, selector={"matchLabels": {"app": "nginx"}}
48+
)
4449
# Instantiate the deployment object
4550
deployment = client.V1Deployment(
4651
api_version="apps/v1",
4752
kind="Deployment",
4853
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
49-
spec=spec)
54+
spec=spec,
55+
)
5056

5157
return deployment
5258

5359

5460
def create_deployment(api_instance, deployment):
5561
# Create deployement
5662
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
6078

6179

6280
def update_deployment(api_instance, deployment):
6381
# Update container image
6482
deployment.spec.template.spec.containers[0].image = "nginx:1.16.0"
65-
# Update the deployment
83+
84+
# patch the deployment
6685
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+
)
71125

72126

73127
def delete_deployment(api_instance):
@@ -76,9 +130,10 @@ def delete_deployment(api_instance):
76130
name=DEPLOYMENT_NAME,
77131
namespace="default",
78132
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.")
82137

83138

84139
def main():
@@ -101,8 +156,10 @@ def main():
101156

102157
update_deployment(apps_v1, deployment)
103158

159+
rolling_restart_deployment(apps_v1, deployment)
160+
104161
delete_deployment(apps_v1)
105162

106163

107-
if __name__ == '__main__':
164+
if __name__ == "__main__":
108165
main()

0 commit comments

Comments
 (0)