Skip to content

Commit c451cb6

Browse files
hposcaanshrma
authored andcommitted
Avoid zero-length matches (awslabs#30)
With the default value of `""` for the `IGNORE_TAGS_REGEX` we ended up with zero-length matches, which caused many image tags to be ignored as the matching didn't return a `None`. An example that can demonstrate this behavior is: ```python import re REGEX='' string='something' response = re.compile(REGEX).match(string) print("Type: {}".format(type(response))) # Type: <type '_sre.SRE_Match'> print("First position of matching: {}".format(response.start())) # 0 print("Last position of matching: {}".format(response.end())) # 0 print("---") REGEX='^$' string='something' response = re.compile(REGEX).match(string) print("Type: {}".format(type(response))) # Type: <type 'NoneType'> ``` --- Before this modification we had: ``` Starting with repository :... Total number of images found: 513 Number of untagged images found 3 Number of running images found 1 Number of images to be deleted: 3 ``` And after: ``` Starting with repository :... Total number of images found: 513 Number of untagged images found 3 Number of running images found 1 Number of images to be deleted: 503 ``` Which is more aligned with the expected behavior.
1 parent 60a97c1 commit c451cb6

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def initialize():
4141
else:
4242
DRYRUN = True
4343
IMAGES_TO_KEEP = int(os.environ.get('IMAGES_TO_KEEP', 100))
44-
IGNORE_TAGS_REGEX = os.environ.get('IGNORE_TAGS_REGEX', "")
44+
IGNORE_TAGS_REGEX = os.environ.get('IGNORE_TAGS_REGEX', "^$")
4545

4646
def handler(event, context):
4747
initialize()
@@ -202,7 +202,7 @@ def delete_images(ecr_client, deletesha, deletetag, id, name):
202202
parser.add_argument('-imagestokeep', help='Number of image tags to keep', default='100', action='store',
203203
dest='imagestokeep')
204204
parser.add_argument('-region', help='ECR/ECS region', default=None, action='store', dest='region')
205-
parser.add_argument('-ignoretagsregex', help='Regex of tag names to ignore', default="", action='store', dest='ignoretagsregex')
205+
parser.add_argument('-ignoretagsregex', help='Regex of tag names to ignore', default="^$", action='store', dest='ignoretagsregex')
206206

207207
args = parser.parse_args()
208208
if args.region:

0 commit comments

Comments
 (0)