Last active
February 7, 2025 16:19
-
-
Save se7enack/63ceb05e672f8617471e5a8d7d97352d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kubernetes import client, config | |
config.load_kube_config() | |
def get_pods(namespace='all-namespaces'): | |
v1 = client.CoreV1Api() | |
if namespace == 'all-namespaces': | |
ret = v1.list_pod_for_all_namespaces(watch=False) | |
else: | |
ret = v1.list_namespaced_pod(namespace) | |
print('\n') | |
try: | |
pod_ip_len = [] | |
namespace_len =[] | |
name_len = [] | |
for i in ret.items: | |
pod_ip_len.append(len(i.status.pod_ip)) | |
namespace_len.append(len(i.metadata.namespace)) | |
name_len.append(len(i.metadata.name)) | |
pod_ip_pad = max(pod_ip_len) | |
namespace_pad = max(namespace_len) | |
name_pad = max(name_len) | |
print("IP".center(pod_ip_pad, " ") + " " + "Namespace".center(namespace_pad, " ") + " " + "Pod".center(name_pad, " ")) | |
for i in ret.items: | |
print(f"{str(i.status.pod_ip):<{pod_ip_pad}} {str(i.metadata.namespace):<{namespace_pad}} {str(i.metadata.name):<{name_pad}}") | |
print('\n') | |
except: | |
pass | |
get_pods('default') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment