Skip to content

Commit 04c499c

Browse files
author
Scott Lee
committed
884: Cleanup examples folder
1 parent 91c080d commit 04c499c

22 files changed

+339
-435
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ More examples can be found in [examples](examples/) folder. To run examples, run
6868
python -m examples.example1
6969
```
7070

71-
(replace example1 with the example base filename)
71+
(replace example1 with one of the filenames in the examples folder)
7272

7373
## Documentation
7474

@@ -178,4 +178,4 @@ Specifically check `ipaddress` and `urllib3` package versions to make sure they
178178

179179
Starting from 4.0 release, we do not support directly calling exec or attach calls. you should use stream module to call them. so instead
180180
of `resp = api.connect_get_namespaced_pod_exec(name, ...` you should call `resp = stream(api.connect_get_namespaced_pod_exec, name, ...`.
181-
See more at [exec example](examples/exec.py).
181+
See more at [exec example](examples/pod_exec.py).

examples/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Python Client Examples
2+
3+
This directory contains various examples how to use the Python client. Please
4+
read the description at the top of each script for more information about what
5+
it does and any prequisite steps. Most scripts also include comments throughout
6+
the code.
7+
8+
## Setup
9+
10+
These scripts require Python 2.7 or 3.5+ and the Kubernetes client which can be
11+
installed via the directions
12+
[here](https://github.com/kubernetes-client/python#installation).
13+
14+
## Contributions
15+
16+
If you find a problem please file an
17+
[issue](https://github.com/kubernetes-client/python/issues).

examples/__init__.py

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

15-
# Empty init file to make examples folder a python module.
15+
# Empty init file to make examples folder a python module

examples/example3.py examples/api_discovery.py

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

15+
"""
16+
Reads the list of the available API versions and prints them.
17+
Similar to running `kubectl api-versions`.
18+
"""
19+
1520
from kubernetes import client, config
1621

1722

examples/custom_object.py

+24-23
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,38 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# This example use an example CRD from this tutorial:
16-
# https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/
17-
#
18-
# The following yaml manifest has to be applied first:
19-
#
20-
# apiVersion: apiextensions.k8s.io/v1beta1
21-
# kind: CustomResourceDefinition
22-
# metadata:
23-
# name: crontabs.stable.example.com
24-
# spec:
25-
# group: stable.example.com
26-
# versions:
27-
# - name: v1
28-
# served: true
29-
# storage: true
30-
# scope: Namespaced
31-
# names:
32-
# plural: crontabs
33-
# singular: crontab
34-
# kind: CronTab
35-
# shortNames:
36-
# - ct
15+
"""
16+
Uses a Custom Resource Definition (CRD) to create a custom object, in this case
17+
a CronTab. This example use an example CRD from this tutorial:
18+
https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/
3719
20+
The following yaml manifest has to be applied first:
21+
22+
apiVersion: apiextensions.k8s.io/v1
23+
kind: CustomResourceDefinition
24+
metadata:
25+
name: crontabs.stable.example.com
26+
spec:
27+
group: stable.example.com
28+
versions:
29+
- name: v1
30+
served: true
31+
storage: true
32+
scope: Namespaced
33+
names:
34+
plural: crontabs
35+
singular: crontab
36+
kind: CronTab
37+
shortNames:
38+
- ct
39+
"""
3840

3941
from pprint import pprint
4042

4143
from kubernetes import client, config
4244

4345

4446
def main():
45-
4647
config.load_kube_config()
4748

4849
api = client.CustomObjectsApi()
File renamed without changes.

examples/deployment_examples.py examples/deployment_crud.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
# limitations under the License.
1414

1515
"""
16-
This example shows how to work with AppsV1Api to create, modify and delete
17-
deployments
16+
Creates, updates, and deletes a deployment using AppsV1Api.
1817
"""
1918

2019
from kubernetes import client, config

examples/exec.py

-97
This file was deleted.

examples/in_cluster_config.py

+35-36
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,46 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Simple example to show loading config from the cluster
16-
#
17-
# It works only from a pod. You can start an image with Python
18-
# (for example python:latest), exec into the pod, install the library,
19-
# then try out this example.
20-
#
21-
# If you get 403 errors from API server you will have to configure
22-
# RBAC to add the permission to list pods.
23-
#
24-
# ---
25-
# kind: ClusterRole
26-
# apiVersion: rbac.authorization.k8s.io/v1
27-
# metadata:
28-
# name: pods-list
29-
# rules:
30-
# - apiGroups: [""]
31-
# resources: ["pods"]
32-
# verbs: ["list"]
33-
# ---
34-
# kind: ClusterRoleBinding
35-
# apiVersion: rbac.authorization.k8s.io/v1
36-
# metadata:
37-
# name: pods-list
38-
# subjects:
39-
# - kind: ServiceAccount
40-
# name: default
41-
# namespace: default
42-
# roleRef:
43-
# kind: ClusterRole
44-
# name: pods-list
45-
# apiGroup: rbac.authorization.k8s.io
46-
# ---
47-
#
48-
# Doc: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
15+
"""
16+
Showcases loading the Kubernetes config from within the cluster. This script
17+
must be run within a pod. You can start a pod with a Python image (for
18+
example, `python:latest`), exec into the pod, install the library, then run
19+
this example.
20+
21+
If you get 403 errors from the API server you will have to configure RBAC to
22+
add the permission to list pods by applying the following manifest:
23+
24+
---
25+
kind: ClusterRole
26+
apiVersion: rbac.authorization.k8s.io/v1
27+
metadata:
28+
name: pods-list
29+
rules:
30+
- apiGroups: [""]
31+
resources: ["pods"]
32+
verbs: ["list"]
33+
34+
---
35+
kind: ClusterRoleBinding
36+
apiVersion: rbac.authorization.k8s.io/v1
37+
metadata:
38+
name: pods-list
39+
subjects:
40+
- kind: ServiceAccount
41+
name: default
42+
namespace: default
43+
roleRef:
44+
kind: ClusterRole
45+
name: pods-list
46+
apiGroup: rbac.authorization.k8s.io
47+
48+
Documentation: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
49+
"""
4950

5051
from kubernetes import client, config
5152

5253

5354
def main():
54-
55-
# it works only if this script is run by K8s as a POD
5655
config.load_incluster_config()
5756

5857
v1 = client.CoreV1Api()

examples/ingress-example.py examples/ingress_create.py

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

15+
"""
16+
Creates deployment, service, and ingress objects. The ingress allows external
17+
network access within the cluster.
18+
"""
19+
1520
from kubernetes import client, config
1621

1722

@@ -73,7 +78,7 @@ def create_ingress(networking_v1_beta1_api):
7378
}),
7479
spec=client.NetworkingV1beta1IngressSpec(
7580
rules=[client.NetworkingV1beta1IngressRule(
76-
host="boddulabs.com",
81+
host="example.com",
7782
http=client.NetworkingV1beta1HTTPIngressRuleValue(
7883
paths=[client.NetworkingV1beta1HTTPIngressPath(
7984
path="/",

examples/job_examples.py examples/job_crud.py

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

15+
"""
16+
Creates, updates, and deletes a Job object.
17+
"""
18+
1519
from os import path
1620

1721
import yaml
@@ -46,7 +50,6 @@ def create_job_object():
4650

4751

4852
def create_job(api_instance, job):
49-
# Create job
5053
api_response = api_instance.create_namespaced_job(
5154
body=job,
5255
namespace="default")
@@ -56,7 +59,6 @@ def create_job(api_instance, job):
5659
def update_job(api_instance, job):
5760
# Update container image
5861
job.spec.template.spec.containers[0].image = "perl"
59-
# Update the job
6062
api_response = api_instance.patch_namespaced_job(
6163
name=JOB_NAME,
6264
namespace="default",
@@ -65,7 +67,6 @@ def update_job(api_instance, job):
6567

6668

6769
def delete_job(api_instance):
68-
# Delete job
6970
api_response = api_instance.delete_namespaced_job(
7071
name=JOB_NAME,
7172
namespace="default",

0 commit comments

Comments
 (0)