Skip to content

Instantly share code, notes, and snippets.

@victortv7
Created August 12, 2019 15:50
Show Gist options
  • Save victortv7/d1e8e7fb54f6cb6d79828e1baaaa6018 to your computer and use it in GitHub Desktop.
Save victortv7/d1e8e7fb54f6cb6d79828e1baaaa6018 to your computer and use it in GitHub Desktop.
Python script that prints the images pulled in each node on a kubernetes cluster, similarly to `docker images`
#!/usr/bin/python
# Usage: kubectl get node [optional: name of node] -o json | python k8s_node_images.py
import sys
import json
data = json.load(sys.stdin)
try:
nodes = data['items']
except KeyError:
nodes = [data]
for node in nodes:
node_name = node['metadata']['name']
print("\nNODE {}".format(node_name))
print("REPOSITORY".ljust(48)+" "+TAG".ljust(16)+" "+SIZE")
images = node['status']['images']
for image in images:
if len(image['names'])<2:
continue
names = image['names'][1].rsplit(":",1)
name = names[0]
tag = names[1]
size = image['sizeBytes']
size_mb = "{0:.1f}MB".format(size/1000000.)
print(name.ljust(48)+" "+tag.ljust(16)+" "+size_mb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment