Created
March 20, 2024 13:57
-
-
Save rjchicago/8a2daab939ac80edf32a68f12c351669 to your computer and use it in GitHub Desktop.
Use kubectl to get all running images in a Kubernetes cluster.
This file contains 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
#! /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