Skip to content

Instantly share code, notes, and snippets.

@ialidzhikov
Last active April 17, 2024 14:13
Show Gist options
  • Save ialidzhikov/db2edcce137cf8f66253512ed652ac10 to your computer and use it in GitHub Desktop.
Save ialidzhikov/db2edcce137cf8f66253512ed652ac10 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
image_size_for_linux_amd64() {
linux_amd64_entry=$(docker manifest inspect -v "$1" | jq -c 'if type == "array" then .[] else . end' | jq -r '. | select(.Descriptor.platform.os=="linux" and .Descriptor.platform.architecture=="amd64")')
echo "$linux_amd64_entry" | jq -c 'if has("SchemaV2Manifest") then .SchemaV2Manifest else .OCIManifest end' | jq -r '[ .layers[].size ] | add '; #| numfmt --to iec --format '%.2f';
}
uniq_cluster_images=$(kubectl get po --all-namespaces -o json | jq -r '.items[] | select(.status.phase == "Running" or .status.phase=="Completed") | .spec.containers[].image' | sort | uniq)
output=""
for image in $uniq_cluster_images; do
echo "Processing image: $image"
image_size=$(image_size_for_linux_amd64 "$image")
output+="$image,$image_size "
done
echo
echo "> Image sizes by upstream"
previous_upstream=""
upstream_total_size=0
for line in $output; do
line_array=(${line//,/ })
image="${line_array[0]}"
size="${line_array[1]}"
current_upstream=$(echo "$image" | cut -d "/" -f 1)
if [[ -z "$previous_upstream" ]]; then
previous_upstream="$current_upstream"
fi
if [[ "$current_upstream" == "$previous_upstream" ]]; then
upstream_total_size=$((upstream_total_size + size))
else
formatted_size=$(echo "${upstream_total_size}" | numfmt --to iec --format '%.2f')
echo "[$previous_upstream] Total: ${formatted_size}"
upstream_total_size="$size"
previous_upstream="$current_upstream"
fi
formatted_size=$(echo "${size}" | numfmt --to iec --format '%.2f')
echo "* ${image} ${formatted_size}"
done
formatted_size=$(echo "${upstream_total_size}" | numfmt --to iec --format '%.2f')
echo "[$previous_upstream] Total: ${formatted_size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment