|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | # Simple example to show loading config from the cluster
|
16 |
| - |
| 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. |
17 | 20 | #
|
18 | 21 | # If you get 403 errors from API server you will have to configure
|
19 |
| -# RBAC to add necessary permissions. |
| 22 | +# RBAC to add the permission to list pods. |
20 | 23 | #
|
21 | 24 | # ---
|
22 | 25 | # kind: ClusterRole
|
|
43 | 46 | # ---
|
44 | 47 | #
|
45 | 48 | # Doc: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
|
46 |
| -# |
47 |
| - |
48 |
| -import sys |
49 |
| -import time |
50 |
| -import traceback |
51 | 49 |
|
52 | 50 | from kubernetes import client, config
|
53 | 51 |
|
54 | 52 |
|
55 | 53 | def main():
|
56 | 54 |
|
57 |
| - while True: |
58 |
| - |
59 |
| - try: |
60 |
| - # it works only if this script is run by K8s as a POD |
61 |
| - config.load_incluster_config() |
62 |
| - |
63 |
| - v1 = client.CoreV1Api() |
64 |
| - print("Listing pods with their IPs:") |
65 |
| - ret = v1.list_pod_for_all_namespaces(watch=False) |
66 |
| - for i in ret.items: |
67 |
| - print("%s\t%s\t%s" % |
68 |
| - (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) |
69 |
| - |
70 |
| - except Exception: |
71 |
| - traceback.print_exc(file=sys.stdout) |
| 55 | + # it works only if this script is run by K8s as a POD |
| 56 | + config.load_incluster_config() |
72 | 57 |
|
73 |
| - finally: |
74 |
| - print("sleep 10s") |
75 |
| - time.sleep(10) |
| 58 | + v1 = client.CoreV1Api() |
| 59 | + print("Listing pods with their IPs:") |
| 60 | + ret = v1.list_pod_for_all_namespaces(watch=False) |
| 61 | + for i in ret.items: |
| 62 | + print("%s\t%s\t%s" % |
| 63 | + (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) |
76 | 64 |
|
77 | 65 |
|
78 | 66 | if __name__ == '__main__':
|
|
0 commit comments