Update examples (#922)

* Consolidate both examples for create deployments

* Update deployment_examples.py: use V1Deployment

* Update sphinx documentation with deployment examples
This commit is contained in:
Oz N Tiram 2019-08-13 22:57:43 +02:00 committed by Kubernetes Prow Robot
parent 40a03984d8
commit 2e3b50e08e
5 changed files with 30 additions and 47 deletions

View File

@ -14,6 +14,7 @@ Contents:
README <README.md>
installation
usage
examples
modules
contributing <CONTRIBUTING.md>

View File

@ -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

View File

@ -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__':

View File

@ -1,33 +0,0 @@
# Copyright 2018 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.
from os import path
from kubernetes import client, config, utils
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
k8s_client = client.ApiClient()
utils.create_from_yaml(k8s_client, "nginx-deployment.yaml")
k8s_api = client.AppsV1Api(k8s_client)
deps = k8s_api.read_namespaced_deployment("nginx-deployment", "default")
print("Deployment {0} created".format(deps.metadata.name))
if __name__ == '__main__':
main()

View File

@ -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
@ -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)
@ -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__':