Skip to content

Commit 2e3b50e

Browse files
oz123k8s-ci-robot
authored andcommitted
Update examples (#922)
* Consolidate both examples for create deployments * Update deployment_examples.py: use V1Deployment * Update sphinx documentation with deployment examples
1 parent 40a0398 commit 2e3b50e

File tree

5 files changed

+30
-47
lines changed

5 files changed

+30
-47
lines changed

doc/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Contents:
1414
README <README.md>
1515
installation
1616
usage
17+
examples
1718
modules
1819
contributing <CONTRIBUTING.md>
1920

doc/source/usage.rst

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
Usage
33
========
44

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

7-
import kubernetes
7+
8+
Deployments
9+
-----------
10+
11+
Here is a simple usage of creating a deployment from a yaml file:
12+
13+
14+
.. literalinclude:: ../../examples/create_deployment.py
15+
16+
17+
The following example demostrates how to create, update and delete deployments
18+
without the need to read a file from the disk:
19+
20+
.. literalinclude:: ../../examples/deployment_examples.py

examples/create_deployment.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def main():
3030
k8s_apps_v1 = client.AppsV1Api()
3131
resp = k8s_apps_v1.create_namespaced_deployment(
3232
body=dep, namespace="default")
33-
print("Deployment created. status='%s'" % str(resp.status))
33+
print("Deployment created. status='%s'" % resp.metadata.name)
3434

3535

3636
if __name__ == '__main__':

examples/create_deployment_from_yaml.py

-33
This file was deleted.

examples/deployment_examples.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from os import path
16-
17-
import yaml
15+
"""
16+
This example shows how to work with AppsV1Api to create, modify and delete
17+
deployments
18+
"""
1819

1920
from kubernetes import client, config
2021

@@ -32,12 +33,13 @@ def create_deployment_object():
3233
metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
3334
spec=client.V1PodSpec(containers=[container]))
3435
# Create the specification of deployment
35-
spec = client.AppsV1beta1DeploymentSpec(
36+
spec = client.V1DeploymentSpec(
3637
replicas=3,
37-
template=template)
38+
template=template,
39+
selector={'matchLabels': {'app': 'nginx'}})
3840
# Instantiate the deployment object
39-
deployment = client.AppsV1beta1Deployment(
40-
api_version="apps/v1beta1",
41+
deployment = client.V1Deployment(
42+
api_version="apps/v1",
4143
kind="Deployment",
4244
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
4345
spec=spec)
@@ -80,16 +82,16 @@ def main():
8082
# utility. If no argument provided, the config will be loaded from
8183
# default location.
8284
config.load_kube_config()
83-
apps_v1beta1 = client.AppsV1beta1Api()
85+
apps_v1 = client.AppsV1Api()
8486
# Create a deployment object with client-python API. The deployment we
8587
# created is same as the `nginx-deployment.yaml` in the /examples folder.
8688
deployment = create_deployment_object()
8789

88-
create_deployment(apps_v1beta1, deployment)
90+
create_deployment(apps_v1, deployment)
8991

90-
update_deployment(apps_v1beta1, deployment)
92+
update_deployment(apps_v1, deployment)
9193

92-
delete_deployment(apps_v1beta1)
94+
delete_deployment(apps_v1)
9395

9496

9597
if __name__ == '__main__':

0 commit comments

Comments
 (0)