From 4437f6bffaf28bc7f3d92d9affad8b0068415ea3 Mon Sep 17 00:00:00 2001 From: Richard Durso Date: Mon, 10 Jun 2024 08:37:49 -0400 Subject: [PATCH 1/2] Retain versioned tag over hash based image Fixes #5 --- cri-purge.sh | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cri-purge.sh b/cri-purge.sh index 4962d69..e0f62de 100644 --- a/cri-purge.sh +++ b/cri-purge.sh @@ -15,8 +15,8 @@ # This script requires root permissions to access the CRICTL binary. # AUTHOR="Richard J. Durso" -RELDATE="06/09/2024" -VERSION="0.1.1" +RELDATE="06/10/2024" +VERSION="0.1.2" ############################################################################# ###[ Define Variables ]####################################################### @@ -142,11 +142,15 @@ __reorder_hash_vales() { # See if image has is associated to a running container if [ -n "$( ${CRI_CMD} ps --image "$(echo "${IMAGES[$i]}" |awk '{ printf "%s\n", $3 }')" -q)" ]; then - # swap elment $i with last element - SWAP="${IMAGES[$elements]}" - - IMAGES[elements]="${IMAGES[$i]}" - IMAGES[i]=$SWAP + # If last image in array has a version tag, do not replace it. + # Try to preserve versions and purge dangling images + if [ "$(echo "${IMAGES[$elements]}" | awk '{ print $2 }')" = "" ]; then + # swap elment $i with last element + SWAP="${IMAGES[$elements]}" + + IMAGES[elements]="${IMAGES[$i]}" + IMAGES[i]=$SWAP + fi fi fi done From 6e1b0d66464b206920031530b8e3fa43f4fc47f9 Mon Sep 17 00:00:00 2001 From: Richard Durso Date: Mon, 10 Jun 2024 10:14:45 -0400 Subject: [PATCH 2/2] NEW: dry-run mode (do not purge images) --- README.md | 35 ++++++++++++++++++++++++++++------- cri-purge.sh | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 0351dcd..13e0438 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ $ cri-purge * ERROR: ROOT privilege required to access CRICTL binaries. - cri-purge | Version: 0.1.1 | 06/08/2024 | Richard J. Durso + cri-purge.sh | Version: 0.1.2 | 06/10/2024 | Richard J. Durso List and Purge downloaded cached images from containerd. ----------------------------------------------------------------------------- @@ -43,13 +43,14 @@ $ cri-purge It will do best effort to honor semantic versioning always leaving the newest version of the downloaded image and only purge previous version(s). - -h, --help : This usage statement. - -l, --list : List cached images and which versions could be purged. - -p, --purge : List images and PURGE/PRUNE older versioned images. - -pd, --purge-dangling : Same as --purge, but include dangling images.. - -s, --show-dangling : List dangling images with '' tag. + -h, --help : This usage statement. + -dp, --dry-run : Dry run --purge, Do not actually purge any images. + -dpd, --dry-run-dangling: Same as --dry-run, but include dangling images. + -l, --list : List cached images and purgable versions. + -p, --purge : List images and PURGE/PRUNE older versioned images. + -pd, --purge-dangling : Same as --purge, but include dangling images. + -s, --show-dangling : List dangling images with '' tag. - By default dangling images with '' tag are not purged. ``` --- @@ -146,6 +147,26 @@ Here `cri-purge.sh` has checked `crictl` and determined hash `3c937764e6f5d` is --- +### To `dry-run` or `dry-run-dangling` + +Dry run will go through all the steps to determine images to be purged, but will NOT actually purge any images. + +```shell +sudo cri-purge --dry-run +``` + +* Dry run of versioned image purge process. + +```shell +sudo cri-purge --dry-run-dangling +``` + +* Dry run of dangling images (tag: ``) purge process. + +The dry-run process will include header output of `DRY-RUN - No images will be purged.` + +--- + ## Using it with OpenShift (Code Ready Containers) The steps below have been verified on OpenShift CRC 4.13 on RedHat CoreOS: diff --git a/cri-purge.sh b/cri-purge.sh index e0f62de..56d9f98 100644 --- a/cri-purge.sh +++ b/cri-purge.sh @@ -27,6 +27,9 @@ CRIINFO_CMD="crio-status" # Define regex to match images with version of some type IMAGE_VERSION_REGEX="^\S+\s+[\w\-_\.\d]+\s+.*" +# Dry run feature is disabled by default +DRY_RUN=0 + ###[ Routines ]############################################################## __usage() { echo " @@ -40,13 +43,13 @@ __usage() { It will do best effort to honor semantic versioning always leaving the newest version of the downloaded image and only purge previous version(s). - -h, --help : This usage statement. - -l, --list : List cached images and which versions could be purged. - -p, --purge : List images and PURGE/PRUNE older versioned images. - -pd, --purge-dangling : Same as --purge, but include dangling images. - -s, --show-dangling : List dangling images with '' tag. - - By default dangling images with '' tag are not purged. + -h, --help : This usage statement. + -dp, --dry-run : Dry run --purge, Do not actually purge any images. + -dpd, --dry-run-dangling: Same as --dry-run, but include dangling images. + -l, --list : List cached images and purgable versions. + -p, --purge : List images and PURGE/PRUNE older versioned images. + -pd, --purge-dangling : Same as --purge, but include dangling images. + -s, --show-dangling : List dangling images with '' tag. " } @@ -183,6 +186,7 @@ __process_images() { __generate_image_list "${1^^}" echo + ((DRY_RUN)) && echo "DRY-RUN - No images will be purged." echo "Total Images: ${TOTAL_CRI_IMAGES} Unique Versioned Images Names: ${TOTAL_UNIQUE_IMAGE_NAMES}" echo COUNT=0 @@ -216,9 +220,11 @@ __process_images() { # Remove the Specific Image:TAG, or by a hash (if tag=) if [ "$TAG_REF" = "\$3" ]; then - ${CRI_CMD} rmi "$(echo "${IMAGES[$i]}" |awk '{ printf "%s\n", $3 }')" > /dev/null 2>&1 + # Only execute if NOT a dry-run + ! ((DRY_RUN)) && ${CRI_CMD} rmi "$(echo "${IMAGES[$i]}" |awk '{ printf "%s\n", $3 }')" > /dev/null 2>&1 else - ${CRI_CMD} rmi "$(echo "${IMAGES[$i]}" |awk '{ printf "%s:%s\n", $1, $2 }')" > /dev/null 2>&1 + # Only execute if NOT a dry-run + ! ((DRY_RUN)) && ${CRI_CMD} rmi "$(echo "${IMAGES[$i]}" |awk '{ printf "%s:%s\n", $1, $2 }')" > /dev/null 2>&1 fi else echo "- Purgeable TAG: $( echo "${IMAGES[$i]}" | awk "{ printf \"%s (%s)\n\", $TAG_REF, \$4 }")" @@ -266,6 +272,20 @@ if [ "$#" -ne 0 ]; then __process_images LIST exit 0 ;; + -dp|--dry-run) + # Enable Dry Run + DRY_RUN=1 + if [ -d "${IMAGE_STORE}" ]; then + __process_images "PURGE" + fi + ;; + -dpd|--dry-run-dangling) + # Enable Dry Run + DRY_RUN=1 + if [ -d "${IMAGE_STORE}" ]; then + __process_images "PURGE-DANGLING" + fi + ;; -p|--purge) if [ -d "${IMAGE_STORE}" ]; then START_DISK_SPACE=$(du -ab "${IMAGE_STORE}" | sort -n -r | head -1 | awk '{ print $1 }')