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

Skip to content

Commit 476ce50

Browse files
authored
Merge pull request kubernetes-client#1452 from Priyankasaggu11929/psaggu-add-typed-client-deployment-rolling-restart
update `deployment_crud.py` to include a `restart_deployment` method for typed client
2 parents 46f00ef + 525f287 commit 476ce50

File tree

1 file changed

+89
-26
lines changed

1 file changed

+89
-26
lines changed

examples/deployment_crud.py

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@
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

22+
import datetime
23+
24+
import pytz
25+
1926
from kubernetes import client, config
2027

2128
DEPLOYMENT_NAME = "nginx-deployment"
@@ -29,56 +36,110 @@ def create_deployment_object():
2936
ports=[client.V1ContainerPort(container_port=80)],
3037
resources=client.V1ResourceRequirements(
3138
requests={"cpu": "100m", "memory": "200Mi"},
32-
limits={"cpu": "500m", "memory": "500Mi"}
33-
)
39+
limits={"cpu": "500m", "memory": "500Mi"},
40+
),
3441
)
42+
3543
# Create and configurate a spec section
3644
template = client.V1PodTemplateSpec(
3745
metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
38-
spec=client.V1PodSpec(containers=[container]))
46+
spec=client.V1PodSpec(containers=[container]),
47+
)
48+
3949
# Create the specification of deployment
4050
spec = client.V1DeploymentSpec(
41-
replicas=3,
42-
template=template,
43-
selector={'matchLabels': {'app': 'nginx'}})
51+
replicas=3, template=template, selector={
52+
"matchLabels":
53+
{"app": "nginx"}})
54+
4455
# Instantiate the deployment object
4556
deployment = client.V1Deployment(
4657
api_version="apps/v1",
4758
kind="Deployment",
4859
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
49-
spec=spec)
60+
spec=spec,
61+
)
5062

5163
return deployment
5264

5365

54-
def create_deployment(api_instance, deployment):
66+
def create_deployment(api, deployment):
5567
# 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))
68+
resp = api.create_namespaced_deployment(
69+
body=deployment, namespace="default"
70+
)
6071

72+
print("\n[INFO] deployment `nginx-deployment` created.\n")
73+
print("%s\t%s\t\t\t%s\t%s" % ("NAMESPACE", "NAME", "REVISION", "IMAGE"))
74+
print(
75+
"%s\t\t%s\t%s\t\t%s\n"
76+
% (
77+
resp.metadata.namespace,
78+
resp.metadata.name,
79+
resp.metadata.generation,
80+
resp.spec.template.spec.containers[0].image,
81+
)
82+
)
6183

62-
def update_deployment(api_instance, deployment):
84+
85+
def update_deployment(api, deployment):
6386
# Update container image
6487
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))
7188

89+
# patch the deployment
90+
resp = api.patch_namespaced_deployment(
91+
name=DEPLOYMENT_NAME, namespace="default", body=deployment
92+
)
93+
94+
print("\n[INFO] deployment's container image updated.\n")
95+
print("%s\t%s\t\t\t%s\t%s" % ("NAMESPACE", "NAME", "REVISION", "IMAGE"))
96+
print(
97+
"%s\t\t%s\t%s\t\t%s\n"
98+
% (
99+
resp.metadata.namespace,
100+
resp.metadata.name,
101+
resp.metadata.generation,
102+
resp.spec.template.spec.containers[0].image,
103+
)
104+
)
105+
106+
107+
def restart_deployment(api, deployment):
108+
# update `spec.template.metadata` section
109+
# to add `kubectl.kubernetes.io/restartedAt` annotation
110+
deployment.spec.template.metadata.annotations = {
111+
"kubectl.kubernetes.io/restartedAt": datetime.datetime.utcnow()
112+
.replace(tzinfo=pytz.UTC)
113+
.isoformat()
114+
}
115+
116+
# patch the deployment
117+
resp = api.patch_namespaced_deployment(
118+
name=DEPLOYMENT_NAME, namespace="default", body=deployment
119+
)
120+
121+
print("\n[INFO] deployment `nginx-deployment` restarted.\n")
122+
print("%s\t\t\t%s\t%s" % ("NAME", "REVISION", "RESTARTED-AT"))
123+
print(
124+
"%s\t%s\t\t%s\n"
125+
% (
126+
resp.metadata.name,
127+
resp.metadata.generation,
128+
resp.spec.template.metadata.annotations,
129+
)
130+
)
72131

73-
def delete_deployment(api_instance):
132+
133+
def delete_deployment(api):
74134
# Delete deployment
75-
api_response = api_instance.delete_namespaced_deployment(
135+
resp = api.delete_namespaced_deployment(
76136
name=DEPLOYMENT_NAME,
77137
namespace="default",
78138
body=client.V1DeleteOptions(
79-
propagation_policy='Foreground',
80-
grace_period_seconds=5))
81-
print("Deployment deleted. status='%s'" % str(api_response.status))
139+
propagation_policy="Foreground", grace_period_seconds=5
140+
),
141+
)
142+
print("\n[INFO] deployment `nginx-deployment` deleted.")
82143

83144

84145
def main():
@@ -101,8 +162,10 @@ def main():
101162

102163
update_deployment(apps_v1, deployment)
103164

165+
restart_deployment(apps_v1, deployment)
166+
104167
delete_deployment(apps_v1)
105168

106169

107-
if __name__ == '__main__':
170+
if __name__ == "__main__":
108171
main()

0 commit comments

Comments
 (0)