Skip to content

Commit

Permalink
Merge pull request #8 from reefland/reefland/issue5
Browse files Browse the repository at this point in the history
Reefland/issue5
  • Loading branch information
reefland authored Jun 10, 2024
2 parents 1d295ef + 6e1b0d6 commit 53eac00
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 23 deletions.
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
-----------------------------------------------------------------------------
Expand All @@ -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 '<none>' 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 '<none>' tag.

By default dangling images with '<none>' tag are not purged.
```

---
Expand Down Expand Up @@ -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: `<none>`) 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:
Expand Down
56 changes: 40 additions & 16 deletions cri-purge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]#######################################################
Expand All @@ -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 "
Expand All @@ -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 '<none>' tag.
By default dangling images with '<none>' 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 '<none>' tag.
"
}
Expand Down Expand Up @@ -142,11 +145,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 }')" = "<none>" ]; then
# swap elment $i with last element
SWAP="${IMAGES[$elements]}"

IMAGES[elements]="${IMAGES[$i]}"
IMAGES[i]=$SWAP
fi
fi
fi
done
Expand Down Expand Up @@ -179,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
Expand Down Expand Up @@ -212,9 +220,11 @@ __process_images() {

# Remove the Specific Image:TAG, or by a hash (if tag=<none>)
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 }")"
Expand Down Expand Up @@ -262,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 }')
Expand Down

0 comments on commit 53eac00

Please sign in to comment.