|
14 | 14 |
|
15 | 15 | # Simple example to show loading config from the cluster
|
16 | 16 |
|
| 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 | + |
17 | 52 | import asyncio
|
| 53 | +import sys |
| 54 | +import traceback |
18 | 55 |
|
19 | 56 | from kubernetes_asyncio import client, config
|
20 | 57 |
|
21 | 58 |
|
22 | 59 | async def main():
|
23 | 60 |
|
24 |
| - # it works only if this script is run by K8s as a POD |
25 |
| - config.load_incluster_config() |
26 |
| - |
27 | 61 | while True:
|
28 | 62 |
|
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) |
32 | 74 |
|
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) |
35 | 77 |
|
36 |
| - print("sleep 10s") |
37 |
| - await asyncio.sleep(10) |
| 78 | + finally: |
| 79 | + print("sleep 10s") |
| 80 | + await asyncio.sleep(10) |
38 | 81 |
|
39 | 82 |
|
40 | 83 | if __name__ == '__main__':
|
|
0 commit comments