Skip to content
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

Update examples #922

Merged
merged 3 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Contents:
README <README.md>
installation
usage
examples
modules
contributing <CONTRIBUTING.md>

Expand Down
17 changes: 15 additions & 2 deletions doc/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
Usage
========

To use kubernetes-python-client in a project::
The directory ``examples`` contains a few examples on how to use the client.

import kubernetes

Deployments
-----------

Here is a simple usage of creating a deployment from a yaml file:


.. literalinclude:: ../../examples/create_deployment.py


The following example demostrates how to create, update and delete deployments
without the need to read a file from the disk:

.. literalinclude:: ../../examples/deployment_examples.py
2 changes: 1 addition & 1 deletion examples/create_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
k8s_apps_v1 = client.AppsV1Api()
resp = k8s_apps_v1.create_namespaced_deployment(
body=dep, namespace="default")
print("Deployment created. status='%s'" % str(resp.status))
print("Deployment created. status='%s'" % resp.metadata.name)


if __name__ == '__main__':
Expand Down
33 changes: 0 additions & 33 deletions examples/create_deployment_from_yaml.py

This file was deleted.

24 changes: 13 additions & 11 deletions examples/deployment_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from os import path

import yaml
"""
This example shows how to work with AppsV1Api to create, modify and delete
deployments
"""

from kubernetes import client, config

Expand All @@ -32,12 +33,13 @@ def create_deployment_object():
metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
spec=client.V1PodSpec(containers=[container]))
# Create the specification of deployment
spec = client.AppsV1beta1DeploymentSpec(
spec = client.V1DeploymentSpec(
replicas=3,
template=template)
template=template,
selector={'matchLabels': {'app': 'nginx'}})
# Instantiate the deployment object
deployment = client.AppsV1beta1Deployment(
api_version="apps/v1beta1",
deployment = client.V1Deployment(
api_version="apps/v1",
kind="Deployment",
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
spec=spec)
Expand Down Expand Up @@ -80,16 +82,16 @@ def main():
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
apps_v1beta1 = client.AppsV1beta1Api()
apps_v1 = client.AppsV1Api()
# Create a deployment object with client-python API. The deployment we
# created is same as the `nginx-deployment.yaml` in the /examples folder.
deployment = create_deployment_object()

create_deployment(apps_v1beta1, deployment)
create_deployment(apps_v1, deployment)

update_deployment(apps_v1beta1, deployment)
update_deployment(apps_v1, deployment)

delete_deployment(apps_v1beta1)
delete_deployment(apps_v1)


if __name__ == '__main__':
Expand Down