Created
April 24, 2020 00:50
-
-
Save andymotta/4317d30dfdf8def7f008d7535cf3c829 to your computer and use it in GitHub Desktop.
Find running images across all of your kubernetes cluster contexts
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
""" | |
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