Repository navigation
add example to demonstrate a rolling restart of the deployment #1450
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
k8s-ci-robot
merged 1 commit into
kubernetes-client:master
from
Priyankasaggu11929:psaggu-deployment-rolling-restart
May 3, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # Copyright 2021 The Kubernetes Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| This example demonstrates the following: | ||
| - Creation of a k8s deployment using dynamic-client | ||
| - Rolling restart of the deployment (demonstrate patch/update action) | ||
| - Listing & deletion of the deployment | ||
| """ | ||
|
|
||
|
|
||
| from kubernetes import config, dynamic | ||
| from kubernetes.client import api_client | ||
| import datetime | ||
| import pytz | ||
|
|
||
| def main(): | ||
| # Creating a dynamic client | ||
| client = dynamic.DynamicClient( | ||
| api_client.ApiClient(configuration=config.load_kube_config()) | ||
| ) | ||
|
|
||
| # fetching the deployment api | ||
| api = client.resources.get(api_version="apps/v1", kind="Deployment") | ||
|
|
||
| name = "nginx-deployment" | ||
|
|
||
| deployment_manifest = { | ||
| "apiVersion": "apps/v1", | ||
| "kind": "Deployment", | ||
| "metadata": {"labels": {"app": "nginx"}, "name": name}, | ||
| "spec": { | ||
| "replicas": 3, | ||
| "selector": {"matchLabels": {"app": "nginx"}}, | ||
| "template": { | ||
| "metadata": {"labels": {"app": "nginx"}}, | ||
| "spec": { | ||
| "containers": [ | ||
| { | ||
| "name": "nginx", | ||
| "image": "nginx:1.14.2", | ||
| "ports": [{"containerPort": 80}], | ||
| } | ||
| ] | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| # Creating deployment `nginx-deployment` in the `default` namespace | ||
|
|
||
| deployment = api.create(body=deployment_manifest, namespace="default") | ||
|
|
||
| print("\n[INFO] deployment `nginx-deployment` created\n") | ||
|
|
||
| # Listing deployment `nginx-deployment` in the `default` namespace | ||
|
|
||
| deployment_created = api.get(name=name, namespace="default") | ||
|
|
||
| print("%s\t%s\t\t\t%s\t%s" % ("NAMESPACE", "NAME", "REVISION", "RESTARTED-AT")) | ||
| print( | ||
| "%s\t\t%s\t%s\t\t%s\n" | ||
| % ( | ||
| deployment_created.metadata.namespace, | ||
| deployment_created.metadata.name, | ||
| deployment_created.metadata.annotations, | ||
| deployment_created.spec.template.metadata.annotations, | ||
| ) | ||
| ) | ||
|
|
||
| # Patching the `spec.template.metadata` section to add `kubectl.kubernetes.io/restartedAt` annotation | ||
| # In order to perform a rolling restart on the deployment `nginx-deployment` | ||
|
|
||
| deployment_manifest["spec"]["template"]["metadata"] = { | ||
| "annotations": { | ||
| "kubectl.kubernetes.io/restartedAt": datetime.datetime.utcnow() | ||
| .replace(tzinfo=pytz.UTC) | ||
| .isoformat() | ||
| } | ||
| } | ||
|
|
||
| deployment_patched = api.patch( | ||
| body=deployment_manifest, name=name, namespace="default" | ||
| ) | ||
|
|
||
| print("\n[INFO] deployment `nginx-deployment` restarted\n") | ||
| print( | ||
| "%s\t%s\t\t\t%s\t\t\t\t\t\t%s" | ||
| % ("NAMESPACE", "NAME", "REVISION", "RESTARTED-AT") | ||
| ) | ||
| print( | ||
| "%s\t\t%s\t%s\t\t%s\n" | ||
| % ( | ||
| deployment_patched.metadata.namespace, | ||
| deployment_patched.metadata.name, | ||
| deployment_patched.metadata.annotations, | ||
| deployment_patched.spec.template.metadata.annotations, | ||
| ) | ||
| ) | ||
|
|
||
| # Deleting deployment `nginx-deployment` from the `default` namespace | ||
|
|
||
| deployment_deleted = api.delete(name=name, body={}, namespace="default") | ||
|
|
||
| print("\n[INFO] deployment `nginx-deployment` deleted\n") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for using dynamic client, instead of using the typed client?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HI @yliaog, thanks for the reveiw.
No, there is no specific reason for using
dynamic client. I've just replicated the same example using thetyped client. If required, I can replace this example with the one usingtyped client.Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. i think typed client would benefit from such an example too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @yliaog. I'll add the same example using the
typed clienttoo.