Skip to content

Commit

Permalink
main.py: Add filter to clean image with tag match
Browse files Browse the repository at this point in the history
Inspired by https://github.com/awslabs/ecr-cleanup-lambda/pull/17/files

Add parameter to allow only deleting images where the tag contains a
specific string

Specify a regex to match tags. If no tags on the image match, the image
will be skipped/ignored.

Allow us to only clean a specifig type/tag of images
  • Loading branch information
talset committed Oct 12, 2018
1 parent ae49a05 commit 92433aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,19 @@ Deletes the images that are not used by running tasks and which are older than t

`python main.py –dryrun False –imagestokeep 20 –region us-west-2 -ignoretagsregex release|archive -repositories foo bar`

Deletes the images that are not used by running tasks and which are older than the last 20 versions (in foo and bar repositories), in Oregon only, select only images with tag begening by `^re` but ignore image tags that contains `release` or `review`:

`python main.py –dryrun False –imagestokeep 20 –region us-west-2 -ignoretagsregex release|review -filtertagsregex ^re -repositories foo bar`

Environment variables can be used instead of arguments:

```
export REGION=us-west-2
export DRYRUN=False
export IMAGES_TO_KEEP=20
export REPOSITORIES_FILTER='foo bar'
export IGNORE_TAGS_REGEX='release|archive'
export IGNORE_TAGS_REGEX='release|review'
export FILTER_TAGS_REGEX='^re'
python main.py
```
16 changes: 13 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
DRYRUN = None
IMAGES_TO_KEEP = None
IGNORE_TAGS_REGEX = None
FILTER_TAGS_REGEX = None
REPOSITORIES_FILTER = None

def handler(event, context):
Expand All @@ -38,10 +39,18 @@ def handler(event, context):
discover_delete_images(REGION)

def ignore_tags_image(image,running_sha):
skip = False
if (FILTER_TAGS_REGEX != ""):
skip = True

for tag in image['imageTags']:
if "latest" in tag or re.compile(IGNORE_TAGS_REGEX).search(tag) is not None or (running_sha and image['imageDigest'] in running_sha):
if ( "latest" in tag
or re.compile(IGNORE_TAGS_REGEX).search(tag) is not None
or (running_sha and image['imageDigest'] in running_sha)):
return True
return False
if (re.compile(FILTER_TAGS_REGEX).search(tag) is not None):
skip=False
return skip


def discover_delete_images(regionname):
Expand Down Expand Up @@ -122,7 +131,6 @@ def discover_delete_images(regionname):

image_count = 0
for image in tagged_images:
print(image['imageDigest'])
if ignore_tags_image(image,running_sha):
continue
if image_count >= int(IMAGES_TO_KEEP):
Expand Down Expand Up @@ -199,6 +207,7 @@ def delete_images(ecr_client, deletesha, deletetag, id, name):
parser.add_argument('-repositories', help='Filter for repositories names discovery separated by space', default=os.environ.get('REPOSITORIES_FILTER', "").split(),
nargs='+', action='store', dest='repositories_filter')
parser.add_argument('-ignoretagsregex', help='Regex of tag names to ignore', default=os.environ.get('IGNORE_TAGS_REGEX', ""), action='store', dest='ignoretagsregex')
parser.add_argument('-filtertagsregex', help='Regex filter of tag names to limit on images with matching tag', default=os.environ.get('FILTER_TAGS_REGEX', ""), action='store', dest='filtertagsregex')

args = parser.parse_args()

Expand All @@ -210,6 +219,7 @@ def delete_images(ecr_client, deletesha, deletetag, id, name):

IMAGES_TO_KEEP = args.imagestokeep
IGNORE_TAGS_REGEX = args.ignoretagsregex
FILTER_TAGS_REGEX = args.filtertagsregex
REPOSITORIES_FILTER = args.repositories_filter

handler(request, None)

0 comments on commit 92433aa

Please sign in to comment.