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

Skip to content

Commit 15365fe

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

File tree

1 file changed

+85
-26
lines changed

1 file changed

+85
-26
lines changed

examples/deployment_crud.py

Lines changed: 85 additions & 26 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,56 +34,108 @@ 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={
48+
"matchLabels":
49+
{"app": "nginx"}})
4450
# Instantiate the deployment object
4551
deployment = client.V1Deployment(
4652
api_version="apps/v1",
4753
kind="Deployment",
4854
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
49-
spec=spec)
55+
spec=spec,
56+
)
5057

5158
return deployment
5259

5360

54-
def create_deployment(api_instance, deployment):
61+
def create_deployment(api, deployment):
5562
# 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+
)
6066

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
6179

62-
def update_deployment(api_instance, deployment):
80+
81+
def update_deployment(api, deployment):
6382
# Update container image
6483
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))
7184

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+
)
72116

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):
74130
# Delete deployment
75-
api_response = api_instance.delete_namespaced_deployment(
131+
resp = api.delete_namespaced_deployment(
76132
name=DEPLOYMENT_NAME,
77133
namespace="default",
78134
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.")
82139

83140

84141
def main():
@@ -101,8 +158,10 @@ def main():
101158

102159
update_deployment(apps_v1, deployment)
103160

161+
rolling_restart_deployment(apps_v1, deployment)
162+
104163
delete_deployment(apps_v1)
105164

106165

107-
if __name__ == '__main__':
166+
if __name__ == "__main__":
108167
main()

0 commit comments

Comments
 (0)