Last active
August 14, 2019 07:31
-
-
Save portante/cdd063bdb7db6dae421abd675df6a002 to your computer and use it in GitHub Desktop.
Format output from "oc get pods -o json" or "oc adm manage-node --list-pods --output=json" into a list with container, pod, namespace, cpu, memory, host
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
#!/usr/bin/env python | |
import sys | |
import os | |
import json | |
if sys.argv[1] == '-': | |
pods = json.load(sys.stdin) | |
else: | |
with open(sys.argv[1], "r") as fp: | |
pods = json.load(fp) | |
assert pods['kind'] == 'List' | |
assert pods['apiVersion'] == 'v1' | |
#print json.dumps(pods, indent=4, sort_keys=True) | |
keys = [ 'cn', 'pn', 'nn', 'cpu', 'mem', 'hn' ] | |
headers = { 'cn': "CONTAINER", 'pn': "POD", 'nn': "NAMESPACE", 'cpu': "CPU lim/req", 'mem': "MEMORY lim/req", 'hn': "HOSTNAME" } | |
widths = { 'cn': len(headers['cn']), 'pn': len(headers['pn']), 'nn': len(headers['nn']), 'cpu': len(headers['cpu']), 'mem': len(headers['mem']), 'hn': len(headers['hn']) } | |
containers = [] | |
for pod in pods['items']: | |
pod_name = pod['metadata']['name'] | |
widths['pn'] = max(len(pod_name), widths['pn']) | |
namespace_name = pod['metadata']['namespace'] | |
widths['nn'] = max(len(namespace_name), widths['nn']) | |
host_name = pod['spec']['nodeName'] | |
widths['hn'] = max(len(host_name), widths['hn']) | |
for container in pod['spec']['containers']: | |
try: | |
req_cpu = container["resources"]["requests"]["cpu"] | |
except Exception: | |
req_cpu = "-" | |
try: | |
req_mem = container["resources"]["requests"]["memory"] | |
except Exception: | |
req_mem = "-" | |
try: | |
lim_cpu = container["resources"]["limits"]["cpu"] | |
except Exception: | |
lim_cpu = "-" | |
try: | |
lim_mem = container["resources"]["limits"]["memory"] | |
except Exception: | |
lim_mem = "-" | |
cpu = "%s/%s" % (lim_cpu, req_cpu) | |
widths['cpu'] = max(len(cpu), widths['cpu']) | |
mem = "%s/%s" % (lim_mem, req_mem) | |
widths['mem'] = max(len(mem), widths['mem']) | |
container_name = container['name'] | |
widths['cn'] = max(len(container_name), widths['cn']) | |
containers.append({ 'cn': container_name, 'pn': pod_name, 'nn': namespace_name, 'cpu': cpu, 'mem': mem, 'hn': host_name }) | |
for k in keys: | |
sys.stdout.write("%-*s " % (widths[k], headers[k])) | |
sys.stdout.write("\n") | |
for container in containers: | |
for k in keys: | |
sys.stdout.write("%-*s " % (widths[k], container[k])) | |
sys.stdout.write("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example output: