Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a5424e

Browse files
committed
after review
1 parent 5a3941f commit 5a5424e

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

examples/in_cluster_config.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
# limitations under the License.
1414

1515
# 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.
1720
#
1821
# 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.
2023
#
2124
# ---
2225
# kind: ClusterRole
@@ -43,36 +46,21 @@
4346
# ---
4447
#
4548
# Doc: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
46-
#
47-
48-
import sys
49-
import time
50-
import traceback
5149

5250
from kubernetes import client, config
5351

5452

5553
def main():
5654

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()
7257

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))
7664

7765

7866
if __name__ == '__main__':

0 commit comments

Comments
 (0)