Skip to content

Commit b0b29f3

Browse files
committed
feat: script to build docker image with examples
1 parent 61a8acc commit b0b29f3

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

examples/in_cluster_config.py

+53-10
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,70 @@
1414

1515
# Simple example to show loading config from the cluster
1616

17+
#
18+
# If you get 403 errors from API server you will have to configure
19+
# RBAC to add necessay permissions.
20+
#
21+
# ---
22+
# kind: ClusterRole
23+
# apiVersion: rbac.authorization.k8s.io/v1
24+
# metadata:
25+
# name: pods-list
26+
# rules:
27+
# - apiGroups: [""]
28+
# resources: ["pods"]
29+
# verbs: ["list"]
30+
# ---
31+
# kind: ClusterRoleBinding
32+
# apiVersion: rbac.authorization.k8s.io/v1
33+
# metadata:
34+
# name: pods-list
35+
# subjects:
36+
# - kind: ServiceAccount
37+
# name: default
38+
# namespace: default
39+
# roleRef:
40+
# kind: ClusterRole
41+
# name: pods-list
42+
# apiGroup: rbac.authorization.k8s.io
43+
# ---
44+
#
45+
# Doc: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
46+
#
47+
# This example can be run from the image: https://hub.docker.com/r/tpimages/kubernetes_asyncio_examples/
48+
#
49+
# $ kubectl run kubernetes-asyncio-examples --image tpimages/kubernetes_asyncio_examples
50+
#
51+
1752
import asyncio
53+
import sys
54+
import traceback
1855

1956
from kubernetes_asyncio import client, config
2057

2158

2259
async def main():
2360

24-
# it works only if this script is run by K8s as a POD
25-
config.load_incluster_config()
26-
2761
while True:
2862

29-
v1 = client.CoreV1Api()
30-
print("Listing pods with their IPs:")
31-
ret = await v1.list_pod_for_all_namespaces()
63+
try:
64+
65+
# it works only if this script is run by K8s as a POD
66+
config.load_incluster_config()
67+
68+
v1 = client.CoreV1Api()
69+
print("Listing pods with their IPs:")
70+
ret = await v1.list_pod_for_all_namespaces()
71+
72+
for i in ret.items:
73+
print(i.status.pod_ip, i.metadata.namespace, i.metadata.name)
3274

33-
for i in ret.items:
34-
print(i.status.pod_ip, i.metadata.namespace, i.metadata.name)
75+
except Exception:
76+
traceback.print_exc(file=sys.stdout)
3577

36-
print("sleep 10s")
37-
await asyncio.sleep(10)
78+
finally:
79+
print("sleep 10s")
80+
await asyncio.sleep(10)
3881

3982

4083
if __name__ == '__main__':

scripts/docker.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
# script to build docker image with examples
4+
5+
TMP=$(mktemp -d)
6+
7+
cat << EOF > $TMP/Dockerfile
8+
FROM python:3.7-alpine
9+
WORKDIR /examples
10+
RUN pip install kubernetes_asyncio
11+
COPY examples/in_cluster_config.py /examples/
12+
CMD ["python", "/examples/in_cluster_config.py"]
13+
EOF
14+
15+
cp -r examples $TMP/
16+
docker build -t tpimages/kubernetes_asyncio_examples:latest $TMP
17+
18+
rm -fr $TMP

0 commit comments

Comments
 (0)