Skip to content

Instantly share code, notes, and snippets.

@andymotta
Created April 24, 2020 00:50
Show Gist options
  • Save andymotta/4317d30dfdf8def7f008d7535cf3c829 to your computer and use it in GitHub Desktop.
Save andymotta/4317d30dfdf8def7f008d7535cf3c829 to your computer and use it in GitHub Desktop.
Find running images across all of your kubernetes cluster contexts
"""
Lists all the images currently in use in all clusters configured in your .kube/config
"""
from kubernetes import client, config
def main():
all_images = []
contexts, active_context = config.list_kube_config_contexts()
if not contexts:
print("Cannot find any context in kube-config file.")
return
for context in contexts:
current_context=(context['name'])
api_instance = client.CoreV1Api(
api_client=config.new_client_from_config(context=current_context))
try:
for i in api_instance.list_pod_for_all_namespaces().items:
for container in i.status.container_statuses:
if not container.image in all_images:
all_images.append(container.image)
except TypeError as e:
print("Exception when calling CoreV1Api->list_pod_for_all_namespaces: %s\n" % e)
all_images.sort()
print(*all_images, sep = "\n")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment