add "restart_deployment" method

Signed-off-by: Priyanka Saggu <priyankasaggu11929@gmail.com>
This commit is contained in:
Priyanka Saggu 2021-05-13 13:09:09 +05:30
parent 46f00efc43
commit 525f2870ed
No known key found for this signature in database
GPG Key ID: 802E62D80FFBD8FC

View File

@ -13,9 +13,16 @@
# limitations under the License.
"""
Creates, updates, and deletes a deployment using AppsV1Api.
The example covers the following:
- Creation of a deployment using AppsV1Api
- update/patch to perform rolling restart on the deployment
- deletetion of the deployment
"""
import datetime
import pytz
from kubernetes import client, config
DEPLOYMENT_NAME = "nginx-deployment"
@ -29,56 +36,110 @@ def create_deployment_object():
ports=[client.V1ContainerPort(container_port=80)],
resources=client.V1ResourceRequirements(
requests={"cpu": "100m", "memory": "200Mi"},
limits={"cpu": "500m", "memory": "500Mi"}
)
limits={"cpu": "500m", "memory": "500Mi"},
),
)
# Create and configurate a spec section
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
spec=client.V1PodSpec(containers=[container]))
spec=client.V1PodSpec(containers=[container]),
)
# Create the specification of deployment
spec = client.V1DeploymentSpec(
replicas=3,
template=template,
selector={'matchLabels': {'app': 'nginx'}})
replicas=3, template=template, selector={
"matchLabels":
{"app": "nginx"}})
# Instantiate the deployment object
deployment = client.V1Deployment(
api_version="apps/v1",
kind="Deployment",
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
spec=spec)
spec=spec,
)
return deployment
def create_deployment(api_instance, deployment):
def create_deployment(api, deployment):
# Create deployement
api_response = api_instance.create_namespaced_deployment(
body=deployment,
namespace="default")
print("Deployment created. status='%s'" % str(api_response.status))
resp = api.create_namespaced_deployment(
body=deployment, namespace="default"
)
print("\n[INFO] deployment `nginx-deployment` created.\n")
print("%s\t%s\t\t\t%s\t%s" % ("NAMESPACE", "NAME", "REVISION", "IMAGE"))
print(
"%s\t\t%s\t%s\t\t%s\n"
% (
resp.metadata.namespace,
resp.metadata.name,
resp.metadata.generation,
resp.spec.template.spec.containers[0].image,
)
)
def update_deployment(api_instance, deployment):
def update_deployment(api, deployment):
# Update container image
deployment.spec.template.spec.containers[0].image = "nginx:1.16.0"
# Update the deployment
api_response = api_instance.patch_namespaced_deployment(
name=DEPLOYMENT_NAME,
namespace="default",
body=deployment)
print("Deployment updated. status='%s'" % str(api_response.status))
# patch the deployment
resp = api.patch_namespaced_deployment(
name=DEPLOYMENT_NAME, namespace="default", body=deployment
)
print("\n[INFO] deployment's container image updated.\n")
print("%s\t%s\t\t\t%s\t%s" % ("NAMESPACE", "NAME", "REVISION", "IMAGE"))
print(
"%s\t\t%s\t%s\t\t%s\n"
% (
resp.metadata.namespace,
resp.metadata.name,
resp.metadata.generation,
resp.spec.template.spec.containers[0].image,
)
)
def delete_deployment(api_instance):
def restart_deployment(api, deployment):
# update `spec.template.metadata` section
# to add `kubectl.kubernetes.io/restartedAt` annotation
deployment.spec.template.metadata.annotations = {
"kubectl.kubernetes.io/restartedAt": datetime.datetime.utcnow()
.replace(tzinfo=pytz.UTC)
.isoformat()
}
# patch the deployment
resp = api.patch_namespaced_deployment(
name=DEPLOYMENT_NAME, namespace="default", body=deployment
)
print("\n[INFO] deployment `nginx-deployment` restarted.\n")
print("%s\t\t\t%s\t%s" % ("NAME", "REVISION", "RESTARTED-AT"))
print(
"%s\t%s\t\t%s\n"
% (
resp.metadata.name,
resp.metadata.generation,
resp.spec.template.metadata.annotations,
)
)
def delete_deployment(api):
# Delete deployment
api_response = api_instance.delete_namespaced_deployment(
resp = api.delete_namespaced_deployment(
name=DEPLOYMENT_NAME,
namespace="default",
body=client.V1DeleteOptions(
propagation_policy='Foreground',
grace_period_seconds=5))
print("Deployment deleted. status='%s'" % str(api_response.status))
propagation_policy="Foreground", grace_period_seconds=5
),
)
print("\n[INFO] deployment `nginx-deployment` deleted.")
def main():
@ -101,8 +162,10 @@ def main():
update_deployment(apps_v1, deployment)
restart_deployment(apps_v1, deployment)
delete_deployment(apps_v1)
if __name__ == '__main__':
if __name__ == "__main__":
main()