Instantly share code, notes, and snippets.
Last active
October 21, 2024 21:41
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save pavelanni/5fa7e8fdecb2611a4543e1e6c44de341 to your computer and use it in GitHub Desktop.
Get image tags from sha256 (works with Harbor; Docker requires slightly different API_URL)
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
#!/bin/bash | |
# Function to get tags for a given image | |
get_tags() { | |
local FULL_IMAGE="$1" | |
local POD_NAME="$2" | |
# Extract components from the full image | |
REGISTRY=$(echo "$FULL_IMAGE" | cut -d'/' -f1) | |
PROJECT=$(echo "$FULL_IMAGE" | cut -d'/' -f2) | |
REPO_AND_DIGEST=$(echo "$FULL_IMAGE" | cut -d'/' -f3-) | |
REPO=$(echo "$REPO_AND_DIGEST" | cut -d'@' -f1) | |
DIGEST=$(echo "$REPO_AND_DIGEST" | cut -d':' -f2) | |
# Construct the API URL | |
API_URL="https://$REGISTRY/api/v2.0/projects/$PROJECT/repositories/$REPO/artifacts/sha256:$DIGEST/tags" | |
# Make the API call and extract tags | |
tags=$(curl -s "$API_URL" | jq -r '.[] | .name') | |
# Print the pod name | |
echo "Pod: $POD_NAME" | |
# Print the full image names with tags | |
for t in ${tags}; do | |
echo " ${REGISTRY}/${PROJECT}/${REPO}:${t}" | |
done | |
echo "" | |
} | |
# Get all pods in the aistor namespace | |
PODS=$(kubectl get pods -n aistor -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') | |
# Loop through each pod | |
for POD_NAME in $PODS; do | |
# Get the image ID (SHA256) for all containers in the pod | |
IMAGE_IDS=$(kubectl get pod $POD_NAME -n aistor -o jsonpath='{.status.containerStatuses[*].imageID}') | |
# Loop through each image ID | |
for IMAGE_ID in $IMAGE_IDS; do | |
# Extract the full image name with SHA256 from the image ID | |
FULL_IMAGE=$(echo $IMAGE_ID | sed 's/docker-pullable:\/\///') | |
# Get and print tags for this image | |
get_tags "$FULL_IMAGE" "$POD_NAME" | |
done | |
done |
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
#!/bin/bash | |
# Function to get tags for a given image | |
get_tags() { | |
local FULL_IMAGE="$1" | |
local POD_NAME="$2" | |
# Extract components from the full image | |
REGISTRY=$(echo "$FULL_IMAGE" | cut -d'/' -f1) | |
PROJECT=$(echo "$FULL_IMAGE" | cut -d'/' -f2) | |
REPO_AND_DIGEST=$(echo "$FULL_IMAGE" | cut -d'/' -f3-) | |
REPO=$(echo "$REPO_AND_DIGEST" | cut -d'@' -f1) | |
DIGEST=$(echo "$REPO_AND_DIGEST" | cut -d':' -f2) | |
# Construct the API URL | |
API_URL="https://$REGISTRY/api/v2.0/projects/$PROJECT/repositories/$REPO/artifacts/sha256:$DIGEST/tags" | |
# Make the API call and extract tags | |
tags=$(curl -s "$API_URL" | jq -r '.[] | .name') | |
# Print the pod name | |
echo "Pod: $POD_NAME" | |
# Print the full image names with tags | |
for t in ${tags}; do | |
echo " ${REGISTRY}/${PROJECT}/${REPO}:${t}" | |
done | |
echo "" | |
} | |
# Parse command line arguments | |
NAMESPACE="aistor" # Default namespace | |
while getopts "n:" opt; do | |
case $opt in | |
n) NAMESPACE="$OPTARG" ;; | |
*) | |
echo "Usage: $0 [-n namespace]" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
# Get all pods in the specified namespace | |
PODS=$(kubectl get pods -n "$NAMESPACE" -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') | |
# Loop through each pod | |
for POD_NAME in $PODS; do | |
# Get the images and image IDs for all containers in the pod | |
IMAGES=$(kubectl get pod $POD_NAME -n "$NAMESPACE" -o jsonpath='{.status.containerStatuses[*].image}') | |
IMAGE_IDS=$(kubectl get pod $POD_NAME -n "$NAMESPACE" -o jsonpath='{.status.containerStatuses[*].imageID}') | |
# Convert space-separated strings to arrays | |
IFS=' ' read -ra IMAGE_ARRAY <<<"$IMAGES" | |
IFS=' ' read -ra IMAGE_ID_ARRAY <<<"$IMAGE_IDS" | |
# Loop through each image | |
for i in "${!IMAGE_ARRAY[@]}"; do | |
IMAGE="${IMAGE_ARRAY[$i]}" | |
IMAGE_ID="${IMAGE_ID_ARRAY[$i]}" | |
# Extract the tag from the image | |
TAG=$(echo "$IMAGE" | awk -F: '{print $NF}') | |
if [[ "$TAG" != "edge" && "$TAG" != "latest" ]]; then | |
# Print pod name and image name if the tag is not 'edge' or 'latest' | |
echo "Pod: $POD_NAME" | |
echo " $IMAGE" | |
echo "" | |
else | |
# Extract the full image name with SHA256 from the image ID | |
FULL_IMAGE=$(echo "$IMAGE_ID" | sed 's/docker-pullable:\/\///') | |
# Get and print tags for this image | |
get_tags "$FULL_IMAGE" "$POD_NAME" | |
fi | |
done | |
done |
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
#!/bin/bash | |
# Function to display usage information | |
usage() { | |
echo "Usage: $0 [-n NAMESPACE] [-c CONTAINER_NAME] POD_NAME" | |
echo "Example: $0 -n default -c my-container my-pod" | |
exit 1 | |
} | |
# Initialize variables | |
NAMESPACE="" | |
CONTAINER_NAME="" | |
# Parse command-line options | |
while getopts ":n:c:" opt; do | |
case $opt in | |
n) NAMESPACE="$OPTARG" ;; | |
c) CONTAINER_NAME="$OPTARG" ;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
usage | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
usage | |
;; | |
esac | |
done | |
# Remove the options from the positional parameters | |
shift $((OPTIND - 1)) | |
# Check if POD_NAME is provided | |
if [ $# -ne 1 ]; then | |
usage | |
fi | |
POD_NAME="$1" | |
# Construct kubectl command | |
KUBECTL_CMD="kubectl get pod $POD_NAME" | |
if [ -n "$NAMESPACE" ]; then | |
KUBECTL_CMD+=" -n $NAMESPACE" | |
fi | |
KUBECTL_CMD+=" -o jsonpath='{.status.containerStatuses[*].imageID}'" | |
# Add container filter if specified | |
if [ -n "$CONTAINER_NAME" ]; then | |
KUBECTL_CMD=$(echo $KUBECTL_CMD | sed "s/containerStatuses\[*\]/containerStatuses[?(@.name=='$CONTAINER_NAME')]/" | sed "s/'//g") | |
fi | |
# Get the image ID (SHA256) from the running pod | |
IMAGE_ID=$(eval $KUBECTL_CMD) | |
if [ -z "$IMAGE_ID" ]; then | |
echo "Error: Could not find image ID for pod '$POD_NAME'" | |
[ -n "$NAMESPACE" ] && echo "Namespace: $NAMESPACE" | |
[ -n "$CONTAINER_NAME" ] && echo "Container: $CONTAINER_NAME" | |
exit 1 | |
fi | |
# Extract the full image name with SHA256 from the image ID | |
FULL_IMAGE=$(echo $IMAGE_ID | sed 's/docker-pullable:\/\///') | |
# Extract components from the full image | |
REGISTRY=$(echo "$FULL_IMAGE" | cut -d'/' -f1) | |
PROJECT=$(echo "$FULL_IMAGE" | cut -d'/' -f2) | |
REPO_AND_DIGEST=$(echo "$FULL_IMAGE" | cut -d'/' -f3-) | |
REPO=$(echo "$REPO_AND_DIGEST" | cut -d'@' -f1) | |
DIGEST=$(echo "$REPO_AND_DIGEST" | cut -d':' -f2) | |
# Construct the API URL | |
API_URL="https://$REGISTRY/api/v2.0/projects/$PROJECT/repositories/$REPO/artifacts/sha256:$DIGEST/tags" | |
# Make the API call and extract tags | |
tags=$(curl -s "$API_URL" | jq -r '.[] | .name') | |
# Print the full image names with tags | |
for t in ${tags}; do | |
echo ${REGISTRY}/${PROJECT}/${REPO}:${t} | |
done |
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
#!/bin/bash | |
# Check if an argument is provided | |
if [ $# -eq 0 ]; then | |
echo "Please provide the full image name with SHA256" | |
echo "Usage: $0 <image_name>@sha256:<digest>" | |
exit 1 | |
fi | |
# Extract components from the input | |
FULL_IMAGE="$1" | |
REGISTRY=$(echo "$FULL_IMAGE" | cut -d'/' -f1) | |
PROJECT=$(echo "$FULL_IMAGE" | cut -d'/' -f2) | |
REPO_AND_DIGEST=$(echo "$FULL_IMAGE" | cut -d'/' -f3-) | |
REPO=$(echo "$REPO_AND_DIGEST" | cut -d'@' -f1) | |
DIGEST=$(echo "$REPO_AND_DIGEST" | cut -d':' -f2) | |
# Construct the API URL | |
API_URL="https://$REGISTRY/api/v2.0/projects/$PROJECT/repositories/$REPO/artifacts/sha256:$DIGEST/tags" | |
# Make the API call and extract tags | |
tags=$(curl -s "$API_URL" | jq -r '.[] | .name') | |
for t in ${tags}; do | |
echo ${REGISTRY}/${PROJECT}/${REPO}:${t} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment