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

Skip to content

update deployment_crud.py to include a restart_deployment method for typed client #1452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 89 additions & 26 deletions examples/deployment_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 restart_deployment(api, deployment):
# update `spec.template.metadata` section
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to fix the pycodestyle test failure, please add an empty line above

# 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_instance):

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():
Expand All @@ -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()