Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: directly referenced images via digest get deleted even if running #45

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,34 @@ def discover_delete_images(regionname):

# Get ImageDigest from ImageURL for running images. Do this for every repository
running_sha = []
for image in tagged_images:
for tag in image['imageTags']:
imageurl = repository['repositoryUri'] + ":" + tag
for runningimages in running_containers:
if imageurl == runningimages:
if imageurl not in running_sha:
running_sha.append(image['imageDigest'])
for running_image in running_containers:
repository_uri = repository['repositoryUri']

# get uri from running image - cut off the tag and digest
uri = re.search(r"^[^@:]+", running_image).group(0)
if not uri == repository_uri:
continue

# Get the digest of the running image
digest = None

# check if image is directly referenced by digest e.g. @sha256:1234567890abcdef
running_digest_match = re.search(r"@([^@]+)$", running_image)
if running_digest_match:
digest = running_digest_match.group(1)
else:
# the image is referenced by tag - lookup the digest for this tag
tag = running_image.split(":")[1]
try:
image = [x for x in tagged_images if tag in x['imageTags']]
digest = image[0]['imageDigest']
except:
# A container is using an image that is not existend anymore?
print("Error: Image tag '{}' not found in tagged images".format(tag))
continue

if digest is not None and digest not in running_sha:
running_sha.append(digest)

print("Number of running images found {}".format(len(running_sha)))
ignore_tags_regex = re.compile(IGNORE_TAGS_REGEX)
Expand Down