Skip to content

Instantly share code, notes, and snippets.

@rjchicago
Created March 20, 2024 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjchicago/8a2daab939ac80edf32a68f12c351669 to your computer and use it in GitHub Desktop.
Save rjchicago/8a2daab939ac80edf32a68f12c351669 to your computer and use it in GitHub Desktop.
Use kubectl to get all running images in a Kubernetes cluster.
#! /bin/sh
set -Eeou pipefail
# kubectl_get_images.sh
#
# Use kubectl to get all running images in a Kubernetes cluster.
# Ref: https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/
# if context is provided, switch context
if [[ ! -z ${1:-} ]]; then
kubectl config use-context $1
fi
# build images list
IMAGES=
while read IMAGE_TAG; do
IMAGE="${IMAGE_TAG%%:*}"
# prefix docker.io images for clarity
if [[ $IMAGE =~ ^[^\.\/]+(\/|$) ]]; then
IMAGE="docker.io/$IMAGE"
fi
IMAGES="$IMAGES\n$IMAGE"
done <<< "$(kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec['initContainers', 'containers'][*].image}" | tr -s '[[:space:]]' '\n')"
# echo images sorted and de-duped
echo $IMAGES | sort -u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment